Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy reverse keras to_categorical

Tags:

numpy

keras

In keras I have used to_categorical to convert by binary nx1 vector y to a nx2 matrix where the first columns is 1 if y=1 and the second column is y=0. How do I reverse this action using numpy?

like image 903
jacob Avatar asked Nov 19 '17 18:11

jacob


People also ask

What does the to_categorical () function in the keras package do?

to_categorical functionConverts a class vector (integers) to binary class matrix.

What is Np_utils to_categorical?

np_utils. to_categorical is used to convert array of labeled data(from 0 to nb_classes - 1 ) to one-hot vector. The official doc with an example. In [1]: from keras. utils import np_utils # from keras import utils as np_utils Using Theano backend.

Why is to_categorical used?

You use to_categorical to transform your training data before you pass it to your model. If your training data uses classes as numbers, to_categorical will transform those numbers in proper vectors for using with models. You can't simply train a classification model without that.

What is Num_classes in CNN?

num_classes: Total number of classes. If nothing is mentioned, it considers the largest number of the input vector and adds 1, to get the number of classes. Its default value is "None". dtype: It is the desired data type of the output values.


2 Answers

Simple.

numpy.argmax(a, axis=None, out=None)

This returns the indices of the maximum values along an axis.

like image 190
MazeRunner09 Avatar answered Sep 28 '22 02:09

MazeRunner09


Adding to MazeRunner09's answer. If you used to_categorical from keras, you will have a list and can use a list comprehension over the entire one-hot encoded list:

y_classes = [np.argmax(y, axis=None, out=None) for y in y_test]
like image 23
cannin Avatar answered Sep 28 '22 01:09

cannin