Skip to main content
Colin Jaffe/3 min read

Linear Regression: Predicting Relationships in Data

Build a Linear Regression Model

1

Plot the Data

Scatter plot reveals whether a linear fit is even appropriate.

2

Fit with sklearn

from sklearn.linear_model import LinearRegression; model.fit(X, y).

3

Inspect Coefficients

model.coef_ and model.intercept_ — the line equation y = mx + b.

4

Evaluate with R²

model.score(X_test, y_test) — proportion of variance explained.

Master Machine Learning at Noble Desktop

Noble Desktop's Python Machine Learning Bootcamp covers scikit-learn, Keras, neural networks, and applied ML.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Explore linear regression to predict y values from given x data points by minimizing variance. Watch this tutorial to learn the key concepts and techniques.

Let's move on to regression. We're going to do some pretty neat stuff now with data and with predictions, starting on our road to machine learning. So regression is used to predict the relationship between variables.

In this case, we'll do two variables, X and Y. How do we, given these X and Y points, given just X, we want to be able to predict Y. Let's take a look at the image actually here so that we can visualize this. Let's run this cell block to output this image. So here are some X and Y points, right? Y is vertical, X is horizontal.

So this point is at 5 on the X-axis, 2.5 on the Y-axis, right? We have these points and we can kind of see, we can kind of eyeball that they go up and to the right a little bit, right? The more X increases, the more Y increases, but they're not, it's not a perfect relationship. This data point is kind of far outside the line. This one's a little high, this one's a little low, right? So what a linear regression will do, it will make a best fit line and that line will minimize the distance from the line to all points, right? It'll plot out this line and the way it's calculating that line is by decreasing, by finding the line that has the least amount of difference from all dots, not just one dot, right? We could just draw a line like that goes basically right through these four, but we'd be maximizing the distance to this one.

So a metaphor somebody gave me on this is that this is like driveways and if you wanted to draw, you want to make a street with short driveways to these little red houses here. The street we want is the one where nobody's too upset, right? Even this one with a long driveway isn't so upset, whereas if we did it over here, right through the lines, it would make this person over here very upset if they had a super long driveway, right? So it finds the line that minimizes overall the distances between the line and the points, or in other words, minimizes the variance. And in fact, it is a square mathematical value, it's a variance.

So this is actually, it's actually the sum of the square areas is what it's minimizing here. All right, we'll take a look at some real data and see what we can do with it.