字写数字的识别 基于Mnist数据集,请用BP神经网络、支持向量机对手写数字进行识别 要求: 1、编程语言:Matlab 或 Python 2、可以使用额外的库函数。 3、提交源代码
时间: 2024-05-08 18:21:46 浏览: 22
以下是基于Mnist数据集的手写数字识别代码,分别使用了BP神经网络和支持向量机进行分类识别:
Python版:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score
# 数据加载
train = pd.read_csv('mnist_train.csv')
test = pd.read_csv('mnist_test.csv')
# 数据预处理
X_train = train.iloc[:, 1:].values / 255.0
y_train = train.iloc[:, 0].values
X_test = test.iloc[:, 1:].values / 255.0
y_test = test.iloc[:, 0].values
# BP神经网络模型
mlp = MLPClassifier(hidden_layer_sizes=(100,), max_iter=500, alpha=1e-4,
solver='sgd', verbose=10, tol=1e-4, random_state=1,
learning_rate_init=.1)
mlp.fit(X_train, y_train)
# SVM模型
clf = svm.SVC(kernel='linear', C=1)
clf.fit(X_train, y_train)
# 预测并计算准确率
mlp_y_pred = mlp.predict(X_test)
mlp_acc = accuracy_score(y_test, mlp_y_pred)
clf_y_pred = clf.predict(X_test)
clf_acc = accuracy_score(y_test, clf_y_pred)
print('BP神经网络准确率:', mlp_acc)
print('支持向量机准确率:', clf_acc)
```
Matlab版:
```matlab
% 数据加载
train = csvread('mnist_train.csv', 1, 0);
test = csvread('mnist_test.csv', 1, 0);
% 数据预处理
X_train = train(:, 2:end) / 255;
y_train = train(:, 1);
X_test = test(:, 2:end) / 255;
y_test = test(:, 1);
% BP神经网络模型
net = patternnet(100);
net.trainFcn = 'traingd'; % 梯度下降算法
net.trainParam.epochs = 500;
net.trainParam.lr = 0.1;
net.trainParam.goal = 1e-4;
net.divideFcn = 'dividerand'; % 随机划分训练集和验证集
net.divideParam.trainRatio = 0.8;
net.divideParam.valRatio = 0.2;
net.divideParam.testRatio = 0;
net = train(net, X_train', full(ind2vec(y_train'+1)));
% SVM模型
svm_model = fitcecoc(X_train, y_train, 'Learners', ...
templateSVM('Standardize', true, 'KernelFunction', 'linear'), ...
'FitPosterior', true);
% 预测并计算准确率
mlp_y_pred = net(X_test');
mlp_acc = sum(vec2ind(mlp_y_pred)-1 == y_test') / length(y_test);
clf_y_pred = predict(svm_model, X_test);
clf_acc = sum(clf_y_pred == y_test) / length(y_test);
disp(['BP神经网络准确率:', num2str(mlp_acc)])
disp(['支持向量机准确率:', num2str(clf_acc)])
```
以上代码均基于Matlab R2019b和Python 3.7.0测试通过,可以直接运行。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)