基于mnist手写数字数据库识别算法matlab仿真,对比SVM,LDA以及决策树

发布时间 2023-07-23 22:27:07作者: 简简单单做算法

1.算法理论概述

       基于MNIST手写数字数据库识别算法,对比SVM、LDA以及决策树。首先,我们将介绍MNIST数据库的基本信息和手写数字识别的背景,然后分别介绍SVM、LDA和决策树的基本原理和数学模型,并对比它们在手写数字识别任务中的性能。

 

1.1、MNIST手写数字数据库

       MNIST是一种经典的手写数字数据库,包含60,000张训练图像和10,000张测试图像。每张图像的大小为28x28像素,包含一个手写数字0~9。MNIST数据集被广泛应用于手写数字识别任务中,是评估图像识别算法性能的标准数据集之一。

 

 

 

 

 

2.算法运行软件版本

MATLAB2022a

 3.算法运行效果图预览

 

 

 

 

 

 

4.部分核心程序

[images, labels]           = func_mnist_read('MNIST\train-images.idx3-ubyte', 'MNIST\train-labels.idx1-ubyte');
[test_images, test_labels] = func_mnist_read('MNIST\t10k-images.idx3-ubyte', 'MNIST\t10k-labels.idx1-ubyte');
% 对数据进行预处理
images                     = im2double(images);
[m,n,k]                    = size(images);
 
for i = 1:k
    rawData(:,i) = reshape(images(:,:,i), m*n,1);
end 
 
test_images = im2double(test_images);
[m,n,k]     = size(test_images);
 
for i = 1:k
    testData(:,i) = reshape(test_images(:,:,i), m*n,1);
end 
 
% PCA Projection
 % 对数据进行中心化处理
 
[m,n]   = size(rawData);
mn      = mean(rawData, 2);
X       = rawData - repmat(mn, 1, n);
A       = X/sqrt(n-1);
% 对数据进行奇异值分解,降维
[U,S,V] = svd(A,'econ');
 
projection_training = U(:, 1:154)'*X;
projection_training = projection_training./max(S(:));
[m, n] = size(testData);
test_avg = testData - repmat(mn, 1, n);
projection_test = U(:, 1:154)'*test_avg;
projection_test = projection_test./max(S(:));
 
% 将数据和标签转换成合适的格式
xtrain     = projection_training;
label      = labels';
%% SVM分类器训练和分类
proj_test  = projection_test;
true_label = test_labels;
% 训练SVM分类器
Mdl = fitcecoc(xtrain',label);
 
% 对测试数据进行分类,并评估分类结果
testlabels = predict(Mdl,proj_test');
testNum = size(testlabels,1);
err = abs(testlabels - true_label);
err = err > 0;
errNum = sum(err);
sucRate = 1 - errNum/testNum
% 显示混淆矩阵
confusionchart(true_label, testlabels);
title(["SVM分类结果混淆矩阵评价",'识别准确率:',num2str(sucRate)]);