In [216]:
%matplotlib inline
import numpy as np
In [ ]:
In [6]:
N = 100
D = 2
In [8]:
X = np.random.randn(N, D)
In [11]:
np.shape(X)
Out[11]:
In [37]:
# 常數項
ones = np.array([[1]*N]).T
In [39]:
ones.shape
Out[39]:
In [40]:
Xb = np.concatenate([ones, X], axis=1)
In [41]:
Xb
Out[41]:
In [42]:
w = np.random.randn(D + 1) # 變項數 + 常數項
In [43]:
z = Xb.dot(w)
In [46]:
def sigmoid(z):
return 1/(1+ np.exp(-z))
In [47]:
print sigmoid(z)
In [ ]:
In [48]:
import numpy as np
import pandas as pd
In [49]:
df = pd.read_csv("../machine_learning_examples/ann_logistic_extra/ecommerce_data.csv")
In [81]:
df.head()
Out[81]:
In [82]:
df.describe()
Out[82]:
In [ ]:
In [135]:
def get_data():
df = pd.read_csv("../machine_learning_examples/ann_logistic_extra/ecommerce_data.csv")
data = df.as_matrix()
X = data[:, :-1]
Y = data[:, -1]
X[:,1] = (X[:,1] - X[:,1].mean())/ X[:,1].std()
X[:,2] = (X[:,2] - X[:,2].mean())/ X[:,2].std()
N, D = X.shape
# create new matrix including one-hot encoding
X2 = np.zeros((N,D+3))
# 只有 time_of_the_day需要做one-hot encoding
# time_of_the_day (1~3)
X2[:,0:(D-1)] = X[:, 0:(D-1)]
for n in xrange(N):
t = int(X[n, D-1])
X2[n, D-1+t] = 1
Z = np.zeros((N, 4))
Z[np.arange(N), X[:,D-1].astype(np.int32)]=1
assert(np.abs(X2[:,-4:] - Z).sum() < 10e-10)
return X2, Y
In [137]:
X2, Y = get_data()
In [139]:
Y
Out[139]:
In [141]:
def get_binary_data():
X, Y = get_data()
X2 = X[Y <= 1]
Y2 = Y[Y <= 1]
return X2, Y2
In [142]:
X , Y = get_binary_data()
In [143]:
N, D = X.shape
In [144]:
W = np.random.randn(D)
In [145]:
b = 0
In [149]:
def sigmoid(a):
return 1 / (1 + np.exp(-a))
In [150]:
def forward(X, W, b):
return sigmoid(X.dot(W) + b)
In [151]:
P_Y_given_X = forward(X, W ,b)
In [154]:
prediction = np.round(P_Y_given_X)
In [155]:
def classfication_rate(Y, P):
return np.mean(Y == P)
In [157]:
print ("score: %s" % classfication_rate(Y,prediction))
In [ ]:
In [ ]:
In [ ]:
In [ ]:
##################
In [85]:
df = pd.read_csv("../machine_learning_examples/ann_logistic_extra/ecommerce_data.csv")
data = df.as_matrix()
X = data[:, :-1]
Y = data[:, -1]
X[:,1] = (X[:,1] - X[:,1].mean())/ X[:,1].std()
X[:,2] = (X[:,2] - X[:,2].mean())/ X[:,2].std()
N, D = X.shape
# create new matrix including one-hot encoding
X2 = np.zeros((N,D+3))
# 只有 time_of_the_day需要做one-hot encoding
# time_of_the_day (1~3)
X2[:,0:(D-1)] = X[:, 0:(D-1)]
for n in xrange(N):
t = int(X[n, D-1])
X2[n, D-1+t] = 1
Z = np.zeros((N, 4))
In [114]:
Z.shape
Out[114]:
In [120]:
Z[np.arange(N), X[:,D-1].astype(np.int32)] = 1
In [125]:
type(X[:,D-1])
Out[125]:
In [158]:
np.arrange()
In [211]:
import seaborn as sns
import scipy
In [213]:
scipy.stats.norm.pdf(2)
Out[213]:
In [214]:
x = np.linspace(-3, 3, 100)
In [215]:
y = scipy.stats.norm.pdf(x)
In [217]:
sns.plt.plot(x,y)
Out[217]:
In [219]:
log_y = np.log(y)
In [220]:
sns.plt.plot(x, log_y)
Out[220]:
In [221]:
squar_y = y * y
In [222]:
sns.plt.plot(x, squar_y)
Out[222]:
In [1]:
import numpy as np
In [2]:
N = 100
D = 2
In [3]:
X = np.random.randn(N, D)
In [4]:
X[0]
Out[4]:
In [5]:
X[:50, :] = X[:50, :] - 2*np.ones((50,D))
In [6]:
X[0]
Out[6]:
In [7]:
X[50:, :] = X[50:, :] + 2*np.ones((50, D))
In [32]:
X[0]
Out[32]:
In [33]:
X
Out[33]:
In [ ]:
In [9]:
T = np.array([0]*50+[1]*50)
In [10]:
ones = np.array([[1]*N]).T
In [11]:
Xb = np.concatenate((ones,X), axis=1)
In [12]:
w = np.random.randn(D+1)
In [13]:
z = Xb.dot(w)
In [14]:
def sigmoid(z):
return 1 / (1+np.exp(-z))
In [15]:
Y = sigmoid(z)
In [16]:
# T: 實際值
# Y: 預測值
def cross_entropy(T, Y):
return -sum(T*np.log(Y)+(1-T)*np.log(1-Y))
In [17]:
cross_entropy(T,Y)
Out[17]:
In [18]:
T[0]
Out[18]:
In [19]:
Y[0]
Out[19]:
In [20]:
np.log(1-Y[0])
Out[20]:
In [21]:
np.log(1)
Out[21]:
In [22]:
w2 = np.array([0, 4, 4])
In [23]:
z2 = Xb.dot(w2)
In [24]:
Y2 = sigmoid(z2)
In [25]:
cross_entropy(T, Y2)
Out[25]:
In [26]:
%matplotlib inline
import seaborn as sns
In [31]:
sns.plt.scatter(X[:,0], X[:,1], c=T, s=100, alpha=0.5)
Out[31]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: