如何将混合(分类和数字)特征传递给sklearn中的决策树回归器? [英] how to pass mixed (categorical and numeric) features to Decision Tree Regressor in sklearn?

查看:232
本文介绍了如何将混合(分类和数字)特征传递给sklearn中的决策树回归器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将分类和数字特征传递给sklearn中的DecisionTreeRegressor?

How can I pass Categorical and numeric features to DecisionTreeRegressor in sklearn?

下面的代码显示了如何将DecisionTreeRegressor用于数字功能:

Below code shows how to use DecisionTreeRegressor for numeric features:

from sklearn import tree
make_tree = tree.DecisionTreeRegressor()
fit_tree = make_tree.fit(X_train, y_train)

推荐答案

首先,应对所有分类特征进行编码(以数字表示),以便对回归模型进行解释.为此,您可以使用 LabelEncoder OneHotEncoder 进行.如果使用高基数功能,则可以使用

First, all categorical features should be encoded (represented by numbers) to be interpretable for the regression models. To do so, you can use, LabelEncoder followed by OneHotEncoder. In the case of high-cardinal features, you can use FeatureHasher.

例如:

from sklearn.feature_extraction import FeatureHasher

# n_feature: number of unique values in the feature(s)
# input_type should be passed as 'string' to be compatible to pandas DataFrames
feature_hasher = FeatureHasher(n_features=5000, input_type='string')
df['COLUMN_NAME'] = feature_hasher.transform(df['COLUMN_NAME'])

然后,您可以将功能传递给回归器.

Then, you can pass your features to the regressor.

这篇关于如何将混合(分类和数字)特征传递给sklearn中的决策树回归器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