Skip to content

Commit 06c6029

Browse files
python3
1 parent 3e7005a commit 06c6029

File tree

6 files changed

+54
-17
lines changed

6 files changed

+54
-17
lines changed

ann_logistic_extra/ann_predict.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
from process import get_data
39

@@ -22,10 +28,11 @@ def forward(X, W1, b1, W2, b2):
2228
return softmax(Z.dot(W2) + b2)
2329

2430
P_Y_given_X = forward(X, W1, b1, W2, b2)
31+
print("P_Y_given_X.shape:", P_Y_given_X.shape)
2532
predictions = np.argmax(P_Y_given_X, axis=1)
2633

2734
# calculate the accuracy
2835
def classification_rate(Y, P):
2936
return np.mean(Y == P)
3037

31-
print "Score:", classification_rate(Y, predictions)
38+
print("Score:", classification_rate(Y, predictions))

ann_logistic_extra/ann_train.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
import matplotlib.pyplot as plt
39

@@ -7,7 +13,7 @@
713
def y2indicator(y, K):
814
N = len(y)
915
ind = np.zeros((N, K))
10-
for i in xrange(N):
16+
for i in range(N):
1117
ind[i, y[i]] = 1
1218
return ind
1319

@@ -56,7 +62,7 @@ def cross_entropy(T, pY):
5662
train_costs = []
5763
test_costs = []
5864
learning_rate = 0.001
59-
for i in xrange(10000):
65+
for i in range(10000):
6066
pYtrain, Ztrain = forward(Xtrain, W1, b1, W2, b2)
6167
pYtest, Ztest = forward(Xtest, W1, b1, W2, b2)
6268

@@ -72,10 +78,10 @@ def cross_entropy(T, pY):
7278
W1 -= learning_rate*Xtrain.T.dot(dZ)
7379
b1 -= learning_rate*dZ.sum(axis=0)
7480
if i % 1000 == 0:
75-
print i, ctrain, ctest
81+
print(i, ctrain, ctest)
7682

77-
print "Final train classification_rate:", classification_rate(Ytrain, predict(pYtrain))
78-
print "Final test classification_rate:", classification_rate(Ytest, predict(pYtest))
83+
print("Final train classification_rate:", classification_rate(Ytrain, predict(pYtrain)))
84+
print("Final test classification_rate:", classification_rate(Ytest, predict(pYtest)))
7985

8086
legend1, = plt.plot(train_costs, label='train cost')
8187
legend2, = plt.plot(test_costs, label='test cost')

ann_logistic_extra/logistic_predict.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
from process import get_binary_data
39

@@ -22,4 +28,4 @@ def forward(X, W, b):
2228
def classification_rate(Y, P):
2329
return np.mean(Y == P)
2430

25-
print "Score:", classification_rate(Y, predictions)
31+
print("Score:", classification_rate(Y, predictions))

ann_logistic_extra/logistic_softmax_train.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
import matplotlib.pyplot as plt
39

@@ -7,7 +13,7 @@
713
def y2indicator(y, K):
814
N = len(y)
915
ind = np.zeros((N, K))
10-
for i in xrange(N):
16+
for i in range(N):
1117
ind[i, y[i]] = 1
1218
return ind
1319

@@ -52,7 +58,7 @@ def cross_entropy(T, pY):
5258
train_costs = []
5359
test_costs = []
5460
learning_rate = 0.001
55-
for i in xrange(10000):
61+
for i in range(10000):
5662
pYtrain = forward(Xtrain, W, b)
5763
pYtest = forward(Xtest, W, b)
5864

@@ -65,10 +71,10 @@ def cross_entropy(T, pY):
6571
W -= learning_rate*Xtrain.T.dot(pYtrain - Ytrain_ind)
6672
b -= learning_rate*(pYtrain - Ytrain_ind).sum(axis=0)
6773
if i % 1000 == 0:
68-
print i, ctrain, ctest
74+
print(i, ctrain, ctest)
6975

70-
print "Final train classification_rate:", classification_rate(Ytrain, predict(pYtrain))
71-
print "Final test classification_rate:", classification_rate(Ytest, predict(pYtest))
76+
print("Final train classification_rate:", classification_rate(Ytrain, predict(pYtrain)))
77+
print("Final test classification_rate:", classification_rate(Ytest, predict(pYtest)))
7278

7379
legend1, = plt.plot(train_costs, label='train cost')
7480
legend2, = plt.plot(test_costs, label='test cost')

ann_logistic_extra/logistic_train.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
import matplotlib.pyplot as plt
39

@@ -37,7 +43,7 @@ def cross_entropy(T, pY):
3743
train_costs = []
3844
test_costs = []
3945
learning_rate = 0.001
40-
for i in xrange(10000):
46+
for i in range(10000):
4147
pYtrain = forward(Xtrain, W, b)
4248
pYtest = forward(Xtest, W, b)
4349

@@ -50,10 +56,10 @@ def cross_entropy(T, pY):
5056
W -= learning_rate*Xtrain.T.dot(pYtrain - Ytrain)
5157
b -= learning_rate*(pYtrain - Ytrain).sum()
5258
if i % 1000 == 0:
53-
print i, ctrain, ctest
59+
print(i, ctrain, ctest)
5460

55-
print "Final train classification_rate:", classification_rate(Ytrain, np.round(pYtrain))
56-
print "Final test classification_rate:", classification_rate(Ytest, np.round(pYtest))
61+
print("Final train classification_rate:", classification_rate(Ytrain, np.round(pYtrain)))
62+
print("Final test classification_rate:", classification_rate(Ytest, np.round(pYtest)))
5763

5864
legend1, = plt.plot(train_costs, label='train cost')
5965
legend2, = plt.plot(test_costs, label='test cost')

ann_logistic_extra/process.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
from __future__ import print_function, division
2+
from builtins import range
3+
# Note: you may need to update your version of future
4+
# sudo pip install -U future
5+
6+
17
import numpy as np
28
import pandas as pd
39
import os
@@ -30,7 +36,7 @@ def get_data():
3036
X2[:,0:(D-1)] = X[:,0:(D-1)] # non-categorical
3137

3238
# one-hot
33-
for n in xrange(N):
39+
for n in range(N):
3440
t = int(X[n,D-1])
3541
X2[n,t+D-1] = 1
3642

0 commit comments

Comments
 (0)