Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions cg.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def CG(f, w, max_fc, **argc):
LS_failed = False
f0, df0 = f(w, **argc)
fval = [f0]
print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f0)
print('Iter = %4.4i Cost = %lf' % (I, f0), file=sys.stderr)

s = -df0
d0 = float(-s.T * s)
Expand All @@ -49,12 +49,12 @@ def CG(f, w, max_fc, **argc):
I += 1
f3, df3 = f(w + w3 * s, **argc)
fval.append(f3)
print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f3)
print('Iter = %4.4i Cost = %lf' % (I, f3), file=sys.stderr)
if isnan(f3) or isinf(f3) or np.any(np.isnan(df3) + np.isinf(df3)):
raise NameError, ('error')
raise NameError('error')
success = True
except Exception, e:
print >> sys.stderr, 'Exception = %s' % e
except Exception as e:
print('Exception = %s' % e, file=sys.stderr)
trace()
w3 = (w2 + w3) / 2.0
if f3 < F0:
Expand All @@ -70,8 +70,8 @@ def CG(f, w, max_fc, **argc):
try:
w3 = w1 - d1 * (w2 - w1) ** 2 / (B + sqrt(B * B - A * d1 * (w2 - w1)))
# add sth
except Exception, e:
print >> sys.stderr, 'Exception = %s' % e
except Exception as e:
print('Exception = %s' % e, file=sys.stderr)
trace()
w3 = w2 * EXT
continue
Expand All @@ -94,8 +94,8 @@ def CG(f, w, max_fc, **argc):
A = 6 * (f2 - f4) / (w4 - w2) + 3 * (d4 + d2)
B = 3 * (f4 - f2) - (2 * d2 + d4) * (w4 - w2)
w3 = w2 + (sqrt(B * B - A * d2 * (w4 - w2) ** 2) - B) / A
except Exception, e:
print >> sys.stderr, 'Exception = %s' % e
except Exception as e:
print('Exception = %s' % e, file=sys.stderr)
trace()
w3 = float('NaN')
if isnan(w3) or isinf(w3):
Expand All @@ -107,12 +107,12 @@ def CG(f, w, max_fc, **argc):
w0, F0, dF0 = w + w3 * s, f3, df3
M -= 1
I += 1
print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f3)
print('Iter = %4.4i Cost = %lf' % (I, f3), file=sys.stderr)
d3 = float(df3.T * s)

if abs(d3) < -SIG * d0 and f3 < f0 + w3 * RHO * d0:
w, f0 = w + w3 * s, f3
print >> sys.stderr, 'Line_search = %4.4i Cost = %lf' % (J, f0)
print('Line_search = %4.4i Cost = %lf' % (J, f0), file=sys.stderr)
J += 1
s = float((df3.T * df3 - df0.T * df3) / (df0.T * df0)) * s - df3
df0 = df3
Expand All @@ -128,7 +128,7 @@ def CG(f, w, max_fc, **argc):
s, d0 = -df0, float(-s.T * s)
w3 = 1.0 / (1.0 - d0)
LS_failed = True
print >> sys.stderr, ''
print('', file=sys.stderr)
# print >> sys.stderr, fval
# print >> sys.stderr, ''
return w
Expand All @@ -140,4 +140,4 @@ def f(x):

x = 100.0 * np.matrix(np.ones([1, 1]))
x_opt = CG(f, x, 40)
print x_opt
print(x_opt)
22 changes: 11 additions & 11 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def trace():
'''
try:
raise Exception
except:
except Exception:
f = sys.exc_info()[2].tb_frame.f_back
print >> sys.stderr, 'function =', f.f_code.co_name, ', line =', f.f_lineno
print('function =', f.f_code.co_name, ', line =', f.f_lineno, file=sys.stderr)


def read_dense_data(fp_data):
Expand Down Expand Up @@ -105,12 +105,12 @@ def plot_sequence_data(x, s, w = 128):
L = max(len(p), len(q))

d = L - len(q)
m = d / 2
m = d // 2
n = d - m
x_out.append(p + ' ' * (L - len(p)))
s_out.append('-' * m + q + '-' * n)

m = (L - 1) / 2
m = (L - 1) // 2
n = L - 1 - m
t_out.append(' ' * m + '|' + ' ' * n)

Expand All @@ -119,14 +119,14 @@ def plot_sequence_data(x, s, w = 128):
x_line = ' '.join(x_out)

if len(s_line) > w:
for I in range(len(s_line) / w + 1):
print >> sys.stderr, s_line[I * w : (I + 1) * w]
print >> sys.stderr, t_line[I * w : (I + 1) * w]
print >> sys.stderr, x_line[I * w : (I + 1) * w]
for I in range(len(s_line) // w + 1):
print(s_line[I * w : (I + 1) * w], file=sys.stderr)
print(t_line[I * w : (I + 1) * w], file=sys.stderr)
print(x_line[I * w : (I + 1) * w], file=sys.stderr)
else:
print >> sys.stderr, s_line
print >> sys.stderr, t_line
print >> sys.stderr, x_line
print(s_line, file=sys.stderr)
print(t_line, file=sys.stderr)
print(x_line , file=sys.stderr)


def map_label(Y):
Expand Down
6 changes: 3 additions & 3 deletions lr.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def train(self, X, Y, lamb = 1.0):
self.w = SGD(self.cost, w, X, Y, opt, lamb = lamb)
'''

