matlabSVM多分类:fitcecoc函数
1.数据集:采用 matlab2016b 自带数据集:iris鸢尾花、ionosphere电离层数据
2.采用函数 fitcecoc 进行SVM多分类模型训练;【fitcecoc:ecoc:error-correcting output code】
3.采用10折交叉验证对数据集进行划分,将Mdl转化为 CVMdl
4.将误差ossLoss作为模型的评价指标
示例1:鸢尾花数据集iris
function [CVMdl,oosLoss]=SVM3() %分成三类
load fisheriris
X = meas; %150*4 :150个样本,4个特征(萼长、萼宽、瓣长、瓣宽);meas=measure(长度)
Y = species; %三种属性{'setosa','versicolor','virginica'};species(种类)
t = templateSVM('Standardize',1); %创建SVM模板t;
%templateSVM是fitcecoc函数中的SVM模板;
%standardize:数据标准化,可用help查看templateSVM其他参数
%训练该模型
Mdl = fitcecoc(X,Y,'Learners',t,'ClassNames',{'setosa','versicolor','virginica'});
%验证该模型
CVMdl = crossval(Mdl); %将模型进行交叉验证,平衡模型欠拟合和过拟合
%显示结果
oosLoss = kfoldLoss(CVMdl) %10折交叉验证得到的泛化误差 oosloss =0.033,效果很好
结果如下:
>> [CVMdl,oosLoss]=SVM3()
CVMdl =
classreg.learning.partition.ClassificationPartitionedECOC
CrossValidatedModel: 'ECOC'
PredictorNames: {'x1' 'x2' 'x3' 'x4'}
ResponseName: 'Y'
NumObservations: 150
KFold: 10
Partition: [1x1 cvpartition]
ClassNames: {'setosa' 'versicolor' 'virginica'}
ScoreTransform: 'none'
CrossValidatedModel: 'ECOC'
PredictorNames: {'x1' 'x2' 'x3' 'x4'}
ResponseName: 'Y'
NumObservations: 150
KFold: 10
Partition: [1x1 cvpartition]
ClassNames: {'setosa' 'versicolor' 'virginica'}
ScoreTransform: 'none'
Properties, Methods
oosLoss =
0.0333333333333333
示例2:电离层数据二分类
% matlab自带电离层
load ionosphere;
%使用默认选项训练ECOC多类模型
model_2 = fitcecoc(X,Y);
%创建一个SVM模板
t_2 = templateSVM('Standardize',1);
%接下来训练ECOC分类器
model_2 = fitcecoc(X,Y,'Learners',t_2);
%使用10倍交叉验证交叉验证Mdl
CVmodel_2 = crossval(model_2);
%估算泛化误差
oosLoss_2 = kfoldLoss(CVmodel_2);
结果:
>> [CVmodel_2,oosLoss_2]=SVM31()
CVmodel_2 =
classreg.learning.partition.ClassificationPartitionedECOC
CrossValidatedModel: 'ECOC'
PredictorNames: {1x34 cell}
ResponseName: 'Y'
NumObservations: 351
KFold: 10
Partition: [1x1 cvpartition]
ClassNames: {'b' 'g'}
ScoreTransform: 'none'
Properties, Methods
oosLoss_2 =
0.113960113960115
参考资料:
1.官方文档:https://www.mathworks.com/help/stats/fitcecoc.html
2.代码参考:fitcecoc的其他数据集尝试: https://blog.csdn.net/kekeicon/article/details/72812097,作者:kekeicon
3.matlab自带数据集一览:https://ww2.mathworks.cn/help/stats/sample-data-sets.html