Understanding k-Nearest Neighbour
我们将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()
输出:
result: [[1.]]
neighbours: [[1. 1. 0.]]
distance: [[ 29. 149. 160.]]
上面返回的是:
- newcomer的标签,如果最近邻算法,k=1
- k-Nearest Neighbors的标签
- 从newcomer到每个最近邻居的相应距离
如果newcomer有大量数据,则可以将其作为数组传递,相应的结果也作为矩阵获得.
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()
输出:
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.]]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。