Generate matrix of vectors from labels for multiclass classification (vectorized)

I am structuring my input for a multiclassical classifier (m data points, k classes). In my input, I have labels for the training data as integers in the vector y (i.e. y is m dimensional and each entry in y is an integer from 1 to k).

I would like to convert this to an mx k matrix. Each row has 1 in the index corresponding to the label of that data point, and 0 otherwise (for example, if the data point has label 3, the row looks like [0 0 1 0 0 0 0 ...]).

I can do this by plotting a vector a = [1 2 3 4 ... k] and then calculating

M_ = y*(1./b)
M = M_ .== 1

      

(where ./

is elemental separation, and .==

is elementally logically equal). This is achieved by what I want, by setting everything in an intermediate matrix that is not exactly 1 to 0.

But this decision seems silly and devious. Is there a more direct route that I am missing?

+3


source to share


3 answers


For a label vector y

like a [1 2 2 1 3 2 3 1]

series of classes k

like 3

, you can convert this to a label matrix y

like this.

function Y = labelmatrix(y, k)
  m = length(y);
  Y = repmat(y(:),1,k) .== repmat(1:k,m,1);

      

The idea is to do the following decompositions:



1 1 1     1 2 3
2 2 2     1 2 3
2 2 2     1 2 3
1 1 1 .== 1 2 3
3 3 3     1 2 3
2 2 2     1 2 3
3 3 3     1 2 3
1 1 1     1 2 3

      

This gives:

1 0 0
0 1 0
0 1 0
1 0 0
0 0 1
0 1 0
0 0 1
1 0 0

      

+4


source


You can use boolean arrays:



M = [1:k] == y;

      

+7


source


Or simply by indexing:

%// Dummy code to generate some input data
y = [1 4 3 7 2 1];
m = length(y);
k = max(y);

%// Actual conversion using y elements as index 
M = zeros(m, k);
M(sub2ind(size(M), [1:m], y)) = 1

%// Result
M =
1   0   0   0   0   0   0
0   0   0   1   0   0   0
0   0   1   0   0   0   0
0   0   0   0   0   0   1
0   1   0   0   0   0   0
1   0   0   0   0   0   0

      

+2


source







All Articles