Reputation: 1337
I checked that OneHotEncoder
does not have an inverse_transform()
method. How to get the values back by reversing the transformation?
Code:
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
base = pd.read_csv(caminho + "risco_credito.csv")
previsores = base.loc[:,["historia","divida","garantias","renda"]].values
classe = base.loc[:,"risco"].values
labelencorder_classe = LabelEncoder()
classe_enc = labelencorder_classe.fit_transform(classe)
onehotencorder = ColumnTransformer(transformers=[("OneHot", OneHotEncoder(), [0,1,2,3])],remainder='passthrough')
previsores_enc = onehotencorder.fit_transform(previsores)
For example: Classe_enc
and previsores_enc
, how to do the inverse transformation, that is, get the values back by reversing the transformation?
Upvotes: 1
Views: 2379
Reputation: 16966
May be you are referring to a wrong documentation. The inverse_transform
is available for OneHotEncoder
and ordinalEncoder
.
See here
>>> import numpy as np
>>> from sklearn.preprocessing import OneHotEncoder
>>> from sklearn.compose import ColumnTransformer
>>> x = np.random.choice(['orange','apple', 'mango'],size=(3,1))
>>> ct = ColumnTransformer(transformers=[("OneHot", OneHotEncoder(sparse=False), [0])],
remainder='passthrough')
>>> x_trans_ = ct.fit_transform(x)
>>> ct.named_transformers_['OneHot'].inverse_transform(x_trans_)
array([['orange'],
['orange'],
['apple']], dtype='<U6')
Similarly, you can do it for ordinalEncoder
, refer here
Upvotes: 1
Reputation: 31
You could use the argmax function from numpy to get the index of the array element with the maximum value (this will be the index of the element that is 1 since the other values should be 0). Then you could use a dictionary to associate this index with a class label if necessary.
Upvotes: 1