K-近邻算法

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.metrics import accuracy_score
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']


data = pd.read_csv('data.csv')  
datas = np.array(data)
x = np.array(data[['radius_mean','texture_mean']])
data['diagnosis'] = data['diagnosis'].map({'M': 1, 'B': 0})  
y = np.array(data['diagnosis'])

X_train,X_test,y_train,y_test = \
    train_test_split(x, y,test_size = 0.3, random_state = 24)
    
#模型拟合,k值为3    
knn = KNN(n_neighbors = 2)
knn.fit(X_train,y_train)
acc = knn.score(X_test, y_test)
acc1 = knn.score(X_train, y_train)

#模型预测
pred_label = knn.predict(X_test)
print(f'预测结果:{pred_label}')
print(f'真实结果:{y_test}')

#返回测试单位个数的概率估计。
P_x1 = knn.predict_proba([[0.9,.3]])
print(P_x1)

#返回给定测试数据和标签的平均精度。即模型的测试得分。
accuracy = accuracy_score(pred_label, y_test)
print("k-近邻测试集得分:",acc)
print("k-近邻训练集得分:",acc1)
print(f'模型预测精确度:{accuracy}')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值