您好,登录后才能下订单哦!
这篇文章主要讲解了“python怎么实现K最近邻居”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python怎么实现K最近邻居”吧!
它可以用于分类和回归问题。但是,它更广泛地用于行业中的分类问题。K个最近邻居是一种简单的算法,可以存储所有可用案例,并通过其k个邻居的多数票对新案例进行分类。在用距离函数测量的K个最近邻居中,分配给该类别的案例最为常见。
这些距离函数可以是欧几里得距离,曼哈顿距离,明可夫斯基距离和汉明距离。前三个函数用于连续函数,第四个函数用于分类变量。如果K = 1,则将案例简单分配给其最近邻居的类别。有时,执行kNN建模时选择K确实是一个挑战。
KNN可以轻松地映射到我们的现实生活。如果您想了解一个没有信息的人,则可能想了解他的密友和他所进入的圈子并获得他/她的信息!
选择kNN之前要考虑的事项:
KNN在计算上很昂贵
变量应归一化,否则较大范围的变量可能会产生偏差
在进行kNN处理之前(例如离群值,噪声消除),在预处理阶段进行更多工作
下面来看使用Python实现的案例:
# importing required libraries
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from 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)
运行结果:
Shape of training data : (712, 25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0 0 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 1
1 0 0 0 0 1 0 0 0 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 0 0
0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 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 0 1 0 0 0 1
0 0 0 1 0 1 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 1 0 0 0 1 0 1 0 0 0
1 1 0 0 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.8047752808988764
Target on test data [0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 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 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 0 1 1 0 1 1 0 0 1 0 0 1
0 1 0 0 1 0 1 0 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0]
accuracy_score on test dataset : 0.7150837988826816
感谢各位的阅读,以上就是“python怎么实现K最近邻居”的内容了,经过本文的学习后,相信大家对python怎么实现K最近邻居这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。