有没有办法在 pandas 数据框中显示分类自变量的回归系数?

Is there a way to display regression coefficients in a pandas data frame for categorical independent variables?

我建立了一个多元线性回归模型,并使用 model.coef_ 找到了系数。 我想制作一个 pandas 数据框来显示每个因素及其系数。

pd.DataFrame(model.coef_, x.columns, columns = ['coef']).sort_values(by = 'coef', ascending = False) 仅适用于数值自变量。我有两列分类变量,我都对它们进行了编码。

假设一列的两个值是 'male' 和 'female',我想显示单独的系数,如

coef
Male 0.2
Female 0.3

可以这样做吗?

您可以在

之后set_index
(pd.DataFrame({'coef':model.coef_, 'category':x.columns})
.sort_values(by = 'coef', ascending = False)
.set_index('category'))