(1): First save the above code as knn.py
(2): Run it under the Run menu under IDLE and generate the Python module
(3):import KNN(since the KNN module has been generated in the previous step)
(4):knn.classify0 ([0,0],group,labels,3) (discusses which class [0,0] points belong to)
Note: where "0,0" can be changed freely
The coordinates within the "" are the coordinates of the point we want to judge:
>>> knn.classify0 ([0,0],group,labels,3)
B
>>> knn.classify0 ([0,1],group,labels,3)
B
>>> knn.classify0 ([0.6,0.6],group,labels,3)
A
The above step is missing a step: the corresponding variable in the KNN template is not generated, that is, the variable is simply declared in the template and not actually generated when the shell interacts, so the following shell command is modified:
(1): First save the above code as knn.py
(2): Run it under the Run menu under IDLE and generate the Python module
(3):import KNN(since the KNN module has been generated in the previous step)
>>> Group,labels=knn.createdataset ()
>>> Group
Array ([[1., 1.1],
[1., 1.],
[0., 0.],
[0., 0.1]])
>>> Labels
[' A ', ' a ', ' B ', ' B ']
>>> knn.classify0 ([0,0],group,labels,3)
B
>>>
(4): knn.classify0 ([0,0],group,labels,3) (discusses which class [0,0] points belong to)
Other unchanged can be, ha haha ...
The KNN algorithm feeling 2