Table of Contents

Exercise 6

Program:

Downloads:

Likely, you will not manage to finish the implementation of all the functions during the excercise in the lab. Finish them as a home work.

STPR toolbox installation

  1. Download the STPR toolbox, unpack it to a directory.
  2. In MATLAB, go to that directory and run the script stprpath which sets the needed paths to the individual parts of the toolbox.
  3. In the same directory, run the script compilemex which compiles the parts of toolbox written in C.

Answer the following questions:

The ''model'' structure

Output of all training algorithms in the STPR toolbox is a structure called a model. After using the svm2 function, the model structure may look like this:

model = 
 
       Alpha: [40x1 double]
           b: -2.3417
          sv: [1x1 struct]
         nsv: 40
           W: [64x1 double]
     options: [1x1 struct]
      kercnt: 46909
      trnerr: 0
      errcnt: 0
    exitflag: 2
        stat: [1x1 struct]
     cputime: 0.1993
         fun: 'svmclass'

The structure contains all the information needed to use a SVM classifier:

What exactly do we check by the following command?

all(model.sv.X * model.Alpha == model.W)

The wedge and XOR datasets

Use the scripts from the last week where you classified the wedge and XOR datasets using neural networks from NETLAB toolbox.

  1. Modify the scripts to classify the datasets using SVM (start with the default linear kernel).
    • The STPR toolbox needs class labels 1 and 2 (instead of our 0 and 1). Recode the y variable.
  2. Visualize the prediction of the models.
    • You can use the functions pboundary(), pareas() and pwpatterns(), e.g. in the following way:

    % Create a new figure
    figure; hold on;
    % Fill areas that belong to classes 1 and 2, respectively
    ha = pareas(model);
    % Plot the data, distinguished by colors
    hx = pwpatterns(data); 
    % Plot circles around the support vectors
    plot(model.sv.X(1,:), model.sv.X(2,:),'ko', 'Linewidth', 2, 'MarkerSize',8);
    % Plot the boundary between classes and make it thicker
    hl = pboundary(model); set(hl,'Linewidth',3,'color','k');
 

Explore:

Hand-written digits dataset

Binary classification

Use the hand-written digits dataset as data for classification.

Multiclass classification

Training of multiclass SVM can be done e.g. by bsvm2() function.

Additional questions

  1. Explore the functions perceptron() and linclass(). Do you see the similarity with the functions trainClassLinearPerceptron() and predClassLinear() that you created during the Exercise 3?
  2. Try the demos from the STPR toolbox, demo_linclass and demo_svm.