Showing the coefficients and intercept
Once the logistic regression model is ready, it can be interesting to have a look at the coefficients to check whether the model makes sense.
Given a fitted logistic regression model logreg, you can retrieve the coefficients using the attribute coef_
. The order in which the coefficients appear, is the same as the order in which the variables were fed to the model. The intercept can be retrieved using the attribute intercept_
.
The logistic regression model that you built in the previous exercises has been added and fitted for you in logreg
.
This is a part of the course
“Introduction to Predictive Analytics in Python”
Exercise instructions
- Assign the coefficients of the logistic regression model to the list
coef
. - Assign the intercept of the logistic regression model to the variable
intercept
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Construct a logistic regression model that predicts the target using age, gender_F and time_since_last gift
predictors = ["age","gender_F","time_since_last_gift"]
X = basetable[predictors]
y = basetable[["target"]]
logreg = linear_model.LogisticRegression()
logreg.fit(X, y)
# Assign the coefficients to a list coef
coef = ____.____
for p,c in zip(predictors,list(coef[0])):
print(p + '\t' + str(c))
# Assign the intercept to the variable intercept
intercept = ____.____
print(intercept)
This exercise is part of the course
Introduction to Predictive Analytics in Python
In this course you'll learn to use and present logistic regression models for making predictions.
In this Chapter, you'll learn the basics of logistic regression: how can you predict a binary target with continuous variables and, how should you interpret this model and use it to make predictions for new examples?
Exercise 1: Introduction and base table structureExercise 2: Structure of the base tableExercise 3: Exploring the base tableExercise 4: Exploring the predictive variablesExercise 5: Logistic regressionExercise 6: Interpretation of coefficientsExercise 7: Building a logistic regression modelExercise 8: Showing the coefficients and interceptExercise 9: Using the logistic regression modelExercise 10: Making predictionsExercise 11: Donor that is most likely to donateWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.