Created
March 3, 2016 08:02
-
-
Save bangonkali/9f9c1b6d8313e2a414ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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("D:\OpenCV\opencv\sources\samples\data\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]) | |
labels, testData = np.hsplit(test,[1]) | |
# Initiate the kNN, classify, measure accuracy. | |
knn = cv2.ml.KNearest_create() | |
# knn.train(trainData, responses) | |
# knn.train(trainData, responses) | |
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