KNN算法

经过这三天的学习,我们可以大概总结KNN算法的主要内容,并尝试做出了思维导图。

 利用KNN算法对鸢尾花分类---交叉验证网络搜索

交叉验证:

# 交叉验证
# todo 1.加载数据
from sklearn.datasets import load_iris
iris_data = load_iris()
# todo 2.数据划分
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(iris_data.data, iris_data.target, train_size=0.3, random_state=22)
# todo 3.数据预处理(标准化)
from sklearn.preprocessing import StandardScaler
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# todo 4.实例化模型
from sklearn.neighbors import KNeighborsClassifier
estimator1 = KNeighborsClassifier(n_neighbors=3)
estimator2 = KNeighborsClassifier(n_neighbors=4)
# todo 5.交叉验证
# from sklearn.model_selection import GridSearchCV
# estimator = GridSearchCV(estimator=estimator,param_grid={'n_neighbors':[1,2,3,4,5]},cv=5)
estimator1.fit(x_train, y_train)
estimator2.fit(x_train, y_train)
# print(f'estimator.best_params_:{estimator.best_params_}')
# todo 6.模型训练及评估
# import pandas as pd
# cvresults = pd.DataFrame(estimator.cv_results_)
# cvresults.to_csv(path_or_buf='./cvresults.csv')
score1 = estimator1.score(x_test,y_test)
score2 = estimator2.score(x_test,y_test)
print(f'score-->{score1}')
print(f'score-->{score2}')

运行示例:

乳腺癌良性恶性预测

from sklearn.datasets import load_breast_cancer
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
data = load_breast_cancer()
x = data.data
y = data.target
from sklearn.model_selection import train_test_split
x_tarin, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=22)
transfor = StandardScaler()
x_tarin = transfor.fit_transform(x_tarin)
x_test = transfor.fit_transform(x_test)
#实例化
model = KNeighborsClassifier(n_neighbors=9)
#训练
model.fit(x_tarin, y_train)
#评估
y_pre = model.predict(x_test)
score = accuracy_score(y_test, y_pre)
print(f'score-->{score}')
score2 = model.score(x_test, y_test)
print(f'score2-->{score2}')
#预测
y_result = model.predict(x_test)
score11 = accuracy_score(y_test, y_result)
print(score11)

预测结果

烧脑的一天结束了

但是有以下几个地方我一直运行不出来,这是为什么???

同样的

我在网上也没有查到具体的原因,感谢指导,感谢指导,感谢指导!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值