I am experiencing some errors will running my data on svm and Treebag
3 views (last 30 days)
Show older comments
%%Create output variable
good = table(dataArray{1:end-1}, 'VariableNames', {'VarName1','VarName2','VarName3','E14','VarName5','VarName6','VarName7','VarName8','VarName9','VarName10','VarName11','VarName12','VarName13','VarName14','VarName15','VarName16','VarName17','VarName18','VarName19','VarName20','VarName21'});
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
Y = good.VarName21;
X = good(:,1:end-1);
C = cvpartition(Y,'holdout',0.5);
XTrain = X(training(C),:);
YTrain = Y(training(C),:);
XTest = X(test(C),:);
YTest = Y(test(C));
disp('Training Set');
tabulate (YTrain);
disp('Test Set')
tabulate(YTest)
%%Discriminant Analysis
da =fitcdiscr(XTrain, YTrain,'discrimType','pseudoQuadratic');
[Y_da,Yscore_da] = da.predict(XTest);
C_da = confusionmat(YTest,Y_da);
C_da = bsxfun(@rdivide,C_da, sum(C_da,2))*100;
%%K Nearest Neighbour
Knn = fitcknn(XTrain,YTrain,'Distance', 'seuclidean');
[Y_Knn,Yscore_knn] = Knn.predict(XTest);
C_Knn = confusionmat(YTest,Y_Knn);
C_knn = bsxfun(@rdivide,C_Knn, sum(C_Knn,2))*100;
% % Support Vector Machine
% opts = struct('optimizer', 'bayesopt', 'ShowPlots',true,'CVPartition',C, 'AcquisitionFunction','expected-improvement-plus');
% %Training the classifier
% svmStruct = fitcsvm(XTrain,YTrain, 'KernelFunction','rbf', 'OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',opts);
% %,'Standardize',true,'kernel_function','rbf','option','opts');
% %Make a prediction for the test set
% [Y_svm,Yscore_svm] = svmclassify(svmStruct,XTest);
% C_svm = confusionmat(YTest,Y_svm);
%
% C_svm = bsxfun(@divide,C_svm,sum(C_svm,2))*100;
%%Ensemble Learning: TreeBagger
%Cost of misclassification
cost = [0 1
7 0];
opts = statset('Useparallel',true);
% Train the classifier
tb = TreeBagger(XTrain,YTrain,'method','classification','options',opts,'OOBVarImp','on''cost',cost);
%Making prediction for the test set
[Y_tb,Yscore_tb] = tb.predict(XTest);
Y_tb = nominal(Y_tb);
%Compute the confusion matrix
C_tb = confusionmat(YTest,Y_tb);
C_tb = bsxfun(@rdivide,C_tb,sum(tb,2))*100;
Cmat = [C_da,C_knn,C_svm, C_tb ];
labels = {'Discriminant Analysis','K-nearest Neighbours', 'Support VM','TreeBagger'};
comparisonPlot(Cmat,labels)
%%ROC Curve for Classification
[xb,yb,~,auc] = perfcurve(Y_Test, Yscore_da(:,2),true);
[xk,yk,~,auc] = perfcurve(Y_Test,Yscore_knn(:,2),true);
[xd,yd,~,auc] = perfcurve(Y_Test, Yscore_svm (:,2),true);
[xL,yL,~,auc] = perfcurve(Y_Test, Yscore_tb (:,2),true);
figure;
plot(xb,yb,'g')
hold on
plot(xk,yk,'b--o')
plot(xd,yd)
plot(xL,yL)
legend('Linear disc''K-nearest Neighbours','KNNNeigbour','TreeBagger');
xlabel('False positive rate');
ylabel('True positive rate');
title('ROC Curves for Naive Bayes Classification, KNN, Decsion Tree, and LDA');
text(0.5,0.25,{'Bayesian analysis with full feature set',strcat('Area Under Curve = ',num2str(auc))},'EdgeColor','k');
hold off
The code above is giving me the following error with respect to svm and treebag For Treebag, I am having the following as error "Error using internal.stats.parseArgs (line 42) Parameter name must be text.
Error in TreeBagger (line 601) = internal.stats.parseArgs(growTreeArgs,growTreeDefs,varargin{:});
Error in testi (line 102) tb = TreeBagger(XTrain,YTrain,'method','classification','options',opts,'OOBVarImp','on''cost',cost);" While SVM is giving me the following error "Error in ClassificationSVM.fit (line 238) temp = ClassificationSVM.template(varargin{:});
Error in fitcsvm (line 278) obj = ClassificationSVM.fit(X,Y,varargin{:});
Error in testi (line 89) svmStruct = fitcsvm(XTrain,YTrain, 'KernelFunction','rbf', 'OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',opts);"
My data set is all numeric values
0 Comments
Answers (1)
Kristen Amaddio
on 27 Jul 2017
Edited: Kristen Amaddio
on 27 Jul 2017
You are seeing this error because there is a typo in the call to 'TreeBagger' when the name-value pairs are provided. The lack of comma between 'on' and 'cost' is throwing off the name-value pair matching. Change this line:
tb = TreeBagger(XTrain,YTrain,'method','classification','options',opts,'OOBVarImp','on''cost',cost);
to the following:
tb = TreeBagger(XTrain,YTrain,'method','classification','options',opts,'OOBVarImp','on','cost',cost);
Now, you might see a new error that says 'Wrong number of arguments'. This is because 'TreeBagger' expects the first parameter to be 'NumTrees', the number of trees you wish to use. For example, to create a TreeBagger with 50 bagged decision trees, you would do the following:
tb = TreeBagger(50,XTrain,YTrain,'method','classification','options',opts,'OOBVarImp','on','cost',cost);
0 Comments
See Also
Categories
Find more on Classification Ensembles in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!