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)
相关文章
|
2天前
|
机器学习/深度学习 算法 数据挖掘
机器学习之sklearn基础——一个小案例,sklearn初体验
机器学习之sklearn基础——一个小案例,sklearn初体验
24 6
|
3天前
|
机器学习/深度学习 数据采集 算法
【机器学习】Scikit-Learn:Python机器学习的瑞士军刀
【机器学习】Scikit-Learn:Python机器学习的瑞士军刀
15 3
|
3天前
|
机器学习/深度学习 机器人 Python
实践指南,终于有大佬把Python和机器学习讲明白了!
机器学习正在迅速成为数据驱动型世界的一个必备模块。许多不同的领域,如机器人、医学、零售和出版等,都需要依赖这门技术。 机器学习是近年来渐趋热门的一个领域,同时 Python 语言经过一段时间的发展也已逐渐成为主流的编程语言之一。今天给小伙伴们分享的这份手册结合了机器学习和 Python 语言两个热门的领域,通过易于理解的项目详细讲述了如何构建真实的机器学习应用程序。
|
5天前
|
机器学习/深度学习 人工智能 监控
【机器学习】Python与深度学习的完美结合——深度学习在医学影像诊断中的惊人表现
【机器学习】Python与深度学习的完美结合——深度学习在医学影像诊断中的惊人表现
20 3
|
7天前
|
机器学习/深度学习 人工智能 TensorFlow
机器学习项目实战:使用Python实现图像识别
在AI时代,Python借助TensorFlow和Keras实现图像识别,尤其在监控、驾驶、医疗等领域有广泛应用。本文通过构建CNN模型识别MNIST手写数字,展示图像识别流程:安装库→加载预处理数据→构建模型→训练→评估。简单项目为深度学习入门提供基础,为进一步探索复杂场景打下基础。
24 5
|
1天前
|
机器学习/深度学习 数据处理 计算机视觉
机器学习- Sklearn (交叉验证和Pipeline)
机器学习- Sklearn (交叉验证和Pipeline)
|
9天前
|
机器学习/深度学习 编解码 文字识别
【开源】轻松实现车牌检测与识别:yolov8+paddleocr【python源码+数据集】
【开源】轻松实现车牌检测与识别:yolov8+paddleocr【python源码+数据集】
|
9天前
|
机器学习/深度学习 存储 监控
基于YOLOv8深度学习的120种犬类检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、狗类检测、犬种识别
基于YOLOv8深度学习的120种犬类检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战、狗类检测、犬种识别
|
1天前
|
机器学习/深度学习 人工智能 Dart
AI - 机器学习GBDT算法
梯度提升决策树(Gradient Boosting Decision Tree),是一种集成学习的算法,它通过构建多个决策树来逐步修正之前模型的错误,从而提升模型整体的预测性能。
|
2天前
|
机器学习/深度学习 算法 数据挖掘
机器学习与智能优化——利用简单遗传算法优化FCM
机器学习与智能优化——利用简单遗传算法优化FCM
17 5