Learn

Linear Regression

Scikit-Learn

Congratulations! You’ve now built a linear regression algorithm from scratch.

Luckily, we don’t have to do this every time we want to use linear regression. We can use Python’s scikit-learn library. Scikit-learn, or `sklearn`

, is used specifically for Machine Learning. Inside the `linear_model`

module, there is a `LinearRegression()`

function we can use:

`from sklearn.linear_model import LinearRegression`

You can first create a `LinearRegression`

model, and then fit it to your `x`

and `y`

data:

```
line_fitter = LinearRegression()
line_fitter.fit(X, y)
```

The `.fit()`

method gives the model two variables that are useful to us:

- the
`line_fitter.coef_`

, which contains the slope - the
`line_fitter.intercept_`

, which contains the intercept

We can also use the `.predict()`

function to pass in x-values and receive the y-values that this line would predict:

`y_predicted = line_fitter.predict(X)`

**Note:** the `num_iterations`

and the `learning_rate`

that you learned about in your own implementation have default values within scikit-learn, so you don’t need to worry about setting them specifically!

We have imported a dataset of soup sales data vs temperature.

Run the code to see the scatterplot. Can you envision the line that would fit this data?

Create an `sklearn`

linear regression model and call it `line_fitter`

.

Fit the `line_fitter`

object to `temperature`

and `sales`

.

Create a list called `sales_predict`

that is the predicted sales values that `line_fitter`

would generate from the `temperature`

list.

Plot `sales_predict`

against `temperature`

as a line, on the same plot as the scatterplot.