{"record":{"id":"1b9f034ef6a7d0b4","repo":"TheAlgorithms/Python","slug":"input-data-set-must-be-one-dimensional","errorCode":null,"errorMessage":"Input data set must be one-dimensional","messagePattern":"Input data set must be one-dimensional","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"machine_learning/decision_tree.py","lineNumber":84,"sourceCode":"        3. Try to train when x and y are not of the same length\n        >>> dt = DecisionTree()\n        >>> dt.train(np.array([1,2,3,4,5]),np.array([[0,0,0,1,1],[0,0,0,1,1]]))\n        Traceback (most recent call last):\n            ...\n        ValueError: x and y have different lengths\n\n        4. Try to train when x & y are of the same length but different dimensions\n        >>> dt = DecisionTree()\n        >>> dt.train(np.array([1,2,3,4,5]),np.array([[1],[2],[3],[4],[5]]))\n        Traceback (most recent call last):\n            ...\n        ValueError: Data set labels must be one-dimensional\n\n        This section is to check that the inputs conform to our dimensionality\n        constraints\n        \"\"\"\n        if x.ndim != 1:\n            raise ValueError(\"Input data set must be one-dimensional\")\n        if len(x) != len(y):\n            raise ValueError(\"x and y have different lengths\")\n        if y.ndim != 1:\n            raise ValueError(\"Data set labels must be one-dimensional\")\n\n        if len(x) < 2 * self.min_leaf_size:\n            self.prediction = np.mean(y)\n            return\n\n        if self.depth == 1:\n            self.prediction = np.mean(y)\n            return\n\n        best_split = 0\n        min_error = self.mean_squared_error(x, np.mean(y)) * 2\n\n        \"\"\"\n        loop over all possible splits for the decision tree. find the best split.","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/machine_learning/decision_tree.py#L66-L102","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ndt.train(df[['height']].values, y)\n\n# after\ndt.train(df['height'].values.ravel(), y)","handlingStrategy":"type-guard","validationCode":"x = np.asarray(x)\nif x.ndim != 1:\n    x = x.ravel()\ndt.train(x, y)","typeGuard":"def is_1d_array(a) -> bool:\n    return isinstance(a, np.ndarray) and a.ndim == 1","tryCatchPattern":"try:\n    dt.train(x, y)\nexcept ValueError as e:\n    if \"one-dimensional\" in str(e) and x.ndim != 1:\n        dt.train(np.asarray(x).ravel(), y)\n    else:\n        raise","preventionTips":["Pass df['col'].values, never df[['col']].values.","ravel() features at the API boundary.","Remember this tree is single-feature only."],"tags":["machine-learning","decision-tree","shape-mismatch","numpy"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}