当前位置: 首页 > news >正文

python机器学习(手写数字识别)

# 导包
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import joblib
from collections import Counter


# 1. 定义函数 show_digit(idx), 用于查看: 数字图片.
def show_digit(idx):
    # idx: 行索引, 即: 要哪行的数据.
    # 1. 读取数据, 获取df对象.
    data = pd.read_csv('data/手写数字识别.csv')
    # 细节: 非法值校验.
    if idx < 0 or idx > len(data) - 1 :
        return
    # 2. 获取数据, 即: 特征 + 标签
    x = data.iloc[:, 1:]
    y = data.iloc[:, 0]
    # 3. 查看下数据集.
    print(f'x的维度: {x.shape}')           # (42000, 784)
    print(f'y的各分类数量: {Counter(y)}')   # Counter({1: 4684, 7: 4401, 3: 4351, 9: 4188, 2: 4177, 6: 4137, 0: 4132, 4: 4072, 8: 4063, 5: 3795})

    # 4. 获取具体的 某张图片, 即: 某行数据 => 样本数据.
    # step1: 把图片转成 28 * 28像素(二维数组)
    digit = x.iloc[idx].values.reshape(28, 28)
    # step2: 显示图片.
    plt.imshow(digit, cmap='gray')      # 灰色显示 => 灰度图
    # step3: 取消坐标显示.
    plt.axis('off')
    # step4: 显示图片
    plt.show()

# 2. 定义函数 train_model(), 用于训练模型.
def train_model():
    # 1. 读取数据, 获取df对象.
    data = pd.read_csv('data/手写数字识别.csv')
    # 2. 获取数据, 即: 特征 + 标签
    x = data.iloc[:, 1:]
    y = data.iloc[:, 0]
    # 3. 数据的预处理.
    # step1: x轴(像素点)的 归一化处理.
    x = x / 255
    # step2: 区分训练集和测试集.       stratify: 按照y的类别比例进行分割
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, stratify=y, random_state=21)
    # 4. 训练模型.
    estimator = KNeighborsClassifier(n_neighbors=3)
    estimator.fit(x_train, y_train)
    # 5. 模型预估, 评测正确率.
    my_score = estimator.score(x_test, y_test)
    print(f'模型预估正确率: {my_score}')       # 0.9657142857142857
    # 6. 保存模型.
    joblib.dump(estimator, 'model/knn.pth')


# 3. 定义use_model()函数, 用于: 测试模型.
def use_model():       # pytest
    # 1. 加载图片.
    img = plt.imread('data/demo.png')       # 28 * 28像素
    plt.imshow(img, cmap='gray')    # 灰度图
    plt.show()

    # 2. 加载模型.
    estimator = joblib.load('model/knn.pth')

    # 3. 预测图片.
    img = img.reshape(1, -1)    # 效果等价于: reshape(1, 784)
    y_test = estimator.predict(img)
    print(f'预测的数字是: {y_test}')

# 4. 在main函数中测试.
if __name__ == '__main__':
    # 显示数字图片.
    # show_digit(10)
    # show_digit(21)

    # 训练模型
    # train_model()

    # 测试模型
    use_model()

http://www.lryc.cn/news/460496.html

相关文章:

  • 如何针对项目中的技术难点准备面试?——黑马点评为例
  • ARP欺骗的多种手法
  • HCIA——one
  • 【vue】⾃定义指令+插槽+商品列表案例
  • 多线程——线程的等待通知
  • 模态与非模态的对话框
  • C语言练习
  • CyberRt实践之Hello Apollo(Apollo 9.0版本)
  • 【JavaScript】LeetCode:61-65
  • 【SpringAI】(一)从实际场景入门大模型——适合Java宝宝的大模型应用开发
  • 植物大战僵尸杂交版
  • live2d 实时虚拟数字人形象页面显示,对接大模型
  • SpringCloud-持久层框架MyBatis Plus的使用与原理详解
  • Servlet的HttpServletRequest
  • U9销售订单不能带出最新价格出来
  • Jmeter接口测试企业级项目实战day1
  • 接口测试面试题含答案
  • 横板营业执照提取生成
  • webm格式怎么转换成mp4?这5种转换方法很好用
  • C/C++语言基础--C++异常看这一篇就够了
  • DFT ATPG中常见影响coverage的因素有哪些?
  • Python机器学习数据清洗到特征工程策略
  • 多线程-进阶(2)CountDownLatchConcurrentHashMapSemaphore
  • 密码管理器KeePass的安装及使用
  • 星海智算:【萤火遛AI-Stable-Diffusion】无需部署一键启动
  • JS生成器的特殊用法:委托yield*
  • 【CuPy报错】NVRTC_ERROR_COMPILATION (6)找不到 ‘vector_types.h‘
  • 机器学习:知识蒸馏(Knowledge Distillation,KD)
  • 【C++入门篇 - 3】:从C到C++第二篇
  • YOLOv8模型改进 第七讲 一种新颖的注意力机制 Outlook Attention