TheAlgorithms/Python · error · ValueError
Data set labels must be one-dimensional
Error message
Data set labels must be one-dimensional
What it means
Raised by DecisionTree.train when the y (labels) argument is not one-dimensional, even if lengths match. The tree computes np.mean(y) for leaf predictions and compares scalar labels against the decision boundary, which only works for shape (n,) arrays.
Source
Thrown at machine_learning/decision_tree.py:88
...
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.
if no split exists that is less than 2 * error for the entire array
then the data set is not split and the average for the entire array is used as
the predictor
"""View on GitHub (pinned to f5988cc097)
Solutions
- Flatten y: y = np.asarray(y).ravel().
- Extract labels as a 1D Series: df['label'].values, not df[['label']].values.
- Add a shared shape check (x.ndim == 1 and y.ndim == 1) in your data-prep function.
Example fix
# before dt.train(x, y.reshape(-1, 1)) # after dt.train(x, y.ravel())
Defensive patterns
Strategy: type-guard
Validate before calling
x = np.asarray(x).ravel() y = np.asarray(y).ravel() dt.train(x, y)
Type guard
def is_1d_pair(x, y) -> bool:
return np.asarray(x).ndim == 1 and np.asarray(y).ndim == 1 Try / catch
try:
dt.train(x, y)
except ValueError as e:
if "one-dimensional" in str(e):
dt.train(np.asarray(x).ravel(), np.asarray(y).ravel())
else:
raise Prevention
- Undo (n, 1) reshapes made for other frameworks before training.
- Use .ravel() on both arrays defensively.
- Keep one canonical 1D representation for labels in the pipeline.
When it happens
Trigger: Calling dt.train(np.array([1,2,3,4,5]), np.array([[1],[2],[3],[4],[5]])) exactly as in the doctest — a column-vector y with shape (n, 1) has len equal to x but ndim == 2.
Common situations: Labels reshaped to (n, 1) for a keras/sklearn pipeline and reused here, or labels loaded via df[['label']].values instead of df['label'].values.
Related errors
- Input data set 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/e25fd69f8868c869.
Report an issue: GitHub.