• 大小: 11.25MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-26
  • 语言: Matlab
  • 标签: mnist  KNN  Naive  Bayes  SVM  

资源简介

机器学习大作业,用matlab对mnist手写字分类,包含knn bayes 和svm算法

资源截图

代码片段和文件信息

% load training set and testing set
clear all;
train_set = loadMNISTImages(‘train-images.idx3-ubyte‘)‘;
train_label = loadMNISTLabels(‘train-labels.idx1-ubyte‘);
test_set = loadMNISTImages(‘t10k-images.idx3-ubyte‘)‘;
test_label = loadMNISTLabels(‘t10k-labels.idx1-ubyte‘);

% cliassify the testing set 
train_scale = size(train_set);
test_scale = size(test_set);
test_classify_label = zeros(test_scale(1)1);
tic;
for i=1:test_scale(1)
    test_point = test_set(i:);
    dist = zeros(train_scale(1)1);
    for j=1:train_scale(1)
        % calculate the distance between test point i and train point j
        train_point = train_set(j :);
        tmp = abs(test_point - train_point);
dist(j) = sum(tmp);
    end
    % find the 9-nearest neighbor 
    dist_tmp = sort(dist);
    num = zeros(10 1);
    for k=1:9
       idx = find(dist==dist_tmp(k));
       num(train_label(idx)+1) = num(train_label(idx)+1)+1;
    end
    % classification
    maxIdx = 0;
    maxNum = -1;
    for k=1:10
      if(num(k)>maxNum)
         maxIdx = k;
         maxNum = num(k);
      end
    end 
    test_classify_label(i) = maxIdx-1;
    disp(test_classify_label(i));  
    disp(test_label(i));  
end
toc;
% calculate accuracy
num_correct = sum(test_label==test_classify_label);
accuracy = num_correct / test_scale(1)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-11-29 21:16  knn-naivebayes-svm\
     目录           0  2017-11-29 21:16  knn-naivebayes-svm\knn\
     文件        1349  2017-11-15 08:15  knn-naivebayes-svm\knn\knn.m
     文件         994  2017-11-14 20:53  knn-naivebayes-svm\knn\loadMNISTImages.m
     文件         571  2017-11-02 15:58  knn-naivebayes-svm\knn\loadMNISTLabels.m
     文件     7840016  1998-01-26 23:07  knn-naivebayes-svm\knn\t10k-images.idx3-ubyte
     文件       10008  1998-01-26 23:07  knn-naivebayes-svm\knn\t10k-labels.idx1-ubyte
     文件    47040016  1996-11-18 23:36  knn-naivebayes-svm\knn\train-images.idx3-ubyte
     文件       60008  1996-11-18 23:36  knn-naivebayes-svm\knn\train-labels.idx1-ubyte
     目录           0  2017-11-29 21:15  knn-naivebayes-svm\naive bayes\
     文件         980  2017-11-09 18:23  knn-naivebayes-svm\naive bayes\loadMNISTImages.m
     文件         571  2017-11-02 15:58  knn-naivebayes-svm\naive bayes\loadMNISTLabels.m
     文件        1579  2017-11-15 08:19  knn-naivebayes-svm\naive bayes\naive.m
     目录           0  2017-11-29 21:16  knn-naivebayes-svm\svm\
     文件          97  2017-11-15 00:26  knn-naivebayes-svm\svm\kernel.m
     文件        1015  2017-11-11 10:28  knn-naivebayes-svm\svm\loadMNISTImages.m
     文件         571  2017-11-02 15:58  knn-naivebayes-svm\svm\loadMNISTLabels.m
     文件         176  2017-11-11 16:26  knn-naivebayes-svm\svm\select.m
     文件        2552  2017-11-15 08:03  knn-naivebayes-svm\svm\smo.m
     文件        1544  2017-11-15 08:24  knn-naivebayes-svm\svm\svm.m

评论

共有 条评论