TheAlgorithms/Python · error · ArithmeticError
Design matrix is not full rank, can't compute coefficients
Error message
Design matrix is not full rank, can't compute coefficients
What it means
Raised as ArithmeticError by PolynomialRegression.fit when the design matrix X (Vandermonde matrix of the training x values) is not full column rank. OLS via the pseudoinverse is only well-determined when the rank equals degree+1; duplicate x values, too few samples (N <= degree), or NaN inputs make the columns linearly dependent.
Source
Thrown at machine_learning/polynomial_regression.py:145
array([-5., 3., -2., 1.])
>>> poly_reg = PolynomialRegression(degree=20)
>>> poly_reg.fit(x, y)
Traceback (most recent call last):
...
ArithmeticError: Design matrix is not full rank, can't compute coefficients
Make sure errors don't grow too large:
>>> coefs = np.array([-250, 50, -2, 36, 20, -12, 10, 2, -1, -15, 1])
>>> y = PolynomialRegression._design_matrix(x, len(coefs) - 1) @ coefs
>>> poly_reg = PolynomialRegression(degree=len(coefs) - 1)
>>> poly_reg.fit(x, y)
>>> np.allclose(poly_reg.params, coefs, atol=10e-3)
True
"""
X = PolynomialRegression._design_matrix(x_train, self.degree) # noqa: N806
_, cols = X.shape
if np.linalg.matrix_rank(X) < cols:
raise ArithmeticError(
"Design matrix is not full rank, can't compute coefficients"
)
# np.linalg.pinv() computes the Moore-Penrose pseudoinverse using SVD
self.params = np.linalg.pinv(X) @ y_train
def predict(self, data: np.ndarray) -> np.ndarray:
"""
Computes the predicted response values y for the given input data by
constructing the design matrix X and evaluating y = Xβ.
@param data: the predictor values x for prediction
@returns: the predicted response values y = Xβ
@raises ArithmeticError: if this function is called before the model
parameters are fit
>>> x = np.array([0, 1, 2, 3, 4])
>>> y = x**3 - 2 * x**2 + 3 * x - 5View on GitHub (pinned to f5988cc097)
Solutions
- Increase the number of distinct training points: keep at least degree+1 unique x values.
- Lower the degree so degree + 1 <= number of unique x values.
- Check for duplicates or NaNs: len(np.unique(x_train)) >= self.degree + 1 and np.isfinite(x_train).all().
Example fix
# before x = np.array([1.0, 2.0, 3.0]) model = PolynomialRegression(degree=5) model.fit(x, y) # 3 points cannot support 6 coefficients # after model = PolynomialRegression(degree=2) model.fit(x, y) # 3 points, 3 coefficients: full rank
Defensive patterns
Strategy: validation
Validate before calling
x = np.asarray(x_train).ravel() assert np.isfinite(x).all(), "NaN in x_train" assert len(np.unique(x)) >= model.degree + 1, "not enough distinct points for this degree" model.fit(x, y_train)
Type guard
def design_full_rank(x: np.ndarray, degree: int) -> bool:
X = np.vander(np.asarray(x).ravel(), N=degree + 1, increasing=True)
return np.isfinite(X).all() and np.linalg.matrix_rank(X) == degree + 1 Try / catch
try:
model.fit(x_train, y_train)
except ArithmeticError as e:
if "full rank" in str(e):
model = PolynomialRegression(min(model.degree, len(np.unique(x_train)) - 1))
model.fit(x_train, y_train)
else:
raise Prevention
- Cap degree at unique_points - 1 when sweeping polynomial degrees.
- Check for duplicated or constant predictor values before fitting.
- Grow the dataset or shrink the degree when the error appears; rank deficiency is a data problem, not a numerical one.
When it happens
Trigger: Calling fit with fewer distinct x values than degree+1 (e.g. degree=3 with 3 data points), repeated x values dominating the sample, or degree set higher than the data can support.
Common situations: Overfitting experiments sweeping degree too high for small datasets; constant or near-constant predictor columns; duplicated rows after resampling; accidental grouping that collapses x to few unique values.
Related errors
- Polynomial degree must be non-negative
- Data must have dimensions N x 1
- Predictor hasn't been fit yet
- determinant modular {req_l} of encryption key({det}) is not
- Coefficient matrix dimensions must be nxn but received {rows
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/44f686929f5985ef.
Report an issue: GitHub.