print 'Done with function evalution C = %d' % self.c
print('Done with function evalution C = %d' % self.c)

def test(self, X, Y):
m, n = X.shape
Expand Down Expand Up @@ -93,6 +93,6 @@ def cost(self, w, X, Y, lamb):
acc_train = clf.test(X_train, Y_train)
acc_test = clf.test(X_test, Y_test)

print >> sys.stderr, 'Training accuracy for Logistic Regression : %lf%%' % (100.0 * acc_train)
print >> sys.stderr, 'Test accuracy for Logistic Regression : %lf%%' % (100.0 * acc_test)
print('Training accuracy for Logistic Regression : %lf%%' % (100.0 * acc_train), file=sys.stderr)
print('Test accuracy for Logistic Regression : %lf%%' % (100.0 * acc_test), file=sys.stderr)

20 changes: 10 additions & 10 deletions nb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self):
def train(self, X, Y, event_model = Bernoulli, smooth = 0.5):

if event_model != Bernoulli and event_model != Multinomial:
print >> sys.stderr, 'event_model parameter error'
print('event_model parameter error', file=sys.stderr)
return

self.priors.clear()
Expand Down Expand Up @@ -69,15 +69,15 @@ def train(self, X, Y, event_model = Bernoulli, smooth = 0.5):
for y in self.priors:
self.priors[y] /= 1.0 * n

print >> sys.stderr, '-' * 128
print >> sys.stderr, 'Top 5 strong features for each class:'
print >> sys.stderr, '-' * 128
print('-' * 128, file=sys.stderr)
print('Top 5 strong features for each class:', file=sys.stderr)
print('-' * 128, file=sys.stderr)

for y in self.prob:
ftrs = sorted(self.prob[y].items(), key = lambda x : x[1], reverse = True)
print >> sys.stderr, y + '\t' + '\t'.join([k + ':' + str(round(v, 5)) for k,v in ftrs[0 : 5]])
print(y + '\t' + '\t'.join([k + ':' + str(round(v, 5)) for k,v in ftrs[0 : 5]]), file=sys.stderr)

print >> sys.stderr, '-' * 128
print('-' * 128, file=sys.stderr)

def test(self, X, Y):

Expand Down Expand Up @@ -114,13 +114,13 @@ def test(self, X, Y):
acc_train = clf.test(X_train, Y_train)
acc_test = clf.test(X_test, Y_test)

print >> sys.stderr, 'Training accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_train)
print >> sys.stderr, 'Test accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_test)
print('Training accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_train), file=sys.stderr)
print('Test accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_test), file=sys.stderr)

clf.train(X_train, Y_train, Multinomial)
acc_train = clf.test(X_train, Y_train)
acc_test = clf.test(X_test, Y_test)

print >> sys.stderr, 'Training accuracy for Multinomial event model : %f%%' % (100 * acc_train)
print >> sys.stderr, 'Test accuracy for Multinomial event model : %f%%' % (100 * acc_test)
print('Training accuracy for Multinomial event model : %f%%' % (100 * acc_train), file=sys.stderr)
print('Test accuracy for Multinomial event model : %f%%' % (100 * acc_test), file=sys.stderr)

4 changes: 2 additions & 2 deletions regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def train(self, X, Y, lamb = 0.0001):
'''
self.w = CG(self.cost, w, 200, X = X, Y = Y, lamb = lamb)

print 'Done with function evalution C = %d' % self.c
print('Done with function evalution C = %d' % self.c)

def test(self, X, Y):
m, n = X.shape
Expand All @@ -52,7 +52,7 @@ def test(self, X, Y):
r = X * self.w - Y
rmse = sqrt(float(r.T * r) / len(Y))

print >> sys.stderr, 'Test RMSE : %lf' % rmse
print('Test RMSE : %lf' % rmse, file=sys.stderr)
return rmse

# (1 / (2 * m)) * (X * w.T - Y) ^ 2 + (lamb / 2) * (w.T * w)
Expand Down