1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| from sklearn import neighbors import warnings warnings.filterwarnings('ignore')
data = pd.DataFrame({ 'fight':[3,2,1,101,99,98], 'kiss':[104,100,81,10,5,2], 'type':['R','R','R','A','A','A'], 'name':['北京遇上西雅图','喜欢你','疯狂动物城','战狼2','力王','敢死队']}) print(data) print('----')
plt.scatter(data[data['type'] == 'R']['fight'],data[data['type'] == 'R']['kiss'],color = 'r',marker = 'o',label = 'R') plt.scatter(data[data['type'] == 'A']['fight'],data[data['type'] == 'A']['kiss'],color = 'r',marker = 'o',label = 'A') plt.grid() plt.legend()
knn = neighbors.KNeighborsClassifier() knn.fit(data[['fight','kiss']],data['type'])
knn.predict([18,90]) plt.scatter(18,90,color = 'r',marker='x',label = 'R') plt.ylabel('kiss') plt.xlabel('fight') plt.text(18,90,'《你的名字》',color='r')
|