當運行下面提到SVM分類(一個針對全部)代碼我得到以下錯誤在該行:model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
SVM一個分類器對所有
在matlab中:Y必須是一個載體或一個字符數組。
任何人都可以幫助我嗎?
The code is:
%# Fisher Iris dataset
load fisheriris
[~,~,labels] = unique(species); %# labels: 1/2/3
data = zscore(meas); %# scale features
numInst = size(data,1);
numLabels = max(labels);
%# split training/testing
idx = randperm(numInst);
numTrain = 100; numTest = numInst - numTrain;
trainData = data(idx(1:numTrain),:); testData = data(idx(numTrain+1:end),:);
trainLabel = labels(idx(1:numTrain)); testLabel = labels(idx(numTrain+1:end));
這裏是一對,所有的方法對多類SVM實現:
%# train one-against-all models
model = cell(numLabels,1);
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
end
%# get probability estimates of test instances using each model
prob = zeros(numTest,numLabels);
for k=1:numLabels
[~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
prob(:,k) = p(:,model{k}.Label==1); %# probability of class==k
end
%# predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel) %# accuracy
C = confusionmat(testLabel, pred) %# confusion matrix
@ Lennon:謝謝你的答覆。在這裏,我沒有使用libSVM。請幫我解決這個問題。 –
YUVARAJ,svmtrain,svmpredict等函數都是libsvm。你使用哪個軟件包? – lennon310
:我沒有使用任何包。實際上,我從互聯網上獲得了這個代碼。只是我嘗試運行。請你幫我解決這個錯誤。 –