使用mlp算法对Digits数据集进行分类

程序功能

这个程序使用多层感知机(MLP)对 Digits 数据集进行分类。程序将数据集分为训练集和测试集,创建并训练一个具有两个隐藏层的 MLP 模型。训练完成后,模型对测试数据进行预测,并通过准确率、分类报告和混淆矩阵评估模型的效果。这些评估指标帮助了解模型在手写数字分类任务上的表现。

代码

python 复制代码
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# 加载Digits数据集
digits = load_digits()

# 分割数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.3, random_state=42)

# 创建多层感知机分类器
mlp = MLPClassifier(hidden_layer_sizes=(100, 100), max_iter=300, activation='relu', solver='adam', random_state=1)

# 训练模型
mlp.fit(X_train, y_train)

# 对测试集进行预测
y_pred = mlp.predict(X_test)

# 计算并输出准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")

# 输出分类报告
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

# 输出混淆矩阵
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
相关推荐
宝杰X734 分钟前
Compose Multiplatform+Kotlin Multiplatfrom 第七弹跨平台 AI开源
人工智能·开源·kotlin
Java樱木35 分钟前
AI 编程 Trae ,有重大更新!用 Trae 做了个图书借阅网站!
人工智能·ai编程
悟乙己37 分钟前
大型语言模型(LLM)文本中提取结构化信息:LangExtract(一)
人工智能·语言模型·自然语言处理
Theodore_102238 分钟前
机器学习(3)梯度下降
人工智能·机器学习
LiJieNiub2 小时前
YOLOv3:目标检测领域的经典革新
人工智能·计算机视觉·目标跟踪
yanxing.D2 小时前
OpenCV轻松入门_面向python(第六章 阈值处理)
人工智能·python·opencv·计算机视觉
霍格沃兹测试开发学社测试人社区3 小时前
新手指南:通过 Playwright MCP Server 为 AI Agent 实现浏览器自动化能力
运维·人工智能·自动化
JJJJ_iii3 小时前
【机器学习01】监督学习、无监督学习、线性回归、代价函数
人工智能·笔记·python·学习·机器学习·jupyter·线性回归
qq_416276425 小时前
LOFAR物理频谱特征提取及实现
人工智能
余俊晖6 小时前
如何构造一个文档解析的多模态大模型?MinerU2.5架构、数据、训练方法
人工智能·文档解析