Get startedGet started for free

Building a logistic regression model

You can build a logistic regression model using the module linear_model from sklearn. First, you create a logistic regression model using the LogisticRegression() method:

logreg = linear_model.LogisticRegression()

Next, you need to feed data to the logistic regression model, so that it can be fit. X contains the predictive variables, whereas y has the target.

X = basetable[["predictor_1","predictor_2","predictor_3"]]`
y = basetable[["target"]]
logreg.fit(X,y)

In this exercise you will build your first predictive model using three predictors.

This exercise is part of the course

Introduction to Predictive Analytics in Python

View Course

Exercise instructions

  • Import the methodlinear_model from sklearn.
  • The basetable is loaded as basetable. Note that the column "gender" has been transformed to gender_F so that it can be used as a predictor. Construct a DataFrame X that contains the predictors age, gender_F and time_since_last_gift.
  • Construct a DataFrame y that contains the target.
  • Create a logistic regression model.
  • Fit the logistic regression model on the given basetable.

Hands-on interactive exercise

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

# Import linear_model from sklearn.
from ____ import ____

# Create a DataFrame X that only contains the candidate predictors age, gender_F and time_since_last_gift.
X = ____[[____, ____, ____]]

# Create a DataFrame y that contains the target.
y = ____[[____]]

# Create a logistic regression model logreg and fit it to the data.
logreg = ____.____
logreg.____(____, ____)
Edit and Run Code