我检查了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_enc
和previsores_enc
,如何进行逆变换,即通过逆变换来取回值?
答案 0 :(得分:1)
您可以使用numpy中的argmax函数来获取具有最大值的数组元素的索引(这将是元素的索引为1,因为其他值应为0)。然后,如有必要,您可以使用字典将该索引与类标签相关联。
答案 1 :(得分:1)
可能是您指的是错误的文档。 inverse_transform
可用于OneHotEncoder
和ordinalEncoder
。
请参见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