Linear Regression Calculator
Fit a linear model to your own data: exact coefficients from the normal equations, predictions, residuals, MSE, and R², with optional ridge regularization and a line you can drag points under.
Data
One sample per row. The last column is the target the model predicts; the others are features, up to 6. Type, paste from a spreadsheet or CSV, or start from an example.
Model
Updates as you type.
8 samples, 2 features, and an intercept.
Ridge adds λ times the sum of the squared coefficients to the error, which pulls every coefficient toward 0. The intercept is left alone.
Coefficients
- Intercept
- ≈84.174
- size (m²)
- ≈2.3614
- age (years)
- ≈-1.958
Each coefficient is how much the prediction changes when that feature goes up by 1 and the others stay the same.
How well it fits
- Mean squared error
- Root mean squared error
- R²
The model explains 99.9% of the variation in price (k$).
Predict
Enter feature values for a new sample.
Predictions and residuals
Coefficients are exact in fractions view. Decimals are rounded to 6 places.
A model is a matrix times a vector
Linear regression predicts a target from features with one weight per feature plus an intercept. Stack the samples as rows of , put a column of ones in front for the intercept, and every prediction at once is one product:
The best makes the residuals as small as possible in total squared error. Setting the derivative of that error to zero gives the normal equations:
That's a small square system, one row and column per coefficient, however many samples there are. This calculator solves it exactly with row reduction, so the coefficients come out as fractions when the data are whole numbers. Geometrically, is the projection of onto the column space of , which the Least Squares Calculator draws.
Reading the result
- Coefficients. Each one is how much the prediction moves when its feature goes up by 1 and every other feature stays put. The units matter: a coefficient on size in square meters and one on age in years aren't comparable until the features are on the same scale.
- MSE and RMSE. The mean squared error averages the squared residuals. Its square root, RMSE, is in the target's own units, so it reads as a typical miss.
- R². One minus the squared error left over, divided by the target's total variation around its mean. means the model accounts for 90% of the variation. Adding features never lowers R² on the data you fit, so a high value alone doesn't mean the model will predict new data well.
When features repeat each other, and what ridge does
If one feature is an exact combination of others, like height in centimeters and height in decimeters, is singular. Infinitely many coefficient sets then fit equally well, and the "Repeated feature" example shows this. The Matrix Inverse Calculator explains why a singular matrix has no inverse.
Ridge regression adds to the diagonal of , everywhere except the intercept's entry:
That's the same as adding to the error. For any the system has exactly one solution, and every coefficient is pulled toward zero. In machine learning this is L2 regularization, or weight decay. It trades a little error on the training data for a model that's steadier on new data. Ridge depends on the units of each feature, which is why features are usually standardized first.
Related calculators
The linear algebra underneath, and more of the machine learning section.
Least Squares Calculator
Fit a line through points or solve Ax ≈ b, exactly. Drag points and watch the residual squares shrink, follow the normal equations step by step, and try fitting a line by hand.
Matrix Inverse Calculator
Invert a square matrix with exact fractions, step through [A | I] to [I | A⁻¹], and see the inverse undo the transformation.
Matrix Transpose Calculator
Transpose matrices up to 12×12 in exact fractions, watch rows become columns, and check whether a matrix is symmetric or skew-symmetric.
Covariance Matrix Calculator
Paste up to 100 samples of 6 features and get the means, centered data, exact covariance matrix, and correlation matrix, with a draggable scatter and covariance ellipse.