Skip to content

Commit

Permalink
add log(0, 3) + z-score normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
orbxball committed Apr 2, 2017
1 parent b673154 commit a769621
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions hw2/best.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ def sigmoid(z):
cubic = [0, 1, 3, 4, 5]
X_train = np.concatenate((X_train,
X_train[:, square]**2,
X_train[:, cubic]**3), axis=1)
X_train[:, cubic]**3,
np.log(X_train[:, [0, 3]] + 1e-100)), axis=1)

# scaling: only on features, not label
maxnum = np.max(X_train, axis=0) #shape: (106,)
minnum = np.min(X_train, axis=0) #shape: (106,)
X_train = (X_train - minnum) / (maxnum - minnum + 1e-100)
mean = np.mean(X_train, axis=0) #shape: (106,)
std = np.std(X_train, axis=0) #shape: (106,)
X_train = (X_train - mean) / (std + 1e-100)

# initialize
b = 0.0
w = np.ones(X_train.shape[1])
lr = 5e-1
epoch = 10000
epoch = 3000
b_lr = 0.0
w_lr = np.zeros(X_train.shape[1])

Expand All @@ -64,10 +65,8 @@ def sigmoid(z):
if (e+1) % 100 == 0:
f[f >= 0.5] = 1
f[f < 0.5] = 0
acc = Y_train - f #shape: (32561,)
acc[acc == 0] = 2
acc[acc != 2] = 0
print('epoch:{}\n Loss:{}\n Accuracy:{}%\n'.format(e+1, loss, np.sum(acc) * 50 / acc.shape[0]))
acc = Y_train == f #shape: (32561,)
print('epoch:{}\n Loss:{}\n Accuracy:{}%\n'.format(e+1, loss, np.sum(acc) * 100 / acc.shape[0]))


# Test
Expand All @@ -87,11 +86,12 @@ def sigmoid(z):
# X_test select
X_test = np.concatenate((X_test,
X_test[:, square]**2,
X_test[:, cubic]**3), axis=1)
X_test[:, cubic]**3,
np.log(X_test[:, [0, 3]] + 1e-100)), axis=1)
ans = []
for i in range(X_test.shape[0]):
Z = X_test[i]
Z = (Z - minnum) / (maxnum - minnum + 1e-100)
Z = (Z - mean) / (std + 1e-100)
z = np.dot(w, Z) + b
if (z >= 0):
ans.append('{},{}'.format(i+1, 1))
Expand Down

0 comments on commit a769621

Please sign in to comment.