Matlab中常用的分类器有随机森林分类器、支持向量机(SVM)、K近邻分类器、朴素贝叶斯、集成学习方法和鉴别分析分类器等。各分类器的相关Matlab函数使用方法如下:
首先对以下介绍中所用到的一些变量做统一的说明:
train_data——训练样本,矩阵的每一行数据构成一个样本,每列表示一种特征
train_label——训练样本标签,为列向量
test_data——测试样本,矩阵的每一行数据构成一个样本,每列表示一种特征
test_label——测试样本标签,为列向量
①随机森林分类器(Random Forest)
TB=TreeBagger(nTree,train_data,train_label);
predict_label=predict(TB,test_data);
②支持向量机(Support Vector Machine,SVM)
SVMmodel=svmtrain(train_data,train_label);
predict_label=svmclassify(SVMmodel,test_data);
③K近邻分类器(KNN)
KNNmodel=ClassificationKNN.fit(train_data,train_label,'NumNeighbors',1);
predict_label=predict(KNNmodel,test_data);
④朴素贝叶斯(Naive Bayes)
Bayesmodel=NaiveBayes.fit(train_data,train_label);
predict_label=predict(Bayesmodel,test_data);
⑤集成学习方法(Ensembles for Boosting)
Bmodel=fitensemble(train_data,train_label,'AdaBoostM1',100,'tree','type','classification');
predict_label=predict(Bmodel,test_data);
⑥鉴别分析分类器(Discriminant Analysis Classifier)
DACmodel=ClassificationDiscriminant.fit(train_data,train_label);
predict_label=predict(DACmodel,test_data);
具体使用如下:(练习数据下载地址如下http://en.wikipedia.org/wiki/Iris_flower_data_set,
简单介绍一下该数据集:有一批花可以分为3个品种,不同品种的花的花萼长度、花萼宽度、花瓣长度、花瓣宽度会有差异,根据这些特征实现品种分类)
%% 随机森林分类器(Random Forest) nTree=10;
B=TreeBagger(nTree,train_data,train_label,'Method', 'classification');
predictl=predict(B,test_data); predict_label=str2num(cell2mat(predictl));
Forest_accuracy=length(find(predict_label ==
test_label))/length(test_label)*100; %% 支持向量机 % SVMStruct =
svmtrain(train_data, train_label); % predictl=svmclassify(SVMStruct,test_data);
% predict_label=str2num(cell2mat(predictl)); %
SVM_accuracy=length(find(predict_label == test_label))/length(test_label)*100;
%% K近邻分类器(KNN) % mdl =
ClassificationKNN.fit(train_data,train_label,'NumNeighbors',1); %
predict_label=predict(mdl, test_data); % KNN_accuracy=length(find(predict_label
== test_label))/length(test_label)*100 %% 朴素贝叶斯 (Naive Bayes) % nb =
NaiveBayes.fit(train_data, train_label); % predict_label=predict(nb,
test_data); % Bayes_accuracy=length(find(predict_label ==
test_label))/length(test_label)*100; %% 集成学习方法(Ensembles for Boosting, Bagging,
or Random Subspace) % ens = fitensemble(train_data,train_label,'AdaBoostM1'
,100,'tree','type','classification'); % predictl=predict(ens,test_data); %
predict_label=str2num(cell2mat(predictl)); %
EB_accuracy=length(find(predict_label == test_label))/length(test_label)*100;
%% 鉴别分析分类器(discriminant analysis classifier) % obj =
ClassificationDiscriminant.fit(train_data, train_label); %
predictl=predict(obj,test_data); % predict_label=str2num(cell2mat(predictl)); %
DAC_accuracy=length(find(predict_label == test_label))/length(test_label)*100;
%% 练习 % meas=[0 0;2 0;2 2;0 2;4 4;6 4;6 6;4 6]; % [N n]=size(meas); %
species={'1';'1';'1';'1';'-1';'-1';'-1';'-1'}; %
ObjBayes=NaiveBayes.fit(meas,species); % x=[3 3;5 5]; %
result=ObjBayes.predict(x);
热门工具 换一换