2
我寫了我認爲是相當簡單的SVM分類器[SVM =支持向量機]。使用具有不同參數的正態分佈數據對其進行「測試」,分類器返回的準確率爲50%。哪裏不對?明確分離數據的機會水平準確性
下面是代碼,結果應該是可重複:
features1 = normrnd(1,5,[100,5]);
features2 = normrnd(50,5,[100,5]);
features = [features1;features2];
labels = [zeros(100,1);ones(100,1)];
%% SVM-Classification
nrFolds = 10; %number of folds of crossvalidation
kernel = 'linear'; % 'linear', 'rbf' or 'polynomial'
C = 1; % C is the 'boxconstraint' parameter.
cvFolds = crossvalind('Kfold', labels, nrFolds);
for i = 1:nrFolds % iterate through each fold
testIdx = (cvFolds == i); % indices test instances
trainIdx = ~testIdx; % indices training instances
% train the SVM
cl = fitcsvm(features(trainIdx,:), labels(trainIdx),'KernelFunction',kernel,'Standardize',true,...
'BoxConstraint',C,'ClassNames',[0,1]);
[label,scores] = predict(cl, features(testIdx,:));
eq = sum(labels(testIdx));
accuracy(i) = eq/numel(labels(testIdx));
end
crossValAcc = mean(accuracy)
是的總bug,現在才明白!問題:總體邏輯是否正確?我想當我沒有爲超參數進行優化時(如此處),那麼這個驗證準確度等於我的分類器在新測試集上的準確度? – Pegah
@Pegah你的邏輯確實很好。此外,如果在此情況下不優化超參數,如果特徵空間是完全線性可分的,則每次摺疊的交叉驗證精度應匹配,這意味着平均值應與實際的準確度相匹配。您已經人爲製作了數據集,其手段彼此非常不同,以確保兩個數據分佈之間不存在重疊,因此這應該在每次摺疊時爲您提供相同的交叉驗證。 – rayryeng
很有道理,thx非常多 – Pegah