NCU-机器学习-作业3:基于SVM的手写字识别

任务描述:

手写数字识别是生活中尤其常见的机器学习任务,给出一份手写数字训练数据集,训练一个SVM模型并对测试集进行手写数字识别。

输入数据:

在train/目录下包含多个txt文件,其中每个文件表示一个用01矩阵表示的手写数字,文件名中下划线前面的数字代表手写数字的值(如2_167.txt表示手写数字为2;3_13.txt表示手写数字为3,训练数据集可在教学资料中下载,文件名为svm_train.tar)。

在test/目录下也包含多个txt文件,只不过test文件夹下面的txt文件无法从文件名得知手写数字的值(文件名:0.txt~945.txt),需要根据训练好的模型进行预测。

输出数据:

程序需要生成一个result.csv文件,用于保存程序对test中各个txt文件中手写数字值的预测结果。第一行固定为num,之后每一行为一个数值,代表预测值,表示程序对test中对应txt文件的预测结果。

评价标准:

测试集上的准确率。

输入样例:

复制代码
00000000000000000011110000000000
00000000000000001111111100000000
00000000001000111111111100000000
00000000011111111111111110000000
00000000111111111111111110000000
00000000111111111111111110000000
00000000111111111111111110000000
00000000111111111111111111000000
00000001111111111101111111000000
00000000111111000000001111000000
00000001111110000000011111000000
00000001111100000000011111000000
00000001111100000000011111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000001111000000
00000001111100000000011111000000
00000000111100000000011111000000
00000000011110000000011111000000
00000000111100000001111110000000
00000000111110000111111000000000
00000000111111111111111000000000
00000000011111111111111000000000
00000000011111111111111000000000
00000000011111111111110000000000
00000000001111111111110000000000
00000000000111111111000000000000
00000000000000111100000000000000

输出样例:

复制代码
num
0
1
2
3
4

思路代码:

Tips:仅为样例代码,存在可优化部分。

python 复制代码
import os

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import numpy as np


def get_dataset(path, need_label=True):
    dataset, labels = [], []
    filenames = os.listdir(path)

    for filename in filenames:
        if need_label:
            labels.append(filename[0])
        filepath = os.path.join(path, filename)
        dataset.append(np.fromfile(filepath, dtype=np.uint8))

    if need_label:
        return dataset, labels
    return dataset


if __name__ == '__main__':
    X_train, y_train = get_dataset("train")
    X_test = get_dataset("test", need_label=False)

    # 数据标准化
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)  # 使用同一个scaler的transform,避免误差
    y_train = list(y_train)

    model = SVC()
    model.fit(X_train, y_train)

    y_pred_test = model.predict(X_test)

    # 保存预测结果到result.csv
    results = pd.DataFrame({'num': y_pred_test})
    results.to_csv('result.csv', index=False)

答案提交:

提交.py文件即可。

相关推荐
智码看视界3 分钟前
Edge AI 全栈1:ESP32 传感器采集到云端大模型推理的完整链路
人工智能·物联网·嵌入式·esp32·aiot·全栈开发·edgeai
MartinYeung55 分钟前
Uniswap v4 许可池:重塑 DeFi 流动性管理的范式革命
人工智能·区块链
自由能燃气设备7 分钟前
燃气热水锅炉/全预混低氮冷凝锅炉哪个品牌好?哪家好?6大商用品牌横评
大数据·数据库·人工智能
智购科技智能售货柜10 分钟前
2026自动售货机设备能耗计量系统:从功率监测到能效分析的工程实践~YH
数据库·人工智能·redis·缓存·架构·perl·symfony
ShineWinsu16 分钟前
人工智能基础概念全景解析:从 AI 到 Transformer、LLM、Prompt、Token、RAG、Agent、对齐与安全
人工智能·prompt·transformer
Bamtone202517 分钟前
Bamtone班通:TDR阻抗测试仪如何高效助力AI服务器高速信号完整性验证?
服务器·人工智能·阻抗测试·阻抗分析
必须会一定会39 分钟前
AI 编程工具选型:Cursor、Claude Code、Codex、OpenCode 与 DeepSeek Harness 的产品形态和成本边界
人工智能·ai编程
tqs_123451 小时前
大模型工程化高频踩坑总结:RAG、Agent、检索、模型优化全复盘
人工智能
chunmiao30321 小时前
NVIDIA Vera CPU 交付 AWS:为智能体设计的 CPU 进入规模化出货
人工智能
十三画者1 小时前
【文献分享】PANORAMIC:用于空间组学共定位分析的分层不确定性传播框架
人工智能·深度学习·数据挖掘·数据分析·集成学习