Get Started

Changing the model coefficients

When you call fit with scikit-learn, the logistic regression coefficients are automatically learned from your dataset. In this exercise you will explore how the decision boundary is represented by the coefficients. To do so, you will change the coefficients manually (instead of with fit), and visualize the resulting classifiers.

A 2D dataset is already loaded into the environment as X and y, along with a linear classifier object model.

This is a part of the course

“Linear Classifiers in Python”

View Course

Exercise instructions

  • Set the two coefficients and the intercept to various values and observe the resulting decision boundaries.
  • Try to build up a sense of how the coefficients relate to the decision boundary.
  • Set the coefficients and intercept such that the model makes no errors on the given training data.

Hands-on interactive exercise

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

# Set the coefficients
model.coef_ = np.array([[0,1]])
model.intercept_ = np.array([0])

# Plot the data and decision boundary
plot_classifier(X,y,model)

# Print the number of errors
num_err = np.sum(y != model.predict(X))
print("Number of errors:", num_err)

This exercise is part of the course

Linear Classifiers in Python

IntermediateSkill Level
4.1+
30 reviews

In this course you will learn the details of linear classifiers like logistic regression and SVM.

In this chapter you will discover the conceptual framework behind logistic regression and SVMs. This will let you delve deeper into the inner workings of these models.

Exercise 1: Linear classifiers: the coefficientsExercise 2: How models make predictionsExercise 3: Changing the model coefficients
Exercise 4: What is a loss function?Exercise 5: The 0-1 lossExercise 6: Minimizing a loss functionExercise 7: Loss function diagramsExercise 8: Classification loss functionsExercise 9: Comparing the logistic and hinge lossesExercise 10: Implementing logistic regression

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free