plt.figure() [numItems, numFeatures] = knn.data.shape for i in range(0, numItems / 2): itemClass = int(trainY[i]) currColor = colors[itemClass, :] plt.scatter(trainX[i, 0], trainX[i, 1], facecolor=currColor, edgecolor='black', s=50, lw=2) correctCount = 0 for i in range(0, numItemsTrain / 2): itemClass = int(testY[i]) currColor = colors[itemClass, :] prediction = int(knn.Predict(testX[i, :])) edgeColor = colors[prediction, :] if (itemClass == prediction): correctCount = correctCount + 1 plt.scatter(testX[i, 0], testX[i, 1], facecolor=currColor, edgecolor=edgeColor, s=50, lw=2) #print(correctCount) acc = int((float(correctCount) / (numItemsTrain / 2)) * 100) print("accuracy: " + str(acc) + "%") #plt.scatter(trainX[:,0:1].flatten(),trainX[:,1:2].flatten(),c=trainY) #plt.scatter(testX[:,0:1].flatten(),testX[:,1:2].flatten(),c=testY) plt.show()
pickle.dump(var, pickle_out) pickle_out.close() SaveDataToPickle([trainX, trainY, testX, testY], "KNN_dataset") knn = KNN() knn.Use_K_Of(15) knn.Fit(trainX,trainY) print('fitted.') import time start_time = time.time() if False: # using KNN_backup great = 0 for row in range(0,2000): prediction = knn.Predict(testX[row]) if prediction==testY[row]: great += 1 else: print('start.') step_size = 1 for i in range(0, testX.shape[0], step_size): predictions = knn.Predict(testX[i:i+step_size]) great = np.sum(np.equal(predictions,testY[i:i+step_size])) print("accuracy:", (1.*great)/step_size) if great!=step_size: # Error occur print(predictions) print(testY[i:i+step_size]) print(testX[i].shape)
for bone in range(0, 2): for joint in range(0, 3): X[row, col] = set1[finger, bone, joint, row] X[row + 1000, col] = set2[finger, bone, joint, row] col = col + 1 return X, y trainM = ReduceData(trainM) trainN = ReduceData(trainN) testM = ReduceData(testM) testN = ReduceData(testN) trainM = CenterData(trainM) trainN = CenterData(trainN) testM = CenterData(testM) testN = CenterData(testN) trainX, trainy = ReshapeData(trainM, trainN) testX, testy = ReshapeData(testM, testN) knn.Use_K_Of(15) knn.Fit(trainX, trainy) counter = 0 for row in range(2000): prediction = knn.Predict(testX[row, :]) if prediction == testy[row]: counter = counter + 1 print((counter / 2000) * 100)
trainX = knn.data[::2, 1:3] trainy = knn.target[::2] testX = knn.data[1::2, 1:3] testy = knn.target[1::2] colors = np.zeros((3, 3), dtype='f') colors[0, :] = [1, 0.5, 0.5] colors[1, :] = [0.5, 1, 0.5] colors[2, :] = [0.5, 0.5, 1] knn.Use_K_Of(15) knn.Fit(trainX, trainy) actualClass = testy[0] prediction = knn.Predict(testX[0, 0:2]) #visualization plt.figure() # plt.scatter(trainX[:,0],trainX[:,1],c=trainy) # plt.scatter(testX[:,0],testX[:,1],c=testy) [numItems, numFeatures] = knn.data.shape for i in range(0, numItems / 2): itemClass = int(trainy[i]) currColor = colors[itemClass, :] plt.scatter(trainX[i, 0], trainX[i, 1], facecolor=currColor, edgeColor=[0, 0, 0], s=50,
pickle_out.close() SaveDataToPickle([trainX, trainY, testX, testY], "KNN_dataset") knn = KNN() knn.Use_K_Of(15) knn.Fit(trainX, trainY) import time start_time = time.time() if False: # using KNN_backup great = 0 for row in range(0, 2000): prediction = knn.Predict(testX[row]) if prediction == testY[row]: great += 1 else: predictions = knn.Predict(testX) great = np.sum(np.equal(predictions, testY)) print("accuracy:", great / 10000.) print("time: ", time.time() - start_time) # First accuracy: 90.75% # Second accuracy: 97.4% # Third accuracy: 98.65% pickle.dump(knn, open('userData/classifier.p', 'wb'))
X = np.zeros((2000,5*2*3),dtype='f') y = np.zeros(2000,dtype='f') for row in range(0,1000): y[row] = 5 y[row+1000] = 6 col = 0 for finger in range(0,5): for bone in range(0,2): for tipAndBase in range(0,3): X[row,col] = set1[finger,bone,tipAndBase,row] X[row+1000,col] = set2[finger,bone,tipAndBase,row] col = col + 1 return X,y trainX,trainy= ReshapeData(train5,train6) testX,testy= ReshapeData(test5,test6) knn.Fit(trainX,trainy) # for row in range(0,2000): counter = 0 for row in range(0, 2000): itemClass = int(testy[row]) # prediction prediction = int(knn.Predict(testX[row])) if itemClass == prediction: counter += 1 # print counter print counter print counter/float(2000)*100
colors = np.zeros((3, 3), dtype='f') colors[0, :] = [1, 0.5, 0.5] colors[1, :] = [0.5, 1, 0.5] colors[2, :] = [0.5, 0.5, 1] #plt.scatter(trainX[:,0], trainX[:,1], c=trainy) #plt.show() #plt.scatter(testX[:,0], testX[:,1], c=testy) #plt.show() knn.Use_K_Of(15) knn.Fit(trainX, trainy) actualClass = testy[70] prediction = knn.Predict(testX[70, 1:3]) print(actualClass, prediction) [numItems, numFeatures] = knn.data.shape for i in range(0, numItems / 2): itemClass = int(trainy[i]) currColor = colors[itemClass, :] plt.scatter(trainX[i, 0], trainX[i, 1], facecolor=currColor, s=50, lw=2) counter = 0 for i in range(0, numItems / 2): itemClass = int(testy[i]) currColor = colors[itemClass, :] prediction = int(knn.Predict(testX[i, 1:3])) if prediction == int(testy[i]):
test9 = CenterData(test9) # add new one trainX, trainy = ReshapeData(train0, train1, train2, train3, train4, train5, train6, train7, train8, train9) testX, testy = ReshapeData(test0, test1, test2, test3, test4, test5, test6, test7, test8, test9) knn = KNN() knn.Use_K_Of(25) trainX = trainX.astype(int) trainy = trainy.astype(int) knn.Fit(trainX, trainy[:, 0]) actualClass = testy[0] prediction = knn.Predict(testX[0, :]) #print(actualClass, prediction) #prediction = knn.Predict(testX[1]) #print(prediction) counter = 0 # add 1000 for row in range(0, 10000): prediction = int(knn.Predict(testX[row, :])) if prediction == int(testy[row, 0]): counter += 1 print("Number correct is %s" % str(counter)) pickle.dump(knn, open('userData/classifier.p', 'wb'))