如何在OneHotEncoder和LabelEncoder中进行inverse_transform?

时间:2020-05-01 21:27:22

标签: python python-3.x scikit-learn

我检查了OneHotEncoder没有inverse_transform()方法。如何通过逆向转换来找回价值?

代码:

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)

例如:Classe_encprevisores_enc,如何进行逆变换,即通过逆变换来取回值?

2 个答案:

答案 0 :(得分:1)

您可以使用numpy中的argmax函数来获取具有最大值的数组元素的索引(这将是元素的索引为1,因为其他值应为0)。然后,如有必要,您可以使用字典将该索引与类标签相关联。

答案 1 :(得分:1)

可能是您指的是错误的文档。 inverse_transform可用于OneHotEncoderordinalEncoder

请参见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')

类似地,您可以为ordinalEncoder做此事,请参考here