CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Machine Learning for Marketing in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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=['___']))
Modifier et exécuter le code