Created
November 23, 2015 04:27
-
-
Save iamaaditya/50beb542bcbbeedaada3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# Load the data, converters convert the letter to a number | |
data= np.loadtxt('letter-recognition.data', dtype= 'float32', delimiter = ',', | |
converters= {0: lambda ch: ord(ch)-ord('A')}) | |
# split the data to two, 10000 each for train and test | |
train, test = np.vsplit(data,2) | |
# split trainData and testData to features and responses | |
responses, trainData = np.hsplit(train,[1]) | |
# responses, trainData = np.hsplit(train,[1]) | |
labels, testData = np.hsplit(test,[1]) | |
knn = cv2.ml.KNearest_create() | |
knn.train(trainData,cv2.ml.ROW_SAMPLE,responses) | |
ret, result, neighbours, dist = knn.findNearest(testData, 5) | |
correct = np.count_nonzero(result == labels) | |
accuracy = correct*100.0/10000 | |
print accuracy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment