Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I'd like to transform this into an m x k matrix. Each row has 1 at the index corresponding to the label of that data point and 0 otherwise (e.g. if the data point has label 3, the row looks like [0 0 1 0 0 0 0 ...]).

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

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

(where ./ is elementwise division and .== is elementwise logical equals). This achieves what I want by setting everything in the intermediate matrix that is not exactly 1 to 0.

But this solution seems silly and roundabout. Is there a more direct way that I'm missing?

like image 217
ethan.roday Avatar asked Dec 03 '22 19:12

ethan.roday


2 Answers

You can use logical arrays:

M = [1:k] == y;
like image 166
Markus Malkusch Avatar answered Dec 11 '22 08:12

Markus Malkusch


Given a label vector y such as [1 2 2 1 3 2 3 1] and a number of classes k such as 3, you can convert this to a label matrix Y as follows.

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

The idea is to perform the following expansions:

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 yields:

1 0 0
0 1 0
0 1 0
1 0 0
0 0 1
0 1 0
0 0 1
1 0 0
like image 36
Timothy Shields Avatar answered Dec 11 '22 08:12

Timothy Shields