Converting 1D array to 2 dimensional matrix based on python class

I am using a multi-class classifier, so in order to evaluate it after testing, I need the predictions from the classifier ( y_pred

) to compare to the true values ​​of the class ( y_test

).

But I have both as 1D arrays, for example:

y_test = [1, 1, 1, 2, 1, 4, 5, 3, ... etc ]
y_pred = [1, 1, 1, 2, 3, 2, 5, 0, ... etc ]

      

I have 46 classes in total.

But to build curves ROC (like here: http://scikit-learn.org/stable/auto_examples/plot_roc.html ), I assume that I need y_test

, and y_pred

to be in a 2D-array with binary values of the following forms: number_of_test_cases x number_of_classes

.

Where each column represents one class and 1 represents the fact that the classifier recognized that class in the given row of the test case.

So given the above values, I showed that I needed y_test to look something like this:

y_test = [ 1 0 ... 
           1 0
           1 0
           0 1 
           1 0   
           0 1
           0 0
           0 0
           ...

      

Here's what I understand ... I hope I'm right!

Is there any function numpy

to create such a matrix from a 1D array?

+1


source to share


1 answer


Take a look at the function label_binarize

that is listed in the sample code in your link.



+1


source







All Articles