【发布时间】:2016-01-16 14:59:59
【问题描述】:
我正在研究年龄预测主题,我可以通过训练每个具有正数的类而不是所有其他类来实现具有 11 个类的多类 SVM,如 here 和 here 所示。
但问题出在for循环中,如下图,训练数据需要11个循环:
for k = 1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G_x_All = (train_label == u(k));
G_x_All = double (G_x_All);
SVMStruct{k} = svmtrain(Data_Set, G_x_All);
end
那么,数据分类也需要对每张图像进行 11 次循环:
for j = 1:total_images
for k = 1:numClasses
if(svmclassify(SVMStruct{k}, Test_Img(j,:)));
break;
end
end
Age (j) = u(k); % Put the number of correct class in Age vector
end
我的菜鸟问题是,在所有这些循环之后,我怎样才能制作k-fold cross validation?
编辑 ::>
这是根据zelanix先生的建议对代码进行的最后一次更新,但我得到了不好的结果。你能帮我提高它的性能吗?
u = unique(train_label);
numClasses = length (u);
N = size (Data_Set,1)
A = 10;
indices = crossvalind('Kfold', N, A);
cp = classperf (train_label);
for i = 1:A
Test = (indices == i);
Train = ~Test;
SVMStruct = cell(numClasses, 1); % Clear data structure.
% Build models
for k = 1:numClasses
%Vectorized statement that binarizes Group
%where 1 is the current class and 0 is all other classes
G_x_All = (train_label == u(k));
G_x_All = double (G_x_All);
SVMStruct{k} = svmtrain(Data_Set (Train,:), G_x_All(Train,:));
end
Age = NaN(size(Data_Set, 1), 1);
% Classify test cases
for k = 1:numClasses
if(svmclassify(SVMStruct{k}, Data_Set(Test,:)));
break;
end
end
Age = u(k);
if Age == 1
disp ('Under 10 years old');
elseif Age == 10
disp ('Between 10 and 20 years old');
elseif Age == 20
disp ('Between 20 and 30 years old');
elseif Age == 30
disp ('Between 30 and 40 years old');
elseif Age == 40
disp ('Between 40 and 50 years old');
elseif Age == 50
disp ('Between 50 and 60 years old');
elseif Age == 60
disp ('Upper 60 years old');
else
disp ('Unknown');
end
classperf(cp, Age, Test);
disp (i)
end
cp.CorrectRate
请注意,我将标签数量从 11 个减少到 7 个。
【问题讨论】:
标签: matlab svm cross-validation