matlab中的分類器使用小結(SVM、KNN、RF、AdaBoost、Naive Bayes、DAC)

沈子恆發表於2017-05-21

1.前言

目前瞭解到的MATLAB分類器有:K近鄰,隨機森林,樸素貝葉斯,整合學習方法,鑑別分析,支援向量機。現將其主要函式使用方法總結如下,更多細節需參考MATLAB 幫助檔案。
設:

  • 訓練樣本:train_data            % 矩陣,每行一個樣本,每列一個特徵
  • 訓練樣本標籤:train_label       % 列向量
  • 測試樣本:test_data
  • 測試樣本標籤:test_label

2.分類器使用方法

2.1 K近鄰分類器—KNN

mdl = ClassificationKNN.fit(train_data,train_label,'NumNeighbors',1);
predict_label= predict(mdl, test_data);
accuracy     = length(find(predict_label == test_label))/length(test_label)*100;

2.2 隨機森林分類器—RandomF

nTree = 20;
B = TreeBagger(nTree,train_data_pca,train_label');
predict_label = predict(B,test_data_pca);
predict_label = str2double(predict_label);
accuracy = length(find(predict_label == test_label'))/length(test_label)*100;

2.3 樸素貝葉斯分類器—Naive Bayes

nb = NaiveBayes.fit(train_data, train_label);
predict_label = predict(nb, test_data);
accuracy = length(find(predict_label == test_label))/length(test_label)*100;

2.4 整合方法分類器—AdaBoost

ens = fitensemble(train_data,train_label,'AdaBoostM1',100,'tree','type','classification');
predict_label = predict(ens, test_data);
accuracy = length(find(predict_label == test_label))/length(test_label)*100;

2.5 鑑別分析分類器—Discriminant Analysis Classifier

obj = ClassificationDiscriminant.fit(train_data, train_label);
predict_label = predict(obj, test_data);

2.6 支援向量機分類器—SVM

option = statset('MaxIter',1000000);
svm_struct = svmtrain(train_data_pca,train_label,'options',option);
predict = svmclassify(svm_struct,test_data_pca);
correct_num = sum((predict-test_label')==0);
accuracy = correct_num / 3000;

相關文章