Understanding k-Nearest Neighbour html
咱们将Red系列标记为Class-0(由0表示),将Blue 系列标记为Class-1(由1表示)。 咱们建立了25个系列或25个训练数据,并将它们标记为0级或1级.在Matplotlib的帮助下绘制它,红色系列显示为红色三角形,蓝色系列显示为蓝色方块.算法
import numpy as np import cv2 import matplotlib.pyplot as plt # Feature set containing (x,y) values of 25 known/training data trainData = np.random.randint(0,100,(25,2)).astype(np.float32) # Labels each one either Red or Blue with numbers 0 and 1 responses = np.random.randint(0,2,(25,1)).astype(np.float32) # Take Red families and plot them red = trainData[responses.ravel()==0] plt.scatter(red[:,0],red[:,1],80,'r','^') # Take Blue families and plot them blue = trainData[responses.ravel()==1] plt.scatter(blue[:,0],blue[:,1],80,'b','s') plt.show()
接下来初始化kNN算法并传递trainData和响应以训练kNN(它构造搜索树).而后咱们将对一个new-comer,并在OpenCV的kNN帮助下将它归类为一个系列.KNN以前,咱们须要了解一下咱们的测试数据(new-comer),数据应该是一个浮点数组,其大小为numberoftestdata×numberoffeatures.而后找到new-comer的最近的邻居并分类.数组
newcomer = np.random.randint(0,100,(1,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, 3) print( "result: {}\n".format(results) ) print( "neighbours: {}\n".format(neighbours) ) print( "distance: {}\n".format(dist) ) plt.show()
输出:dom
result: [[1.]] neighbours: [[1. 1. 0.]] distance: [[ 29. 149. 160.]]
上面返回的是:测试
若是newcomer有大量数据,则能够将其做为数组传递,相应的结果也做为矩阵得到.spa
newcomers = np.random.randint(0,100,(10,2)).astype(np.float32) plt.scatter(newcomers[:,0],newcomers[:,1],80,'g','o') knn = cv2.ml.KNearest_create() knn.train(trainData, cv2.ml.ROW_SAMPLE, responses) ret, results, neighbours ,dist = knn.findNearest(newcomers, 3) print( "result: {}\n".format(results) ) print( "neighbours: {}\n".format(neighbours) ) print( "distance: {}\n".format(dist) ) plt.show()
输出:rest
result: [[1.] [0.] [1.] [0.] [0.] [0.] [0.] [0.] [0.] [0.]] neighbours: [[0. 1. 1.] [0. 0. 0.] [1. 1. 1.] [0. 1. 0.] [1. 0. 0.] [0. 1. 0.] [0. 0. 0.] [0. 1. 0.] [0. 0. 0.] [0. 0. 1.]] distance: [[ 229. 392. 397.] [ 4. 10. 233.] [ 73. 146. 185.] [ 130. 145. 1681.] [ 61. 100. 125.] [ 8. 29. 169.] [ 41. 41. 306.] [ 85. 505. 733.] [ 242. 244. 409.] [ 61. 260. 493.]]