Skip to content

Instantly share code, notes, and snippets.

@iamaaditya
Created November 23, 2015 04:27
Show Gist options
  • Save iamaaditya/50beb542bcbbeedaada3 to your computer and use it in GitHub Desktop.
Save iamaaditya/50beb542bcbbeedaada3 to your computer and use it in GitHub Desktop.
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