Get startedGet started for free

Explore logistic regression coefficients

You will now explore the coefficients of the logistic regression to understand what is driving churn to go up or down. For this exercise, you will extract the logistic regression coefficients from your fitted model, and calculate their exponent to make them more interpretable.

The fitted logistic regression instance is loaded as logreg and the scaled features are loaded as a pandas DataFrame called train_X. The numpy and pandas libraries are loaded as np and pd respectively.

This exercise is part of the course

Machine Learning for Marketing in Python

View Course

Exercise instructions

  • Combine feature names and coefficients into a pandas DataFrame.
  • Calculate the exponent of the logistic regression coefficients.
  • Remove the coefficients that are equal to zero and print them sorted by the exponent coefficient.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Combine feature names and coefficients into pandas DataFrame
feature_names = pd.DataFrame(___.columns, columns = ['Feature'])
log_coef = pd.DataFrame(np.transpose(logreg.coef_), columns = ['Coefficient'])
coefficients = pd.concat([feature_names, ___], axis = 1)

# Calculate exponent of the logistic regression coefficients
coefficients['Exp_Coefficient'] = np.___(coefficients['Coefficient'])

# Remove coefficients that are equal to zero
coefficients = coefficients[coefficients['Coefficient']!=___]

# Print the values sorted by the exponent coefficient
print(coefficients.sort_values(by=['___']))
Edit and Run Code