import sys import numpy as np import hw2_fc as fc w = np.load("logisticw.npy") xt = fc.readX(sys.argv[1]) xt = fc.fit(xt) xt = np.array(xt) xt = np.concatenate((np.ones((xt.shape[0], 1)), xt), axis=1) yt = [] for i in range(len(xt)): yt.append(fc.sign(fc.theta(np.dot(w, xt[i])))) fc.outputcsv(yt, sys.argv[2])
x = np.array(x) x = np.concatenate((np.ones((x.shape[0], 1)), x), axis=1) y = np.array(y) yval = np.array(yval) xval = np.array(xval) xval = np.concatenate((np.ones((xval.shape[0], 1)), xval), axis=1) lr = 0.1 w = np.array([1.0] * len(x[0])) s_gra = np.zeros(len(x[0])) for iter in range(60): for a in range(lx): fx = fc.theta(np.dot(w, x[a])) gra = (fx - y[a]) * x[a] s_gra += gra**2 ada = np.sqrt(s_gra) w -= lr * gra / ada err = 0 for i in range(lx): if fc.sign(fc.theta(np.dot(w, x[i]))) != y[i]: err += 1 errval = 0 for i in range(lxval): if fc.sign(fc.theta(np.dot(w, xval[i]))) != yval[i]: errval += 1 print("Iteration = %d | Accuracyin = %f | Accruacyout = %f" % (iter, 1 - err / lx, 1 - errval / lxval)) np.save("logisticw.npy", w)
pc = np.load("generative_pc.npy") pxc = np.load("generative_pxc.npy") xt = fc.readX(sys.argv[1]) lxt = len(xt) mean = [0] * len(xt[0]) for feature in fc.noz: for xs in range(lxt): mean[feature] += xt[xs][feature] / lxt yt = [] for i in range(lxt): up, down = 1, 1 for feature in range(len(xt[i])): if feature not in fc.noz: up *= pxc[1][feature][xt[i][feature]] down *= pxc[0][feature][xt[i][feature]] else: if xt[i][feature] > mean[feature]: up *= pxc[1][feature][1] down *= pxc[0][feature][1] else: up *= pxc[1][feature][0] down *= pxc[0][feature][0] up *= pc[1] down *= pc[0] yt.append(fc.sign(up / (up + down))) fc.outputcsv(yt, sys.argv[2])