使用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))
相关推荐
Wireless_wifi62 小时前
Why Choose IPQ9574 for Your WiFi 7 Solution
linux·人工智能·5g
Ar-Sr-Na2 小时前
工作路演PPT处理,交给Workbuddy
人工智能·powerpoint·workbuddy开发者分享季
阿洛学长2 小时前
Cursor下载安装使用教程(最新详细图文)
人工智能·gpt·深度学习·ai·ai编程
2603_955279702 小时前
城市与雨水的隐秘对话
人工智能
renhongxia12 小时前
原生多模态对应用架构的重塑
人工智能·深度学习·机器学习·自然语言处理·架构·机器人
俊哥V2 小时前
每日 AI 研究简报 · 2026-07-03
人工智能·ai
Nomarsgo2 小时前
工控机蓝屏怎么办?
人工智能·科技·计算机视觉·视觉检测·电脑
金融小师妹2 小时前
人工智能推演框架:非农降温信号如何重构黄金定价模型
数据结构·人工智能·机器学习·transformer
HavenlonLabs2 小时前
Havenlon 思考录(十一):系统的冷,不是对人,而是对风险
人工智能·安全威胁分析·安全架构·havenlon
林澈在路上2 小时前
最新版权清晰 AI音乐写歌工具软件App推荐 商用全场景实测指南
数据库·人工智能·ai·aigc·音频