python机器学习sklearn实现识别数字
作者:小唐同学大呆子 时间:2023-08-18 17:57:19
简介
本文主要简述如何通过sklearn模块来进行预测和学习,最后再以图表这种更加直观的方式展现出来
数据集
学习数据
预测数据
数据处理
数据分离
因为我们打开我们的的学习数据集,最后一项是我们的真实数值,看过小唐上一篇的人都知道,老规矩先进行拆分,前面的特征放一块,后面的真实值放一块,同时由于数据没有列名,我们选择使用iloc[]来实现分离
def shuju(tr_path,ts_path,sep='\t'):
train=pd.read_csv(tr_path,sep=sep)
test=pd.read_csv(ts_path,sep=sep)
#特征和结果分离
train_features=train.iloc[:,:-1].values
train_labels=train.iloc[:,-1].values
test_features = test.iloc[:, :-1].values
test_labels = test.iloc[:, -1].values
return train_features,test_features,train_labels,test_labels
训练数据
我们在这里直接使用sklearn函数,通过选择模型,然后直接生成其识别规则
#训练数据
def train_tree(*data):
x_train, x_test, y_train, y_test=data
clf=DecisionTreeClassifier()
clf.fit(x_train,y_train)
print("学习模型预测成绩:{:.4f}".format(clf.score(x_train, y_train)))
print("实际模型预测成绩:{:.4f}".format(clf.score(x_test, y_test)))
#返回学习模型
return clf
数据可视化
为了让我们的观察更加直观,我们还可以使用matplotlib来进行观测
def plot_imafe(test,test_labels,preds):
plt.ion()
plt.show()
for i in range(50):
label,pred=test_labels[i],preds[i]
title='实际值:{},predict{}'.format(label,pred)
img=test[i].reshape(28,28)
plt.imshow(img,cmap="binary")
plt.title(title)
plt.show()
print('done')
结果
完整代码
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
def shuju(tr_path,ts_path,sep='\t'):
train=pd.read_csv(tr_path,sep=sep)
test=pd.read_csv(ts_path,sep=sep)
#特征和结果分离
train_features=train.iloc[:,:-1].values
train_labels=train.iloc[:,-1].values
test_features = test.iloc[:, :-1].values
test_labels = test.iloc[:, -1].values
return train_features,test_features,train_labels,test_labels
#训练数据
def train_tree(*data):
x_train, x_test, y_train, y_test=data
clf=DecisionTreeClassifier()
clf.fit(x_train,y_train)
print("学习模型预测成绩:{:.4f}".format(clf.score(x_train, y_train)))
print("实际模型预测成绩:{:.4f}".format(clf.score(x_test, y_test)))
#返回学习模型
return clf
def plot_imafe(test,test_labels,preds):
plt.ion()
plt.show()
for i in range(50):
label,pred=test_labels[i],preds[i]
title='实际值:{},predict{}'.format(label,pred)
img=test[i].reshape(28,28)
plt.imshow(img,cmap="binary")
plt.title(title)
plt.show()
print('done')
train_features,test_features,train_labels,test_labels=shuju(r"C:\Users\twy\PycharmProjects\1\train_images.csv",r"C:\Users\twy\PycharmProjects\1\test_images.csv")
clf=train_tree(train_features,test_features,train_labels,test_labels)
preds=clf.predict(test_features)
plot_imafe(test_features,test_labels,preds)
来源:https://blog.csdn.net/weixin_52521533/article/details/123802259
标签:python,sklearn,识别数字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用表格来储存数据库的记录?
2010-05-16 15:14:00
用Python定时发送天气邮件
2022-09-22 15:11:31
![](https://img.aspxhome.com/file/2023/4/107184_0s.jpg)
Web 开发中遇到的UTF-8编码的问题总结第1/2页
2023-06-09 05:02:10
CSS3的五个使用技巧[译]
2009-02-19 13:01:00
![](https://img.aspxhome.com/file/UploadPic/20092/19/01-92s.jpg)
浅析python 中__name__ = '__main__' 的作用
2022-07-21 16:32:28
golang使用map支持高并发的方法(1000万次操作14ms)
2024-05-13 10:41:33
python数据预处理 :样本分布不均的解决(过采样和欠采样)
2023-08-10 07:03:14
ASP 三层架构 Error处理类
2011-03-16 11:06:00
《悟透JavaScript》之 甘露模型
2008-06-09 14:03:00
![](https://img.aspxhome.com/file/UploadPic/20086/9/200869142355966s.gif)
跟我学习javascript的闭包
2024-04-23 09:11:51
![](https://img.aspxhome.com/file/2023/6/135676_0s.png)
使用python向MongoDB插入时间字段的操作
2021-05-21 06:54:19
SQL Server数据转换服务基本概念介绍
2009-01-20 15:52:00
SQL对数据进行按月统计或对数据进行按星期统计的实例代码
2024-01-28 08:41:47
![](https://img.aspxhome.com/file/2023/8/116288_0s.png)
python中子类继承父类的__init__方法实例
2022-05-27 19:37:21
python中if和elif的区别介绍
2022-07-23 14:22:10
JSP EL表达式详细介绍
2023-07-02 22:32:32
![](https://img.aspxhome.com/file/2023/9/97899_0s.jpg)
详解如何用模块化的方式写vuejs
2024-04-27 16:08:04
通过Turtle库在Python中绘制一个鼠年福鼠
2021-03-01 03:48:12
![](https://img.aspxhome.com/file/2023/2/69462_0s.gif)
利用Python删除电脑中重复文件的方法
2023-06-19 21:58:20
![](https://img.aspxhome.com/file/2023/6/118646_0s.webp)
MySQL 创建用户、授权用户、撤销用户权限、更改用户密码、删除用户(实用技巧)
2024-01-25 06:11:46