Change the axis limits of a SVM plot

4 views (last 30 days)

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');

Accepted Answer

Jonathon Gibson
Jonathon Gibson on 27 Jun 2018
Edited: Jonathon Gibson on 27 Jun 2018
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 Comments
NC
NC on 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 on 28 Jun 2018
Edited: Jonathon Gibson on 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);

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!