__xin__
__xiin__
用心*Blog
机器学习入门 - K-近邻对load_iris进行分类
机器学习入门 - K-近邻对load_iris进行分类

分类算法 : K-近邻

 

前几天终于开始正式入门 机器学习  !!!!

于是一晚上跟着淘来的教学视频今行了简单的入门。

于是火急火燎的学习了最简单的一个算法

 

数据 :

from sklearn.datasets import load_iris

iris = load_iris()

x = iris.data
y = iris.target
获取一下sklearn里自带的数据,以这为数据集。

由于k-近邻对数据要求同等重要的特征区别不能大,所以嘛!
from sklearn.preprocessing import StandardScaler
导入标准化的包对特征值标准化

std = StandardScaler()

x = std.fit_transform(x)

下面就需要对数据集区分训练和测试集了

from sklearn.model_selection import train_test_split

(一个库能解决的绝不手写)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
x_trian :训练集特征值
x_test : 测试集特征值
y_trian : 训练集目标值
y_test : 测试集目标值

最重要的就是对数据计算了!!!!

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=5)
n_neighbors: 离计算值最近的几个计算结果

knn.fit(x_train, y_train)
把训练集传fit下,

predict = knn.predict(x_test)
再把测试集传入一下得到预测结果

score = knn.score(x_test, y_test)
这里可以传入一下测试集得到评分。
具体我还没1查查的到是精确率还是召回率


print("预测结果:",y_predict)
print("测试集对比:", y_test)
print("测试评分:", score)
打印一下结果::

image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnwAAABlCAYAAADXqz4sAAAayElEQVR4nO3da1Mb15ov8P5oq7t1RxcQYAxICCGEhBAXCYQFxAgQF3NzsIltMDa+Ad4Qb05wtlNz9syuVGW2Mzlnp5KZc4qpXeUP858XCm0UScgt06hJ/V/8qnD306uftbobP/RVEkKAiIiIiP64pHonQERERETGkuqdABEREREZS6p3AkRERERkLKneCRARERGRsaR6J0BERERExpLqnQARERERGUvSu4Asywj2hxBKhhFKhjG1mUNbuF3795m1N/fga22EEALB/hA6ogE0d7agdzSGUDKM3nQfAvEurd38sztF62ntakPfeLxoWv/kAELJcNm8OqIBdA/2l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jKWVO8EiIiIiMhYUr0TICIiIiJjSfVOgIiIiIiM9T92Fe85wl0cjgAAAABJRU5ErkJggg==

赞赏
没有标签
首页      机器学习      机器学习入门 - K-近邻对load_iris进行分类
http://2.gravatar.com/avatar/e389e53be21e167c8cb76834520e48b5?s=256&d=mm&r=g

wulei

文章作者

发表评论

textsms
account_circle
email

用心*Blog

机器学习入门 - K-近邻对load_iris进行分类
分类算法 : K-近邻   前几天终于开始正式入门 机器学习  !!!! 于是一晚上跟着淘来的教学视频今行了简单的入门。 于是火急火燎的学习了最简单的一个算法   …
扫描二维码继续阅读
2020-06-06
2020
2020年八月
« 6月    
 12
3456789
10111213141516
17181920212223
24252627282930
31  
分类目录