python机器学习sklearn怎么实现识别数字

发布时间:2022-03-29 15:41:23 作者:iii
来源:亿速云 阅读:183

这篇文章主要介绍了python机器学习sklearn怎么实现识别数字的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python机器学习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')

结果

python机器学习sklearn怎么实现识别数字

python机器学习sklearn怎么实现识别数字

python机器学习sklearn怎么实现识别数字

python机器学习sklearn怎么实现识别数字

完整代码

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)

关于“python机器学习sklearn怎么实现识别数字”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python机器学习sklearn怎么实现识别数字”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 机器学习笔记-模式识别
  2. python opencv实现信用卡的数字识别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python sklearn

上一篇:本地Vue项目请求本地Node.js服务器如何配置

下一篇:怎么用python快速搭建redis集群

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》