This post shows how to implement Linear Regression from scratch in Python — without using machine learning libraries like scikit-learn — so you actually understand the math behind one of the most fundamental AI and machine learning algorithms.

Linear regression from scratch in Python:

Explanation:
Linear regression finds the best-fit straight line (y = mx + b) through a set of data points. We calculate the slope (m) and intercept (b) using the Ordinary Least Squares formulas, which minimize the squared difference between actual and predicted values. Once we have m and b, we can predict a y value for any new x.


Python Program for Linear Regression from Scratch

x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 4, 5]

n = len(x)
mean_x = sum(x) / n
mean_y = sum(y) / n

numerator = sum((x[i] - mean_x) * (y[i] - mean_y) for i in range(n))
denominator = sum((x[i] - mean_x) ** 2 for i in range(n))

slope = numerator / denominator
intercept = mean_y - slope * mean_x

print(f"Slope (m): {slope:.2f}")
print(f"Intercept (b): {intercept:.2f}")

new_x = 6
predicted_y = slope * new_x + intercept
print(f"Predicted y for x={new_x}: {predicted_y:.2f}")

Output:

Slope (m): 0.60
Intercept (b): 2.20
Predicted y for x=6: 5.80

This is exactly what libraries like scikit-learn's LinearRegression do internally — understanding the formula first makes the library version much less of a black box. Try running and modifying this yourself in our Code Runner, and see more foundational algorithms in our Interview Prep section.


Frequently Asked Questions About Linear Regression in Python

Do I need NumPy or scikit-learn for linear regression?

No. This implementation uses only built-in Python and basic math — useful for learning how the algorithm actually works. In production code, libraries like NumPy and scikit-learn are faster and handle more complex cases, but the underlying math is the same.

What does the slope and intercept actually mean?

The slope tells you how much y changes for every one-unit increase in x, and the intercept is the predicted value of y when x is 0 — together they define the best-fit line through your data.

Is linear regression considered AI?

Yes — linear regression is one of the foundational algorithms in machine learning, a core branch of AI. It's often the first algorithm taught because many more advanced models build on the same underlying idea of fitting a function to data.