python 机器学习 sklearn——一起识别数字吧

简介: python 机器学习 sklearn——一起识别数字吧

简介

本文主要简述如何通过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)
相关文章
|
10天前
|
机器学习/深度学习 数据可视化 搜索推荐
Python在社交媒体分析中扮演关键角色,借助Pandas、NumPy、Matplotlib等工具处理、可视化数据及进行机器学习。
【7月更文挑战第5天】Python在社交媒体分析中扮演关键角色,借助Pandas、NumPy、Matplotlib等工具处理、可视化数据及进行机器学习。流程包括数据获取、预处理、探索、模型选择、评估与优化,以及结果可视化。示例展示了用户行为、话题趋势和用户画像分析。Python的丰富生态使得社交媒体洞察变得高效。通过学习和实践,可以提升社交媒体分析能力。
25 1
|
6天前
|
机器学习/深度学习 数据采集 算法
Python基于OpenCV和卷积神经网络CNN进行车牌号码识别项目实战
Python基于OpenCV和卷积神经网络CNN进行车牌号码识别项目实战
41 19
|
6天前
|
机器学习/深度学习 监控 算法
Python数据分析与机器学习在金融风控中的应用
Python数据分析与机器学习在金融风控中的应用
31 12
|
7天前
|
机器学习/深度学习 数据采集 搜索推荐
Python数据分析与机器学习在电子商务推荐系统中的应用
Python数据分析与机器学习在电子商务推荐系统中的应用
24 5
|
6天前
|
机器学习/深度学习 数据采集 监控
Python基于BP神经网络算法实现家用热水器用户行为分析与事件识别
Python基于BP神经网络算法实现家用热水器用户行为分析与事件识别
|
7天前
|
机器学习/深度学习 算法 Python
【Python】已完美解决:机器学习填补数值型缺失值时报错)TypeError: init() got an unexpected keyword argument ‘axis’,
【Python】已完美解决:机器学习填补数值型缺失值时报错)TypeError: init() got an unexpected keyword argument ‘axis’,
12 1
|
10天前
|
机器学习/深度学习 算法 文件存储
使用Python实现深度学习模型:神经架构搜索与自动机器学习
【7月更文挑战第5天】 使用Python实现深度学习模型:神经架构搜索与自动机器学习
25 2
|
6天前
|
机器学习/深度学习 算法 Python
【Python】已解决:ModuleNotFoundError: No module named ‘sklearn‘
【Python】已解决:ModuleNotFoundError: No module named ‘sklearn‘
13 0
|
机器学习/深度学习 Python
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略daiding
|
机器学习/深度学习 Python
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略
Python之sklearn2pmml:sklearn2pmml库函数的简介、安装、使用方法之详细攻略