Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coverting Back One Hot Encoded Results back to single Column in Python

I was doing Multi-class Classification using Keras.It contained 5 classes of Output. I converted the single class vector to matrix using one hot encoding and made a model. Now to evaluate the model I want to convert back the 5 class probabilistic result back to Single Column.

I am getting this as output in numpy array format

..................0..................1............................2.......................3.............................4

 5.35433665e-02   1.72592481e-05   1.49291719e-03   9.44392741e-01
    5.53713820e-04  
   1.97096306e-05   2.08907949e-08   3.11666554e-07   1.40611945e-07
    9.99979794e-01  
   9.99999225e-01   2.42999278e-07   1.58917388e-07   7.84497018e-08
    2.85837785e-07  
   7.09977685e-05   1.02068476e-09   1.38186664e-07   9.99928594e-01
    2.73126261e-07  
   1.29937407e-05   2.49388819e-07   9.99986231e-01   4.76015231e-07
    7.39421040e-08 

Want to convert this matrix to

[3,4,0,3,2]
like image 909
Abhik Sarkar Avatar asked Jul 19 '17 07:07

Abhik Sarkar


People also ask

What is OneHotEncoder in Python?

OneHotEncoder. Encode categorical integer features using a one-hot aka one-of-K scheme. The input to this transformer should be a matrix of integers, denoting the values taken on by categorical (discrete) features. The output will be a sparse matrix where each column corresponds to one possible value of one feature.

How do you reverse a data frame?

Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.

How do I use OneHotEncoder in Python?

We can load this using the load_dataset() function: # One-hot encoding a single column from sklearn. preprocessing import OneHotEncoder from seaborn import load_dataset df = load_dataset('penguins') ohe = OneHotEncoder() transformed = ohe. fit_transform(df[['island']]) print(transformed.


Video Answer


1 Answers

It seems like you are looking for np.argmax:

import numpy as np
class_labels = np.argmax(class_prob, axis=1) # assuming you have n-by-5 class_prob
like image 116
Shai Avatar answered Nov 03 '22 01:11

Shai