← CS Lab AI & Machine Learning / Linear Regression Open standalone ↗

Linear regression draws the straight line that best fits a cloud of points. Drag the sliders in the tool and you are doing it by hand; least squares does the same thing in one shot — it finds the slope and intercept that make the line miss the data as little as possible.

Where you meet it

What it does

It learns two numbers — a slope and an intercept — so that one input x gives a prediction for the output. Every point's residual (the pink stick in the tool) is how far the line misses it; the goal is to make those misses collectively as small as possible.

A perfect fit proves x predicts y, never that it causes it — and because misses are squared, one outlier can swing the whole line.

How it works

The model is a single line. The slope the tool's m slider sets is written w here — the usual machine-learning name:

ŷ = w·x + b      # w = slope (weight), b = intercept (bias)

For each data point the residual is rᵢ = yᵢ − ŷᵢ. "Best fit" means minimising the sum of squared residuals — equivalently the mean squared error:

MSE = (1/n) · Σ (yᵢ − (w·xᵢ + b))²

Why square the residuals? It keeps positive and negative misses from cancelling, punishes big misses far harder than small ones, and gives a smooth bowl-shaped cost with exactly one bottom — so there is a unique best line. That bottom has a closed-form solution (the normal equations): for one variable, w = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)² and b = ȳ − w·x̄ — no searching needed. With many features or huge datasets you instead roll downhill with gradient descent, nudging w and b against the slope of the error until it bottoms out.

Watch out

  • It assumes the relationship is linear. If the truth curves, a straight line will be wrong in a patterned way no matter how you fit it.
  • Outliers pull hard. Because errors are squared, one far-off point can swing the whole line — inspect your data first.
  • Correlation isn't causation. A good fit means x predicts y, not that x causes y; a hidden third variable may drive both.
  • Read R² honestly. It's the share of variance the line explains (0–1), but a high R² doesn't prove the model is right, and it can't tell you about points outside your data's range.

Go deeper

Lineare Regression legt die Gerade, die am besten durch eine Punktwolke passt. Wenn du im Tool an den Reglern ziehst, machst du genau das von Hand; Kleinste Quadrate erledigt es in einem Schritt — es findet Steigung und Achsenabschnitt, die die Gerade so wenig wie möglich an den Daten vorbeilaufen lassen.

Wo es vorkommt

Was es tut

Es lernt zwei Zahlen — eine Steigung und einen Achsenabschnitt — sodass eine Eingabe x eine Vorhersage für die Ausgabe liefert. Das Residuum jedes Punkts (der pinke Strich im Tool) ist der Abstand, um den die Gerade ihn verfehlt; Ziel ist, diese Fehler in der Summe so klein wie möglich zu machen.

Ein perfekter Fit zeigt, dass x y vorhersagt — nie, dass es y verursacht; und weil Fehler quadriert werden, kippt ein Ausreißer die ganze Gerade.

Wie es funktioniert

Das Modell ist eine einzige Gerade. Die Steigung, die im Tool der Regler m setzt, heißt hier w — der übliche Machine-Learning-Name:

ŷ = w·x + b      # w = Steigung (Gewicht), b = Achsenabschnitt (Bias)

Für jeden Datenpunkt ist das Residuum rᵢ = yᵢ − ŷᵢ. "Best Fit" heißt, die Summe der quadrierten Residuen zu minimieren — gleichbedeutend mit dem mittleren quadratischen Fehler:

MSE = (1/n) · Σ (yᵢ − (w·xᵢ + b))²

Warum die Residuen quadrieren? So heben sich positive und negative Fehler nicht auf, große Fehler werden viel härter bestraft als kleine, und es entsteht eine glatte, schüsselförmige Kostenfunktion mit genau einem Tiefpunkt — also gibt es eine eindeutig beste Gerade. Dieser Tiefpunkt hat eine geschlossene Lösung (die Normalengleichungen): für eine Variable gilt w = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)² und b = ȳ − w·x̄ — ohne Suche. Bei vielen Merkmalen oder riesigen Datenmengen rollt man stattdessen mit Gradientenabstieg bergab und schiebt w und b gegen die Steigung des Fehlers, bis er den Tiefpunkt erreicht.

Worauf achten

  • Es nimmt einen linearen Zusammenhang an. Krümmt sich die Wahrheit, liegt eine Gerade systematisch daneben — egal wie du sie fittest.
  • Ausreißer ziehen stark. Weil Fehler quadriert werden, kann ein einziger weit entfernter Punkt die ganze Gerade kippen — schau dir die Daten zuerst an.
  • Korrelation ist nicht Kausalität. Ein guter Fit heißt, x sagt y vorher — nicht, dass x y verursacht; eine versteckte dritte Variable kann beides treiben.
  • R² ehrlich lesen. Es ist der Anteil der Varianz, den die Gerade erklärt (0–1), aber ein hohes R² beweist nicht, dass das Modell stimmt — und sagt nichts über Punkte außerhalb des Datenbereichs.

Mehr dazu

Next up
AI & Machine LearningLogistic Regression & Sigmoid AI & Machine LearningDecision Tree All concepts →