手写数字识别

Question:

手写数字识别,用机器学习的方法将手写数字正确分类

输入:

mnist数据库(自己到网上下载~)

                 

输出:

分类正确率

Answer:

function images = loadMNISTImages(filename)
%loadMNISTImages returns a 28x28x[number of MNIST images] matrix containing
%the raw MNIST images

fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2051, ['Bad magic number in ', filename, '']);

numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
numCols = fread(fp, 1, 'int32', 0, 'ieee-be');

images = fread(fp, inf, 'unsigned char');
images = reshape(images, numCols, numRows, numImages);
images = permute(images,[2 1 3]);

fclose(fp);

% Reshape to #pixels x #examples
images = reshape(images, size(images, 1) * size(images, 2), size(images, 3));
% Convert to double and rescale to [0,1]
images = double(images) / 255;

end
function labels = loadMNISTLabels(filename)
%loadMNISTLabels returns a [number of MNIST images]x1 matrix containing
%the labels for the MNIST images

fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2049, ['Bad magic number in ', filename, '']);

numLabels = fread(fp, 1, 'int32', 0, 'ieee-be');

labels = fread(fp, inf, 'unsigned char');

assert(size(labels,1) == numLabels, 'Mismatch in label count');

fclose(fp);

end
function Classification
%test_images = textread('C:\Users\Administrator\Desktop\数字媒体技术导论大作业\数字媒体技术作业3\mnist\t10k-images-idx3-ubyte');
%test_lable = textread('C:\Users\Administrator\Desktop\数字媒体技术导论大作业\数字媒体技术作业3\mnist\t10k-labels-idx1-ubyte');

train_images = loadMNISTImages('.\mnist\train-images-idx3-ubyte');
train_labels = loadMNISTLabels('.\mnist\train-labels-idx1-ubyte');
test_images = loadMNISTImages('.\mnist\t10k-images-idx3-ubyte');
test_labels = loadMNISTLabels('.\mnist\t10k-labels-idx1-ubyte');

train_images = train_images';
test_images = test_images';

mdl = ClassificationKNN.fit(train_images,train_labels,'NumNeighbors',1);
predict_label = predict(mdl, test_images);
accuracy = length(find(predict_label == test_labels))/length(test_labels)*100;
disp(accuracy);

end

Algorithm description:

(1)读取图像和标签的训练库和测试库;

读取函数实现在loadMNISTImages 函数以及loadMNISTLabels 函数中。
(2)使用K近邻分类器对数据进行分类,并输出分类正确率;

注意:矩阵的维度转换。
(3)得到最后结果;分类正确率为96.9100。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值