【发布时间】:2019-09-09 05:31:45
【问题描述】:
考虑以下我试图对其进行预测的示例表
如你所见,我混合了数值 (Num1 & Num2) 和分类特征 (Cat1 & Cat2) 来预测一个值,我是使用随机森林回归来做到这一点
读入文件后,我正在使用 LabelEncoder 将分类特征转换为数值特征,就像这样
category_col =['Cat1', 'Cat2']
labelEncoder = preprocessing.LabelEncoder()
# creating a map of all the numerical values of each categorical labels.
mapping_dict={}
for col in category_col:
df[col] = labelEncoder.fit_transform(df[col])
le_name_mapping = dict(zip(labelEncoder.classes_, labelEncoder.transform(labelEncoder.classes_)))
mapping_dict[col]=le_name_mapping
转换后,我将数据框拆分为训练和测试集并进行预测,就像这样
train_features, test_features, train_labels, test_labels = train_test_split(df, labels, test_size = 0.30)
rf = RandomForestRegressor(n_estimators = 1000)
rf.fit(train_features, train_labels)
predictions = rf.predict(test_features)
我的问题是,如何更改 Cat1 和 Cat2 的数字以再次显示原始类别,以便我可以将预测导出回来,就像这样
我知道我需要使用 labelEncoder.inverse_transform,但是,我似乎无法获得正确的语法来获取与结果相关的类别文本。
感谢任何帮助!
【问题讨论】:
标签: python machine-learning regression random-forest