for i in range(ln): XS[i] = HT.dot(train_in[i]) return XS def plot(x): plt.plot([i for i in range(1,257)], x) plt.show() if __name__ == '__main__': M = 12 train_in, train_out, test_in, test_out = loaddata() #plot(train_in[101]) print train_in.shape #print outputs.shape knots = createknots(1,256) print knots p = len(train_in[0]) H = basismatrix(knots, p, M) XS = transform(H, train_in, M) print XS.shape print np.linalg.matrix_rank(XS) beta = lg.logistic(XS, train_out) print beta error = 0 TXS = transform(H, test_in, M) for i in range(len(TXS)): res = lg.predict(TXS[i], beta) if test_out[i] != res: error += 1 print 'error ratio:', float(error)/len(TXS)
row = [1] for j in range(len(data[0])): seg = [] if j == 3: seg.append(data[i,j]) row = row + seg continue for k in range(0,K): seg.append(N(data[i,j], k, knots[j])) row = row + seg[1:] new_matrix[i] = np.array(row) return new_matrix if __name__ == '__main__': K = 5 X, Y = hd.loaddata() knots = split(X, K) print knots H = cookdata(X, K, knots) np.savetxt('H.dat', H, delimiter=',') print H.shape print np.linalg.matrix_rank(H) beta = lg.logistic(H, Y) print beta error = 0 for i in range(len(H)): res = lg.predict(H[i], beta) if Y[i] != res: error += 1 print 'error ratio:', float(error)/len(X)