{"record":{"id":"44f686929f5985ef","repo":"TheAlgorithms/Python","slug":"design-matrix-is-not-full-rank-can-t-compute-coef","errorCode":null,"errorMessage":"Design matrix is not full rank, can't compute coefficients","messagePattern":"Design matrix is not full rank, can't compute coefficients","errorType":"exception","errorClass":"ArithmeticError","httpStatus":null,"severity":"error","filePath":"machine_learning/polynomial_regression.py","lineNumber":145,"sourceCode":"        array([-5.,  3., -2.,  1.])\n        >>> poly_reg = PolynomialRegression(degree=20)\n        >>> poly_reg.fit(x, y)\n        Traceback (most recent call last):\n        ...\n        ArithmeticError: Design matrix is not full rank, can't compute coefficients\n\n        Make sure errors don't grow too large:\n        >>> coefs = np.array([-250, 50, -2, 36, 20, -12, 10, 2, -1, -15, 1])\n        >>> y = PolynomialRegression._design_matrix(x, len(coefs) - 1) @ coefs\n        >>> poly_reg = PolynomialRegression(degree=len(coefs) - 1)\n        >>> poly_reg.fit(x, y)\n        >>> np.allclose(poly_reg.params, coefs, atol=10e-3)\n        True\n        \"\"\"\n        X = PolynomialRegression._design_matrix(x_train, self.degree)  # noqa: N806\n        _, cols = X.shape\n        if np.linalg.matrix_rank(X) < cols:\n            raise ArithmeticError(\n                \"Design matrix is not full rank, can't compute coefficients\"\n            )\n\n        # np.linalg.pinv() computes the Moore-Penrose pseudoinverse using SVD\n        self.params = np.linalg.pinv(X) @ y_train\n\n    def predict(self, data: np.ndarray) -> np.ndarray:\n        \"\"\"\n        Computes the predicted response values y for the given input data by\n        constructing the design matrix X and evaluating y = Xβ.\n\n        @param data:    the predictor values x for prediction\n        @returns:       the predicted response values y = Xβ\n        @raises ArithmeticError:    if this function is called before the model\n                                    parameters are fit\n\n        >>> x = np.array([0, 1, 2, 3, 4])\n        >>> y = x**3 - 2 * x**2 + 3 * x - 5","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/polynomial_regression.py#L127-L163","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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()."],"exampleFix":"# before\nx = np.array([1.0, 2.0, 3.0])\nmodel = PolynomialRegression(degree=5)\nmodel.fit(x, y)   # 3 points cannot support 6 coefficients\n\n# after\nmodel = PolynomialRegression(degree=2)\nmodel.fit(x, y)   # 3 points, 3 coefficients: full rank","handlingStrategy":"validation","validationCode":"x = np.asarray(x_train).ravel()\nassert np.isfinite(x).all(), \"NaN in x_train\"\nassert len(np.unique(x)) >= model.degree + 1, \"not enough distinct points for this degree\"\nmodel.fit(x, y_train)","typeGuard":"def design_full_rank(x: np.ndarray, degree: int) -> bool:\n    X = np.vander(np.asarray(x).ravel(), N=degree + 1, increasing=True)\n    return np.isfinite(X).all() and np.linalg.matrix_rank(X) == degree + 1","tryCatchPattern":"try:\n    model.fit(x_train, y_train)\nexcept ArithmeticError as e:\n    if \"full rank\" in str(e):\n        model = PolynomialRegression(min(model.degree, len(np.unique(x_train)) - 1))\n        model.fit(x_train, y_train)\n    else:\n        raise","preventionTips":["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."],"tags":["machine-learning","regression","linear-algebra","rank-deficiency"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}