2

I have built a multiple linear regression model and I found the coefficients using model.coef_. I want to make a pandas data frame which displays each of the factors and its coefficient.

pd.DataFrame(model.coef_, x.columns, columns = ['coef']).sort_values(by = 'coef', ascending = False) works only for the numerical independent variables. I have two columns of categorical variables and I have encoded them both.

Suppose the two values of a column are 'male' and 'female', I want to display the individual coefficients like

coef
Male 0.2
Female 0.3

Is it possible to do this?

Henry Yik
  • 22,275
  • 4
  • 18
  • 40
Sona
  • 35
  • 1
  • 5

1 Answers1

2

You can set_index after

(pd.DataFrame({'coef':model.coef_, 'category':x.columns})
.sort_values(by = 'coef', ascending = False)
.set_index('category'))
alparslan mimaroğlu
  • 1,450
  • 1
  • 10
  • 20