【发布时间】:2021-11-24 05:43:21
【问题描述】:
在计算线性回归并计算我的 R2 分数后,我想计算我的特征的系数(使用 sklearn coef_ 属性)。
关键是在我的特征中,我有一些数字和一些分类数据。为了处理 LinearRegression() 我有 OneHotEncoded 我的分类值。所以这种情况下不能直接计算每个特征的coef_。
您将如何恢复流程以使每个类别特征具有一列(因为 OneHotEncoding 意味着每个类别特征的列值尽可能多)。
我看到了这篇很棒的帖子:https://katstam.com/regression-feature_importance/,它可以解决我的问题。我将此行添加到我的笔记本中(在文章的底部):
onehot_columns = list(clf.named_steps['preprocessor'].named_transformers_['cat'].named_steps['one_hot'].get_feature_names(input_features=categorical_features))
和:
numeric_features_list = list(numeric_features)
numeric_features_list.extend(onehot_columns)
但我不知道“clf”指的是什么。
在文章中,“clf”指的是这个对象:
clf = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', LinearRegression())])
但在我的笔记本中,我分两步处理预处理和线性回归,所以我没有这个“clf”对象的等价物。
你有什么想法或者其他方法可以使用吗?
【问题讨论】:
标签: python machine-learning scikit-learn