import numpy as np #bring in data from training file i = 0 x = [] #inputs x ty = [] #outputs ty f = open("classificationReduced.tra") for line in f.readlines(): #first 2 cols are inputs, last col is class x.append(list(map(float, line.split()))[0:2]) #ty.append(list(map(float, line.split()))[2]) #change classes to -1 and 1 instead of 1 and 2 (so splits across 0) if list(map(float, line.split()))[2] == 1: ty.append(-1) else: ty.append(1) i=i+1 print("TRAINING DATA") print("Length of training set: %d , %d " % (len(x), len(ty))) #input("Press Enter to view input data...") #print('x:') #print(x) #input("Press Enter to view output data...") #print('ty:') #print(ty) #x-augmented, copy x add a column of all ones xa = np.append(x,np.ones([len(x),1]),1) print("Shape xa: " + str(np.shape(xa))) print("Shape ty: " + str(np.shape(ty))) #print(xa) Nin = 3 Nout = 1 #bring in data from TEST file i2 = 0 x2 = [] #inputs x ty_test = [] #outputs ty f2 = open("classification.tst") for line in f2.readlines(): #first 2 cols are inputs, last col is class x2.append(list(map(float, line.split()))[0:2]) #ty_test.append(list(map(float, line.split()))[2]) #change classes to -1 and 1 instead of 1 and 2 (so splits across 0) if list(map(float, line.split()))[2] == 1: ty_test.append(-1) else: ty_test.append(1) i2=i2+1 print("\nTEST DATA") print("Length of test set: %d , %d " % (len(x2), len(ty_test))) #input("Press Enter to view input data...") #print('x2:') #print(x2) #input("Press Enter to view output data...") #print('ty_test:') #print(ty_test) #x-augmented, copy x add a column of all ones xa_test = np.append(x2,np.ones([len(x2),1]),1) print("Shape xa_test: " + str(np.shape(xa_test))) print("Shape ty_test: " + str(np.shape(ty_test))) input("\nPress Enter to continue...") print("Calculating auto-correlation...") #auto-correlation xTx R = [[0.0 for j in range(Nin)] for i in range(Nin)] for xarow in xa: for i in range(Nin): for j in range(Nin): R[i][j] = R[i][j] + (xarow[i] * xarow[j]) print("Calculating cross-correlation...") #cross-correlation xTty C = [[0.0 for j in range(Nin)] for i in range(Nout)] for n in range(len(xa)): for i in range(Nout): for j in range(Nin): #C[i][j] = C[i][j] + (ty[n][i] * xa[n][j]) C[i][j] = C[i][j] + (ty[n] * xa[n][j]) #print("Shape R: " + str(np.shape(R)) + " Shape C: " + str(np.shape(C))) print("Normalizing correlations...") #normalize (1/Nv) for i in range(Nin): for j in range(Nin): R[i][j] = (R[i][j]/(len(xa))) + 0.0 #regularization coeff. for i in range(Nout): for j in range(Nin): C[i][j] = C[i][j]/(len(ty)) meanseed = 0.0 stddevseed = 0.5 ##set up W w0 = [[0.0 for j in range(Nin-1)] for i in range(Nout)] W = [[0.0 for j in range(Nin)] for i in range(Nout)] for i in range(Nout): for j in range(Nin-1): #assign random weight for initial value w0[i][j] = np.random.normal(meanseed,stddevseed) #0.1 W[i][j] = w0[i][j] W[i][Nin-1] = np.random.normal(meanseed,stddevseed) #0.1 #conjugate gradient subroutine (this could be called as a function) #input("Press enter to calculate weights...") print("Calculating weights...") for i in range(Nout): #loop around CGrad in sample passiter = 0 XD = 1.0 #copying matrix parts needed w = W[i] r = R c = C[i] Nu = Nin while passiter < 2: #2 passes p = [0.0 for j in range(Nu)] g = [0.0 for j in range(Nu)] for j in range(Nu): #equivalent to "iter" loop in sample code (again, check loop values) for k in range(Nu): #equiv to l loop in sample tempg = 0.0 for m in range(Nu): tempg = tempg + w[m]*r[m][k] g[k] = -2.0*c[k] + 2.0*tempg XN = 0.0 for k in range(Nu): XN = XN + g[k] * g[k] B1 = XN / XD XD = XN for k in range(Nu): p[k] = -g[k] + B1*p[k] ##if j == 1 and k < 2: ## print("k: %d, p[k]: %f, g[k]: %f" % (k,p[k],g[k])) Den = 0.0 Num = Den for k in range(Nu): #numerator of B2 Num = Num + p[k] * g[k] / -2.0 ##if j == 1 and k < 2: ## print("k: %d, p[k]: %f, g[k]: %f" % (k,p[k],g[k])) #denominator of B2 for m in range(Nu): Den = Den + p[m] * p[k] * r[m][k] ##if j==1 and k < 2: ## print("j: " + str(j) + ", k: " + str(k) + ", Num: " + str(Num) + ", Den: " + str(Den)) B2 = Num / Den #update weights for k in range(Nu): w[k] = w[k] + B2 * p[k] passiter += 1 #after the two passes, store back in W[i] before next i W[i] = w print("\nW: ") print(W) #show predictions class_predict = [0.0 for i in range(len(xa))] predict_correct = [] pred_corr_class1 = [] pred_corr_class2 = [] for N in range(len(xa)): for i in range(Nout): y=0.0 for j in range(Nin): y += xa[N][j]*W[i][j] if y >= 0: class_predict[N] = 1 else: class_predict[N] = -1 #print("ty[%d]: %d, class_predict[%d]: %d" % (N,ty[N],N,class_predict[N])) if class_predict[N] == ty[N]: if ty[N] == 1: pred_corr_class2.append(1) if ty[N] == -1: pred_corr_class1.append(1) predict_correct.append(1) else: if ty[N] == 1: pred_corr_class2.append(0) if ty[N] == -1: pred_corr_class1.append(0) predict_correct.append(0) print ("\nCorrectly classified: %d, Percent: %f" % (sum(predict_correct),sum(predict_correct)/len(predict_correct))) print ("Class 1 correct: %d, Percent: %f" % (sum(pred_corr_class1),sum(pred_corr_class1)/len(pred_corr_class1))) print ("Class 2 correct: %d, Percent: %f" % (sum(pred_corr_class2),sum(pred_corr_class2)/len(pred_corr_class2))) #show predictions class_predict_test = [0.0 for i in range(len(xa_test))] predict_correct_test = [] pred_corr_test_class1 = [] pred_corr_test_class2 = [] for N in range(len(xa_test)): for i in range(Nout): y=0.0 for j in range(Nin): y += xa_test[N][j]*W[i][j] if y >= 0: class_predict_test[N] = 1 else: class_predict_test[N] = -1 #print("ty[%d]: %d, class_predict[%d]: %d" % (N,ty[N],N,class_predict[N])) if class_predict_test[N] == ty_test[N]: if ty_test[N] == 1: pred_corr_test_class2.append(1) if ty_test[N] == -1: pred_corr_test_class1.append(1) predict_correct_test.append(1) else: if ty_test[N] == 1: pred_corr_test_class2.append(0) if ty_test[N] == -1: pred_corr_test_class1.append(0) predict_correct_test.append(0) print ("\nCorrectly classified TEST set: %d, Percent: %f" % (sum(predict_correct_test),sum(predict_correct_test)/len(predict_correct_test))) print ("Class 1 correct: %d, Percent: %f" % (sum(pred_corr_test_class1),sum(pred_corr_test_class1)/len(pred_corr_test_class1))) print ("Class 2 correct: %d, Percent: %f" % (sum(pred_corr_test_class2),sum(pred_corr_test_class2)/len(pred_corr_test_class2)))