Jiang
Jiang

Reputation: 157

Octave code to plot labeled data

Good afternoon, guys. I'm learning SVM and try to finish an exercise at openclassroom.stanford.edu.

My question is: What is the Octave/Matlab code to plot as follows enter image description here

If I have a set of 2D feature points

{(x_11, x_12), (x_21, x_22), ..., (x_i1, x_i2)},

and the corresponding labels set is

{1, -1, ..., -1 },

what is the code to plot those data in a 2D manner as in that picture?

I would like to make (x_i1, x_i2) correspond to 1 (or -1, whatever).

Thank you very much :)

Upvotes: 0

Views: 947

Answers (2)

Jean-Pierre Schnyder
Jean-Pierre Schnyder

Reputation: 1934

Here's my solution, which works ...

X = [2 2;3 4;0.5 4;3 6;5 7;7 8;6 8]
y = [0;0;0;0;1;1;1]
plot(X(y>0,1), X(y>0,2), 'rs','MarkerFaceColor', 'r', 'MarkerSize', 27, X(y==0,1), X(y==0,2),'go', 'MarkerFaceColor', 'g', 'MarkerSize', 27)
axis([0 10 0 10])

Result:

enter image description here

Upvotes: 0

David
David

Reputation: 8459

Say you have a vector of x-coordinates X, and y-coordinates Y, and an indicator vector k of 1's and -1's, you could do

plot(X(k>0),Y(k>0),'b',X(k<0),Y(k<0),'g')

which uses logical indexing to pick out the elements with k=1 and k=-1 separately, or use scatter and use the k vector to colour the points. I set the colormap to have blue (k=-1) and green (k=1) points.

colormap([0 0 1;0 1 0])
scatter(X,Y,[],k,'filled')

Using plot: (to be fair you could change the markers to filled dots as well) enter image description here

and scatter: enter image description here

Upvotes: 1

Related Questions