TheAlgorithms/Python · error · ValueError
Input data set must be one-dimensional
Error message
Input data set must be one-dimensional
What it means
Raised by DecisionTree.train when the x argument is not a one-dimensional numpy array. This regression tree splits on a single scalar feature, so a 2D x (multiple features or a column vector shaped (n, 1)) is rejected up front.
Source
Thrown at machine_learning/decision_tree.py:84
3. Try to train when x and y are not of the same length
>>> dt = DecisionTree()
>>> dt.train(np.array([1,2,3,4,5]),np.array([[0,0,0,1,1],[0,0,0,1,1]]))
Traceback (most recent call last):
...
ValueError: x and y have different lengths
4. Try to train when x & y are of the same length but different dimensions
>>> dt = DecisionTree()
>>> dt.train(np.array([1,2,3,4,5]),np.array([[1],[2],[3],[4],[5]]))
Traceback (most recent call last):
...
ValueError: Data set labels must be one-dimensional
This section is to check that the inputs conform to our dimensionality
constraints
"""
if x.ndim != 1:
raise ValueError("Input data set must be one-dimensional")
if len(x) != len(y):
raise ValueError("x and y have different lengths")
if y.ndim != 1:
raise ValueError("Data set labels must be one-dimensional")
if len(x) < 2 * self.min_leaf_size:
self.prediction = np.mean(y)
return
if self.depth == 1:
self.prediction = np.mean(y)
return
best_split = 0
min_error = self.mean_squared_error(x, np.mean(y)) * 2
"""
loop over all possible splits for the decision tree. find the best split.View on GitHub (pinned to f5988cc097)
Solutions
- Flatten x to 1D: x = np.asarray(x).ravel() or use df['col'].values.
- Select a single feature column explicitly before training.
- For multivariate features you need a different model; this class only supports one scalar input dimension.
Example fix
# before dt.train(df[['height']].values, y) # after dt.train(df['height'].values.ravel(), y)
Defensive patterns
Strategy: type-guard
Validate before calling
x = np.asarray(x)
if x.ndim != 1:
x = x.ravel()
dt.train(x, y) Type guard
def is_1d_array(a) -> bool:
return isinstance(a, np.ndarray) and a.ndim == 1 Try / catch
try:
dt.train(x, y)
except ValueError as e:
if "one-dimensional" in str(e) and x.ndim != 1:
dt.train(np.asarray(x).ravel(), y)
else:
raise Prevention
- Pass df['col'].values, never df[['col']].values.
- ravel() features at the API boundary.
- Remember this tree is single-feature only.
When it happens
Trigger: Calling dt.train(np.array([[1], [2], [3]]), y) with a column-vector x, or passing a 2D feature matrix with several columns as produced by most sklearn-style pipelines.
Common situations: Loading data with pandas and passing df[['feature']].values (shape (n, 1)) instead of df['feature'].values (shape (n,)), or adapting code from scikit-learn which expects 2D X.
Related errors
- Data set labels must be one-dimensional
- Shape of y_true and y_pred must be the same.
- Data must have dimensions N x 1
- 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/1b9f034ef6a7d0b4.
Report an issue: GitHub.