1.算法理論概述
基于MNIST手寫數(shù)字?jǐn)?shù)據(jù)庫識別算法,對比SVM、LDA以及決策樹。首先,我們將介紹MNIST數(shù)據(jù)庫的基本信息和手寫數(shù)字識別的背景,然后分別介紹SVM、LDA和決策樹的基本原理和數(shù)學(xué)模型,并對比它們在手寫數(shù)字識別任務(wù)中的性能。
1.1、MNIST手寫數(shù)字?jǐn)?shù)據(jù)庫
MNIST是一種經(jīng)典的手寫數(shù)字?jǐn)?shù)據(jù)庫,包含60,000張訓(xùn)練圖像和10,000張測試圖像。每張圖像的大小為28x28像素,包含一個手寫數(shù)字0~9。MNIST數(shù)據(jù)集被廣泛應(yīng)用于手寫數(shù)字識別任務(wù)中,是評估圖像識別算法性能的標(biāo)準(zhǔn)數(shù)據(jù)集之一。
2.算法運行軟件版本
MATLAB2022a
3. 算法運行效果圖預(yù)覽
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'); % 對數(shù)據(jù)進行預(yù)處理 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 % 對數(shù)據(jù)進行中心化處理 [m,n] = size(rawData); mn = mean(rawData, 2); X = rawData - repmat(mn, 1, n); A = X/sqrt(n-1); % 對數(shù)據(jù)進行奇異值分解,降維 [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(:)); % 將數(shù)據(jù)和標(biāo)簽轉(zhuǎn)換成合適的格式 xtrain = projection_training; label = labels'; %% SVM分類器訓(xùn)練和分類 proj_test = projection_test; true_label = test_labels; % 訓(xùn)練SVM分類器 Mdl = fitcecoc(xtrain',label); % 對測試數(shù)據(jù)進行分類,并評估分類結(jié)果 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分類結(jié)果混淆矩陣評價",'識別準(zhǔn)確率:',num2str(sucRate)]);
聯(lián)系客服