{"record":{"id":"e223741b593735e5","repo":"TheAlgorithms/Python","slug":"data-must-have-dimensions-n-x-1","errorCode":null,"errorMessage":"Data must have dimensions N x 1","messagePattern":"Data must have dimensions N x 1","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/polynomial_regression.py","lineNumber":98,"sourceCode":"        array([[1, 0],\n               [1, 1],\n               [1, 2]])\n        >>> PolynomialRegression._design_matrix(x, degree=2)\n        array([[1, 0, 0],\n               [1, 1, 1],\n               [1, 2, 4]])\n        >>> PolynomialRegression._design_matrix(x, degree=3)\n        array([[1, 0, 0, 0],\n               [1, 1, 1, 1],\n               [1, 2, 4, 8]])\n        >>> PolynomialRegression._design_matrix(np.array([[0, 0], [0 , 0]]), degree=3)\n        Traceback (most recent call last):\n        ...\n        ValueError: Data must have dimensions N x 1\n        \"\"\"\n        _rows, *remaining = data.shape\n        if remaining:\n            raise ValueError(\"Data must have dimensions N x 1\")\n\n        return np.vander(data, N=degree + 1, increasing=True)\n\n    def fit(self, x_train: np.ndarray, y_train: np.ndarray) -> None:\n        \"\"\"\n        Computes the polynomial regression model parameters using ordinary least squares\n        (OLS) estimation:\n\n        β = (XᵀX)⁻¹Xᵀy = X⁺y\n\n        where X⁺ denotes the Moore-Penrose pseudoinverse of the design matrix X. This\n        function computes X⁺ using singular value decomposition (SVD).\n\n        References:\n            - https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse\n            - https://en.wikipedia.org/wiki/Singular_value_decomposition\n            - https://en.wikipedia.org/wiki/Multicollinearity\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/polynomial_regression.py#L80-L116","documentation":"Thrown by PolynomialRegression._design_matrix when the input data is not a 1-D array of shape (N,). The design matrix is built with np.vander over a flat vector of predictor values; any 2-D input like (N, 2) has no defined polynomial expansion here and is rejected by unpacking data.shape and checking for leftover dimensions.","triggerScenarios":"Passing np.array([[0, 0], [0, 0]]) or any (N, M) matrix with M > 1 to _design_matrix, fit, or predict; passing a column vector of shape (N, 1) also raises because a second dimension remains.","commonSituations":"Multivariate feature matrices fed to a univariate fitter; sklearn-style column vectors (N, 1) not flattened; DataFrame column selected with double brackets producing 2-D.","solutions":["Flatten the input: data = np.asarray(data).ravel() before calling fit/predict.","For multivariate inputs, use a multivariate regression method instead of this class.","Select DataFrame columns with a single bracket: df['x'] not df[['x']]."],"exampleFix":"# before\nx = df[['x']].to_numpy()          # shape (N, 1)\nmodel.fit(x, y)\n\n# after\nx = df['x'].to_numpy()            # shape (N,)\nmodel.fit(x, y)","handlingStrategy":"validation","validationCode":"x = np.asarray(x).ravel()\nassert x.ndim == 1\nmodel.fit(x, y)","typeGuard":"def is_1d_array(data: np.ndarray) -> bool:\n    return isinstance(data, np.ndarray) and data.ndim == 1","tryCatchPattern":null,"preventionTips":["Flatten inputs with .ravel() at the API boundary of your training script.","Select pandas columns with single brackets to keep Series 1-D.","Reserve multivariate feature matrices for multivariate regressors."],"tags":["machine-learning","regression","numpy","shape-mismatch"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}