【项目实战-MATLAB】:基于SVM的手写数字识别

我看很多博主都是很老的MATLAB版本了,这里用了高版本的函数fitcecoc进行SVM多分类

下载链接https://download.csdn.net/download/qq_45047246/63960845?spm=1001.2014.3001.5503

clc;
clear all;

% load data
x_train=LoadMNISTImages('train-images-idx3-ubyte');
x_test=LoadMNISTImages('t10k-images-idx3-ubyte');
y_train=LoadMNISTLabels('train-labels-idx1-ubyte');
y_test=LoadMNISTLabels('t10k-labels-idx1-ubyte');
 
% After transposing, each image becomes a number * pixel matrix
x_train=x_train';
x_test=x_test';
%% SVM
t = templateSVM('Standardize',true)
SVMMdl = fitcecoc(x_train,y_train,'Learners',t,...
    'ClassNames',{'0','1','2','3','4','5','6','7','8','9'});
predict_label = predict(SVMMdl,x_test);

s=0;
for i=1:length(predict_label)
    if str2double(predict_label(i))==y_test(i)
        s=s+1;
    end
end
acc=s/length(predict_label)
figure
plot(str2double(predict_label),'b*')
hold on;
plot(y_test,'ro')
hold on;
legend('predict_label','real_label')
title('SVM Model')
figure
confusionchart(str2double(predict_label)',y_test')
title('SVM Model Confusion matrix')

参考其他博主的加载数据的函数

function labels = LoadMNISTLabels(filename)
% read label

fp = fopen(filename,'rb');% Read files in binary mode
assert(fp~=-1,['Could not open',filename,'']); 
magic = fread(fp,1,'int32',0,'ieee-be');% Read data from binary file
assert(magic==2049,['Bad magic number in',filename,'']);

 
numlabels = fread(fp,1,'int32',0,'ieee-be');% Number of labels obtained
 
labels = fread(fp,inf,'unsigned char');
 
assert(size(labels,1)==numlabels,'Mismatch in label count');

fclose(fp);% Close file
 
end
 
function images = LoadMNISTImages(filename)
% Read image data in data

 
fp = fopen(filename,'rb');% Read files in binary mode
assert(fp~=-1,['Could not open',filename,'']);
% assertFunction to determine whether the conditions are met. If the conditions are not 
% met, an error message is reported
 
magic = fread(fp,1,'int32',0,'ieee-be');% Read data from binary file

assert(magic==2051,['Bad magic number in',filename,'']);
%In MNIST image dataset, if the magic value is 2051, an error will be reported if it is 
% not 2051
 
numimages = fread(fp,1,'int32',0,'ieee-be');% Number of images obtained
numrows = fread(fp,1,'int32',0,'ieee-be');% row
numcols = fread(fp,1,'int32',0,'ieee-be');% column
 
images = fread(fp,inf,'unsigned char');
% Read all the remaining pixel data with the precision of "unsigned char" and store it in % the column vector images
images = reshape(images,numcols,numrows, numimages);

images = permute(images,[2 1 3]);
% Rearrange the dimensions of the previous three-dimensional array to obtain a three-
% dimensional matrix composed of all image data with 28 * 28 * number of images
 
fclose(fp);% Close file
 
images = reshape(images,size(images,1)*size(images,2),size(images,3));
% Convert to two-dimensional vector
images = double(images/255);
% convert to double and rescale to [0,1] 
end
 
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MATLAB提供了一个内置的svm分类器工具箱用于手写数字识别。下面是一个简单的例子,展示了如何使用svm分类器来识别手写数字。 1. 加载数据集 首先,我们需要加载手写数字数据集。在MATLAB中,可以使用`load`函数来加载数据集。数据集通常包含训练数据和测试数据。以下是加载手写数字数据集的代码: ``` load('digitDataset.mat'); ``` 2. 准备数据 接下来,我们需要将数据集转换为适合svm分类器的格式。在这个例子中,我们将使用`extractHOGFeatures`函数来提取图像的HOG特征。HOG特征可以用于描述图像的形状和纹理信息。 ``` % 提取HOG特征 trainingFeatures = zeros(size(trainingImages,1),4680); for i = 1:size(trainingImages,1) trainingFeatures(i, :) = extractHOGFeatures(trainingImages{i}); end % 将标签转换为分类器需要的格式 trainingLabels = categorical(trainingLabels); ``` 3. 训练svm分类器 现在,我们已经准备好了训练数据,接下来我们需要训练svm分类器。在MATLAB中,可以使用`fitcecoc`函数来训练svm分类器。以下是训练分类器的代码: ``` % 训练svm分类器 classifier = fitcecoc(trainingFeatures, trainingLabels); ``` 4. 测试分类器 训练完成后,我们需要测试分类器的性能。在这个例子中,我们将使用测试数据集来测试分类器的准确性。以下是测试分类器的代码: ``` % 用测试集测试分类器性能 testFeatures = zeros(size(testImages,1),4680); for i = 1:size(testImages,1) testFeatures(i, :) = extractHOGFeatures(testImages{i}); end testLabels = categorical(testLabels); predictedLabels = predict(classifier, testFeatures); accuracy = sum(predictedLabels == testLabels)/numel(testLabels); ``` 5. 结果分析 最后,我们可以分析分类器的结果。以下是输出分类器准确性的代码: ``` % 输出分类器的准确性 fprintf('分类器准确率为 %.2f%%\n', accuracy * 100); ``` 通过这个简单的例子,你可以看到如何使用MATLAB内置的svm分类器工具箱来识别手写数字。如果你想了解更多关于svm分类器的信息,可以查看MATLAB的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大桃子技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值