K Nearest Neighbor Classification algorithm

Source: Internet
Author: User

1 #-*-coding:utf-8-*-2 """3 Created on Thu June 17:16:19 20184 5 @author: Zhen6 """7  fromSklearn.model_selectionImportTrain_test_split8 ImportMglearn9 ImportMatplotlib.pyplot as PltTenX, y =Mglearn.datasets.make_forge () One X_train, X_test, y_train, y_test = Train_test_split (x, Y, random_state=0) # Generate training and test set data A  -  fromSklearn.neighborsImportKneighborsclassifierCLF = Kneighborsclassifier (n_neighbors=3) # call K nearest Neighbor classification algorithm-clf.fit (X_train, Y_train) # Training Data -  - Print("Test set predictions:{}". Format (clf.predict (x_test)) # Forecast +  - Print("Test set accuracy:{:.2f}". Format (Clf.score (X_test, Y_test))) +  AFig, axes = plt.subplots (1, 3, figsize= (10, 3) # Use Matplotlib to paint at  -  forN_neighbors, AxinchZip ([1, 3, 9], axes): -     #The Fit method returns the object itself, so we can put the instantiation and fitting in one line of code -CLF = Kneighborsclassifier (n_neighbors=n_neighbors). Fit (x, y) -Mglearn.plots.plot_2d_separator (CLF, X, Fill=true, eps=0.5, Ax=ax, alpha=0.4) -Mglearn.discrete_scatter (x[:, 0], x[:, 1], Y, ax=Ax) inAx.set_title ("{} neighbor (s)". Format (n_neighbors)) -Ax.set_xlabel ("feature 0") toAx.set_ylabel ("Feature 1") +Axes[0].legend (loc=3)
Results:

Summary: As you can see, the decision boundaries drawn with a single neighbor follow the training data, and as the neighbors increase, the decision boundaries become smoother and the smoother boundaries correspond to simpler models, in other words, with fewer neighbors that correspond to higher model complexity.

K Nearest Neighbor Classification algorithm

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.