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?
to_categorical functionConverts a class vector (integers) to binary class matrix.
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.
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.
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.
Simple.
numpy.argmax(a, axis=None, out=None)
This returns the indices of the maximum values along an axis.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With