X[:, :, 2, :] = allZCoordinates - meanValue return X train3 = ReduceData(train3) train4 = ReduceData(train4) test3 = ReduceData(test3) test4 = ReduceData(test4) train3 = CenterData(train3) train4 = CenterData(train4) test3 = CenterData(test3) test4 = CenterData(test4) trainX, trainy = ReshapeData(train3, train4) testX, testy = ReshapeData(test3, test4) knn = knn.KNN() knn.Use_K_Of(15) knn.Fit(trainX, trainy) correctPredictions = 0 for row in range(0, 2000): actualClass = testy[row] prediction = knn.Predict(testX[row]) if (actualClass == prediction): correctPredictions = correctPredictions + 1 print(correctPredictions) print((correctPredictions / 2000) * 100)
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, s=50, lw=2, edgecolor=[0, 0, 0]) numCorrect = 0 for i in range(0, numItems / 2): itemClass = int(testy[i]) currColor = colors[itemClass, :] prediction = int(knn.Predict(testX[i, :])) if itemClass == prediction: numCorrect = numCorrect + 1 edgeColor = colors[prediction, :] plt.scatter(testX[i, 0], testX[i, 1], facecolor=currColor, s=50, lw=2, edgecolor=edgeColor) print(numCorrect) print(str((float(numCorrect) / float(numItems / 2)) * 100.0) + '%') # plt.scatter(x, y, c=trainy) # plt.scatter(xTest, yTest, c=trainy) plt.show()
print(testX.shape, testy.shape) count0 = 0 count1 = 0 count2 = 0 count3 = 0 count4 = 0 count5 = 0 count6 = 0 count7 = 0 count8 = 0 count9 = 0 for row in range(0, 15000): # normally 0:20000 itemClass = int(testy[row]) prediction = int(knn.Predict(testX[row, :])) actualClass = testy[row] print(prediction, itemClass) if actualClass == prediction: counter += 1 if actualClass == 0: count0 = count0 + 1 if actualClass == 1: count1 = count1 + 1 if actualClass == 2: count2 = count2 + 1 if actualClass == 3: count3 = count3 + 1 if actualClass == 4: count4 = count4 + 1 if actualClass == 5:
x = knn.data[:, 0] y = knn.data[:, 1] trainX = knn.data[::2, 1:3] trainy = knn.target[::2] testX = knn.data[1::2, 1:3] testy = knn.target[1::2] knn.Use_K_Of(15) knn.Fit(trainX, trainy) correct = 0 for i in range(0, 75): actualClass = testy[i] prediction = knn.Predict(testX[i, 0:2]) if (actualClass == prediction): correct = correct + 1 print((correct / float(len(testX))) * 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.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])