The KNN algorithm feeling 2

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.