Change the axis limits of a SVM plot

I trained a SVM classifier using "fitcsvm" and I got the graph shown below when the data was plotted. I want to make it more readable by reducing the range of axis. How to do it? The code I used is given below and the used datasets are attached.

    close all;
    clear all;
    load ImageDataSet.csv
    load ImageDataSetLabels.csv
    load PhotoshopPredict.csv
   %grp_idx = grp2idx(FeatureLabels);
    X = ImageDataSet(1:1763,:);
    y = ImageDataSetLabels(1:1763,:);
    X_new_data = PhotoshopPredict(1:end,:);
    %dividing the dataset into training and testing 
    rand_num = randperm(1763);
    %training Set
    X_train = X(rand_num(1:1410),:);
    y_train = y(rand_num(1:1410),:);
    %testing Set
    X_test = X(rand_num(1411:end),:);
    y_test = y(rand_num(1411:end),:);
    %preparing validation set out of training set
    c = cvpartition(y_train,'k',5);
     SVMModel = fitcsvm(X_train,y_train,'Standardize',true,'KernelFunction','RBF',...
    'KernelScale','auto','OutlierFraction',0.05);
    CVSVMModel = crossval(SVMModel);
    classLoss = kfoldLoss(CVSVMModel)
    classOrder = SVMModel.ClassNames
    sv = SVMModel.SupportVectors;
   figure
   gscatter(X_train(:,1),X_train(:,2),y_train)
   hold on
   plot(sv(:,1),sv(:,2),'ko','MarkerSize',10)
   legend('Resampled','Non','Support Vector')
   hold off
   X_test_w_best_feature =X_test(:,:);
   [c,score] = predict(SVMModel,X_new_data);
   saveCompactModel(SVMModel,'SVM1000Images');

 Akzeptierte Antwort

Jonathon Gibson
Jonathon Gibson am 27 Jun. 2018
Bearbeitet: Jonathon Gibson am 27 Jun. 2018

1 Stimme

To change the axis limits, you can add axis([xmin xmax ymin ymax]) to the end of your script. This will make some of the higher points not visible, but gives you a better sense of the data:
axis([0 200 0 5000]);
Because the data is so spread out, it might help to instead use a logarithmic scale on your axes:
axis tight;
set(gca,'yscale','log');
set(gca,'xscale','log');

4 Kommentare

NC
NC am 27 Jun. 2018
Thank you very much. Could you please tell the reason for support vectors being placed at the left most corner?
Jonathon Gibson
Jonathon Gibson am 27 Jun. 2018
Bearbeitet: Jonathon Gibson am 27 Jun. 2018
It looks like most of your support vectors are small compared to the rest of what you're plotting, so they are essentially all being shown at the origin, which happens to be the bottom left corner.
sv =
-0.0448 -0.0274 1.2708 1.2718
6.1127 1.0149 1.0027 1.0014
-0.0031 -0.0273 0.8030 0.8022
0.2574 -0.0249 -1.3140 -1.3130
-0.0490 -0.0274 0.3421 0.3422
-0.0490 -0.0274 0.3781 0.3801
-0.0427 -0.0274 0.2288 0.2284
-0.0486 -0.0274 0.9908 0.9919
-0.0489 -0.0274 0.6228 0.6220
0.0772 -0.0269 -0.7844 -0.7865
...
-0.0494 -0.0274 0.6747 0.6742
37.0075 37.5089 1.4439 1.4425
-0.0406 -0.0274 0.4488 0.4465
-0.0132 -0.0273 -1.3379 -1.3367
0.0624 -0.0270 -0.6791 -0.6775
0.0308 -0.0272 -1.0380 -1.0379
0.0910 -0.0268 -0.8633 -0.8624
-0.0330 -0.0274 -1.1611 -1.1612
-0.0391 -0.0274 -0.3820 -0.3834
-0.0363 -0.0274 -1.3108 -1.3130
So I'd say sv is wrong. Instead of 'SVMModel.SupportVectors', I'd use 'SVMModel.IsSupportVector' to filter the plotted data, like this:
plot(X_train(SVMModel.IsSupportVector,1),X_train(SVMModel.IsSupportVector,2),'ko','MarkerSize',10)
Which gives you more of what you're looking for:
NC
NC am 27 Jun. 2018
Thanks again. Now I have another problem. When I used the trained model to predict mew data. It always gives wrong answers. Is this caused by wrong dataset?
Jonathon Gibson
Jonathon Gibson am 28 Jun. 2018
Bearbeitet: Jonathon Gibson am 28 Jun. 2018
I believe that your sv was smaller than expected and you are getting the wrong predictions because you train on standardized data, but then try to test and plot the raw unstandardized data. Change to this to see the difference:
SVMModel = fitcsvm(X_train,y_train,'Standardize',false,'KernelFunction','RBF',...
'KernelScale','auto','OutlierFraction',0.05);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by