Search⌘ K

Exercise: Linear Regression in scikit-learn

Explore how to use scikit-learn for linear regression by fitting a line to synthetic data, examining model coefficients, and plotting predicted results. This exercise helps you understand model fitting and prediction within a Python environment, preparing you for practical applications.

Linear regression with scikit-learn

In this exercise, we will take the synthetic data we just generated and determine a line of best fit, or linear regression, using scikit-learn. The first step is to import a linear regression model class from scikit-learn and create an object from it. The import is similar to the LogisticRegression class we worked with previously. As with any model class, you should observe what all the default options are. Notice that for linear regression, there are not that many options to specify: you will use the defaults for this exercise. The default settings include fit_intercept=True, meaning the regression model will include an intercept term. This is certainly appropriate because we added an intercept to the synthetic data. Perform the following steps to complete the exercise, noting that the code creating the data for linear regression from the preceding lessons must be run first in the same notebook, found at the end of the lesson:

  1. Execute this code to import the linear regression ...