Search code examples
pandasmachine-learningdata-analysis

After LinearRegression Model, How to Pair the Coefficients with the corresponding Features?


I built my first LinearRegression model (ElasticNet), predicting house SalePrice.

I would like to find out the features that have strong correlations (both negative and positive correlations) with the SalePrice

In the screenshot, I listed out all the coefficient and feature names. What code can I use to pair these two values so I can see each feature's coefficient value?

I am very new to coding and data analytics. Thank you in advance!

My model:

grid_model = GridSearchCV(estimator = base_elastic_model,
                     param_grid = param_grid,
                     scoring = 'neg_mean_squared_error',
                     cv=5,
                     verbose=1)
grid_model.fit(scaled_X_train,y_train)

I got the list of coefficient:

grid_model.fit(scaled_X_train,y_train)

I got the list of features whose coefficent with the SalePrice is not 0

df.columns[coef[coef == 0].index]

How can i print a dataframe with Coefficient and Feature Name listed matching each other?


Solution

  • Try this:

    pd.DataFrame(X_train.columns, grid_model.best_estimator_.coef_)
    

    It will give output like this:

    -0.003801   feature0
    -0.033107   feature1
    0.053203    feature2
    -0.645900   feature3
    -7.474264   feature4
    -0.571417   feature5
    0.007333    feature6
    0.184133    feature7
    0.091905    feature8
    0.002021    feature9