Linear Regression Calculator

Linear Regression Calculator y = mx + b

Paste your data below (one point per line as x y or x,y). You get the best-fit line, R², a live scatter plot, and a prediction — with the steps and the NumPy code.

Examples:

This free linear regression calculator fits the best straight line through your data, draws it on a live scatter plot, and reports the equation, R², and correlation — then shows the working and the NumPy code. Paste your points above, press Fit line, and you see exactly how well a line describes the relationship, not just the numbers.

What linear regression actually does

Linear regression finds the straight line y = mx + b that comes closest to a set of data points. “Closest” has a precise meaning: the line that minimises the sum of the squared vertical distances between each point and the line — the least squares criterion. Those vertical distances are the residuals, and this calculator draws them as faint lines from each point to the fitted line so you can literally see what is being minimised. It is the first genuine machine-learning model most people meet, and it underpins everything from trend forecasting to the output layer of a neural network.

The slope m tells you how much y changes for each one-unit increase in x; the intercept b is the predicted value of y when x is zero. Together they define the line, and once you have them you can predict y for any new x — which is exactly what the prediction box above does.

How to use the linear regression calculator

  1. Enter your data, one point per line, as x y or x,y. You need at least two points.
  2. Press Fit line. The best-fit equation, slope, intercept, R² and correlation appear instantly, alongside a scatter plot with the regression line drawn through it.
  3. Type any value into Predict y at x to get the model’s prediction for a new input.
  4. Open Show the working to see the sums and the least-squares formulas that produced the slope and intercept.

Try the example presets to see a strong fit, a noisy fit, and a negative-slope relationship — a fast way to build intuition for what R² means.

The least-squares formulas

For n points, the slope and intercept are:

m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)

b = (Σy − m·Σx) / n

The calculator shows these sums for your data in the working panel, so you can follow the arithmetic rather than trust a black box. This is the closed-form solution — there is no iteration or guessing involved for a single input variable.

What R² and correlation tell you

A line always fits, but how well? The coefficient of determination R² answers that. It ranges from 0 to 1 and gives the fraction of the variation in y that the line explains. An R² of 0.98 means the line accounts for 98% of the variation — a strong relationship; an R² of 0.2 means the line barely helps. The correlation coefficient r is the signed square root of R²: positive when the line slopes up, negative when it slopes down, and near zero when there is no linear trend. Reading these two numbers together with the plot is the fastest way to judge whether a linear model is even appropriate for your data.

Common mistake
A high R² does not prove causation, and a low R² does not mean there is no relationship — it may just mean the relationship is not a straight line. Always look at the scatter plot: a clear curve with low R² is a sign you need polynomial or another form of regression, not linear.

Linear regression in Python with NumPy

In practice you will fit regressions in code. NumPy does it in one line with polyfit:

import numpy as np
x = np.array([1, 2, 3, 4, 5, 6])
y = np.array([2, 4.1, 5.8, 8.2, 9.9, 12.1])
m, b = np.polyfit(x, y, 1) # slope, intercept
print(m, b)
# R-squared:
r = np.corrcoef(x, y)[0, 1]
print(r**2)

The 1 in polyfit is the polynomial degree — change it to 2 and you get a quadratic fit instead. This calculator returns the same slope, intercept and R² that np.polyfit and np.corrcoef produce, so you can prototype here and reproduce it in code.

Where linear regression shows up in machine learning

Linear regression is the foundation of supervised learning. The idea of defining a loss (squared error), then finding the parameters that minimise it, generalises directly to logistic regression, neural networks, and beyond — only the model and the optimisation method change. Understanding it well, including how the residuals and R² behave, makes every more advanced model easier to reason about.

Frequently asked questions

How many data points do I need?

At least two to define a line, but more points give a more reliable fit and a more meaningful R². A handful of points can produce a perfect-looking fit that does not generalise.

What does the shaded vertical line from each point mean?

That is the residual — the vertical distance between the actual point and the fitted line. Least-squares regression chooses the line that makes the sum of these squared distances as small as possible.

Can it do multiple regression or curves?

This tool fits a single-variable straight line. For curved data, a polynomial or exponential regression is more appropriate; those calculators are being added to this site.

Is the result the same as Excel or NumPy?

Yes. The slope, intercept and R² match Excel’s SLOPE/INTERCEPT/RSQ functions and NumPy’s polyfit exactly, because they all use the same least-squares formulas.

Is the calculator free?

Completely free, no sign-up, and it runs entirely in your browser — including the chart, which never sends your data anywhere.

Related calculators

Explore the full linear algebra calculators hub, or keep building your toolkit with the matrix multiplication calculator and the dot product calculator.