如何重新转换scikit的LabelEncoder标签?

时间:2016-10-25 16:22:38

标签: python

这里对Python很新,我相信我的工作中缺少一些东西,但我不知道该找什么......

我做了我的研究并在LabelEncoder函数上在线阅读了很多例子,特别是来自: http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.htmlhttp://scikit-learn.org/stable/modules/preprocessing.html#label-encodinghttp://biggyani.blogspot.com/2014/08/using-onehot-with-categorical.html

然而,我找不到我的问题的答案(在我的代码上尝试了很多东西,但总是遇到错误)。

以下是该帖子的链接,其中描述了我遇到的问题: http://blog.kaukotyo.fi/blog/python/how-to-retransform-scikits-labelencoder-labels.html

当我尝试在预测结果Y上使用.inverse_transform()时,我得到了同样的错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

但它对我来说没有意义,为什么我需要使用.any()或.all()。

所以我的问题是,在我预测转换数据之后,我如何重新转换(或逆转换)我的结果以回到原始标签?

下面是我的代码:

encoded_vals = encoder.fit_transform(df)

imp = Imputer(missing_values="NaN", strategy='mean', axis=0)   
### i am trying to get rid of all the missing values here so that i can fit into the classifier model later

X = pd.DataFrame(imp.fit_transform(encoded_vals), column=df.columns)
Y_in = X.ix[,pred_col]
clf = RandomForestRegressor(max_leaf_nodes=maxLeafNodes, min_samples_leaf=minSamplesPerLeave)
clf = clf.fit(X, Y_in)
Y = clf.predict(X)
Y = list(encoder.inverse_transform(Y))

0 个答案:

没有答案