Tuesday, September 01, 2015

K-Nearest Neighbour in Python with OpenCV 3.0

Problem:

K-Nearest Neighbour is a machine learning classification algorithm. In this program we are having 5 individual test data (Green colour) that we need to classify as either Red or Blue group member. Here we are using 51 (odd number) randomly generated training data that will either become Red or Blue group member, which will decide whether the test data will become Red or Blue group member. Here the length of k is 9 which indicated 9 nearest neighbours will decide fate of the test data.

Code:

import cv2
import numpy as np
import matplotlib.pyplot as plt

trainData = np.random.randint(0,100,(51,2)).astype(np.float32)
responses = np.random.randint(0,2,(51,1)).astype(np.float32)

red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^')
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s')


newcomer = np.random.randint(0,100,(5,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o')

knn = cv2.ml.KNearest_create()
knn.train(trainData,cv2.ml.ROW_SAMPLE,responses)
ret, results, neighbours, dist = knn.findNearest(newcomer, 9)

print "results: ", results,"\n"
print "neighbours: ", neighbours,"\n"
print "distances: ", dist

plt.show()

Output:


Reference: