繁体   English   中英

如何将混合(分类和数字)特征传递给 sklearn 中的决策树回归器?

[英]how to pass mixed (categorical and numeric) features to Decision Tree Regressor in sklearn?

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

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

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

首先,所有分类特征都应该被编码(用数字表示)以便回归模型可以解释。 为此,您可以使用LabelEncoder后跟OneHotEncoder 对于高基数特征,可以使用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'])

然后,您可以将您的特征传递给回归器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM