DEV Community

Cover image for Linear Regression Explained: From Equation to Prediction + Python Examples
Rijul Rajesh
Rijul Rajesh

Posted on

Linear Regression Explained: From Equation to Prediction + Python Examples

This article explores a common machine learning problem called linear regression.

What is linear regression?

Consider that you have many data points. Linear regression tries to draw the best straight line through the given data points.

Example:

  • Study hours -> Exam marks
  • More hours put into study -> Higher marks

We can express this like this:

marks = m * hours + c
Enter fullscreen mode Exit fullscreen mode
  • m = slope
  • c = intercept (where the line cuts the y-axis)

Now let’s take some sample values:

(1, 40)
(2, 50)
(3, 60)
(4, 70)
Enter fullscreen mode Exit fullscreen mode

A straight line will fit these nicely.

Linear regression will find the best m and c values, so the line is as close as possible to all the points.

Visualizing via Python

import numpy as np
import matplotlib.pyplot as plt

# Input data (X must be 2D)
hours = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
marks = np.array([40, 50, 60, 65, 75])

plt.scatter(hours, marks)
plt.xlabel("Study Hours")
plt.ylabel("Marks")
plt.title("Study Hours vs Marks")
plt.show()
Enter fullscreen mode Exit fullscreen mode

Training the model

from sklearn.linear_model import LinearRegression

model = LinearRegression()
model.fit(hours, marks)
print("Slope (m):", model.coef_[0])
print("Intercept (c):", model.intercept_)
Enter fullscreen mode Exit fullscreen mode

We get this output:

Slope (m): 8.5
Intercept (c): 32.5
Enter fullscreen mode Exit fullscreen mode

Drawing the regression line

predicted_marks = model.predict(hours)

plt.scatter(hours, marks, label="Actual Data")
plt.plot(hours, predicted_marks, color="red", label="Regression Line")
plt.xlabel("Study Hours")
plt.ylabel("Marks")
plt.title("Linear Regression Example")
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Making predictions using linear regression

Suppose let’s just do a simple prediction of the marks gained by students if 6 hours are put in.

hours_new = np.array([[6]])
predicted = model.predict(hours_new)

print("Predicted marks for 6 hours:", predicted[0])
Enter fullscreen mode Exit fullscreen mode

We get the following result:

Predicted marks for 6 hours: 83.5
Enter fullscreen mode Exit fullscreen mode

Wrapping up

Linear regression is just another building block when you decide to dive into concepts like gradient descent, backpropagation, and neural networks in general. This problem, and how we can design solutions for simple use cases, is interesting.

You can try out the examples in the Colab Notebook

If you’ve ever struggled with repetitive tasks, obscure commands, or debugging headaches, this platform is here to make your life easier. It’s free, open-source, and built with developers in mind.

👉 Explore the tools: FreeDevTools
👉 Star the repo: freedevtools

Top comments (0)