TheAlgorithms/Python · error · ValueError
Data must have dimensions N x 1
Error message
Data must have dimensions N x 1
What it means
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.
Source
Thrown at machine_learning/polynomial_regression.py:98
array([[1, 0],
[1, 1],
[1, 2]])
>>> PolynomialRegression._design_matrix(x, degree=2)
array([[1, 0, 0],
[1, 1, 1],
[1, 2, 4]])
>>> PolynomialRegression._design_matrix(x, degree=3)
array([[1, 0, 0, 0],
[1, 1, 1, 1],
[1, 2, 4, 8]])
>>> PolynomialRegression._design_matrix(np.array([[0, 0], [0 , 0]]), degree=3)
Traceback (most recent call last):
...
ValueError: Data must have dimensions N x 1
"""
_rows, *remaining = data.shape
if remaining:
raise ValueError("Data must have dimensions N x 1")
return np.vander(data, N=degree + 1, increasing=True)
def fit(self, x_train: np.ndarray, y_train: np.ndarray) -> None:
"""
Computes the polynomial regression model parameters using ordinary least squares
(OLS) estimation:
β = (XᵀX)⁻¹Xᵀy = X⁺y
where X⁺ denotes the Moore-Penrose pseudoinverse of the design matrix X. This
function computes X⁺ using singular value decomposition (SVD).
References:
- https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse
- https://en.wikipedia.org/wiki/Singular_value_decomposition
- https://en.wikipedia.org/wiki/Multicollinearity
View on GitHub (pinned to f5988cc097)
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']].
Example fix
# before x = df[['x']].to_numpy() # shape (N, 1) model.fit(x, y) # after x = df['x'].to_numpy() # shape (N,) model.fit(x, y)
Defensive patterns
Strategy: validation
Validate before calling
x = np.asarray(x).ravel() assert x.ndim == 1 model.fit(x, y)
Type guard
def is_1d_array(data: np.ndarray) -> bool:
return isinstance(data, np.ndarray) and data.ndim == 1 Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Input data set must be one-dimensional
- Data set labels must be one-dimensional
- Shape of y_true and y_pred must be the same.
- Expected the same number of rows for A and B. Instead found
- Expected the same number of columns for B and C. Instead fou
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e223741b593735e5.
Report an issue: GitHub.