Python teaches you the simplest machine learning algorithm: KNN, just have it

foreword

Hello! Hello everyone, this is the Demon King~
insert image description here

video tutorial

Python teaches you the simplest machine learning algorithm, just have it

insert image description here

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Handwritten Digit Recognition

from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

# 加载数据
digits = load_digits()
data = digits.data
# 数据探索
# data里面每个元素代表一张图片
print(data[0])
# 查看第一幅图像
print(digits.images[0])
# 第一幅图像代表的数字含义
print(digits.target[0])
# 将第一幅图像显示出来
plt.imshow(digits.images[0])
plt.show()

# 分割数据,将25%的数据作为测试集,其余作为训练集(你也可以指定其他比例的数据作为训练集)
train_x, test_x, train_y, test_y = train_test_split(data, digits.target, test_size=0.25, random_state=33)
print(train_x)

# 创建KNN分类器
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(train_x, train_y)
print("KNN训练集得分: %.4lf" % knn.score(train_x, train_y))
print("KNN测试集得分: %.4lf" % knn.score(test_x, test_y))
# 测试分类效果
print(knn.predict(data))

epilogue

Well, this article of mine ends here!

If you have more suggestions or questions, feel free to comment or private message me! Let's work hard together (ง •_•)ง

Follow the blogger if you like it, or like and comment on my article! ! !

Guess you like

Origin blog.csdn.net/python56123/article/details/124063066