Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize K nearest neighbor by python

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how python realizes K nearest neighbor". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve K nearest neighbor by python".

Background introduction

It can be used for classification and regression problems. However, it is more widely used for classification problems in the industry. K nearest neighbors is a simple algorithm that stores all available cases and classifies new cases by the majority of their k neighbors. Of the K nearest neighbors measured by the distance function, the cases assigned to this category are the most common.

These distance functions can be Euclidean distance, Manhattan distance, Minkowski distance and hamming distance. The first three functions are used for continuous functions, and the fourth function is used to classify variables. If K = 1, the case is simply assigned to the category of its nearest neighbor. Sometimes choosing K is a real challenge when performing kNN modeling.

KNN can be easily mapped to our real life. If you want to know a person who has no information, you may want to know his close friend and the circle he enters and get his / her information!

Things to consider before choosing kNN:

KNN is computationally expensive

Variables should be normalized, otherwise a wide range of variables may produce deviation

More work is done during the preprocessing phase before kNN processing (such as outliers, noise cancellation)

Let's take a look at an example of using Python:

# importing required librariesimport pandas as pdfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn.metrics import accuracy_score

Train_data = pd.read_csv ('train-data.csv') test_data = pd.read_csv (' test-data.csv')

Print ('Shape of training data:', train_data.shape) print ('Shape of testing data:', test_data.shape)

Train_x = train_data.drop (columns= ['Survived'], axis=1) train_y = train_data [' Survived']

Test_x = test_data.drop (columns= ['Survived'], axis=1) test_y = test_data [' Survived']

'' sklearn K-Neighbors Classifier: https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html''model = KNeighborsClassifier ()

Model.fit (train_x,train_y)

Print ('\ nThe number of neighbors used to predict the target:'\, model.n_neighbors)

Predict_train = model.predict (train_x) print ('\ nTarget on train data',predict_train)

Accuracy_train = accuracy_score (train_y,predict_train) print ('accuracy_score on train dataset:', accuracy_train)

Predict_test = model.predict (test_x) print ('Target on test data',predict_test)

Accuracy_test = accuracy_score (test_y,predict_test) print ('accuracy_score on test dataset:', accuracy_test)

Running result:

Shape of training data: (712,25) Shape of testing data: (179,25)

The number of neighbors used to predict the target: 5

Target on train data [0 1 1 0 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 0 0 0 1 1 1 0 1 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 1 1 1 0 0 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 1 0 0 1 0 0 1 0 0 1 1 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1 1 1 0 0 0 1 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 1 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 0 1 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 1 0 0 1 1 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 1 1 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 0] accuracy_ Score on train dataset: 0.8047752808988764Target on test data [0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 0 0 Accuracy_score on test dataset: 0.7150837988826816 Thank you for your reading The above is the content of "how python realizes K nearest neighbor". After the study of this article, I believe you have a deeper understanding of how python realizes K nearest neighbor, and the specific usage still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report