Error on scikit-learn: ValueError: Expected 2D array, got1D array installed:

Asked 1 years ago, Updated 1 years ago, 472 views

An error occurred when sklearn tried to fit the decision tree algorithm by separating the training data from the test data.
I don't know how to solve it, so please point it out if you like.

import numpy as np
import pandas aspd
import matplotlib.pyplot asplt
import seaborn as sns
import csv
csvfile=open('BioAsseT practice data5.csv')
df = pd.read_csv("BioAsseT practice data5.csv")
X = df.external
y = df.total
X.shape, y.shape
from sklearn.model_selection import train_test_split
X_train_val, X_test, y_train_val, y_test=train_test_split(X,y,test_size=0.1, random_state=1)
X_train, X_val, y_train, y_val, =train_test_split(X_train_val, y_train_val, test_size=0.5, random_state=1)
from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier(random_state=0)

dtree.fit(X_train,y_train)
ValueError Traceback (most recent call last)
<ipython-input-44-97e56a26e4dc>in<module>
---->1dtree.fit(X_train,y_train)

3frames
/usr/local/lib/python 3.8/dist-packages/sklearn/tree/_classes.py input (self, X, y, sample_weight, check_input, X_idx_sorted)
    935         """
    936 
-->937 super().fit(
    938 X,
    939 y,

/usr/local/lib/python 3.8/dist-packages/sklearn/tree/_classes.py input (self, X, y, sample_weight, check_input, X_idx_sorted)
    163 check_X_params=dict(dtype=DTYPE, accept_sparse="csc")
    164 check_y_params=dict(ensure_2d=False,dtype=None)
-->165 X,y=self._validate_data(
    166 X, y, validate_separately=(check_X_params, check_y_params)
    167             )

/usr/local/lib/python 3.8/dist-packages/sklearn/base.py in_validate_data(self, X, y, reset, validate_separately, **check_params)
    576                 # :(
    577 check_X_params, check_y_params=validate_separately
-->578X=check_array(X,**check_X_params)
    579y=check_array(y,**check_y_params)
    580 else:

/usr/local/lib/python 3.8/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, enable_2d, allow_nd, enable_min_sample, feature_element)
    767#If input is 1D raise error
    768 if array.ndim == 1:
-->769 raise ValueError(
    770 "Expected 2D array, got 1D array installed:\narray={}.\n"
    771 "Reshape your data ether using array.reshape(-1,1) if"

ValueError: Expected 2Darray, got1Darray installed:
array=[34.254.847.169.761.951.148.369.755.268.549.261.966.61.8
 51.1 79.7 58.8 67.6 55.2 48.7 67.  56.2 62.5 48.  65.6 40.8 27.6 44.6
 31.6 43.7 40.1 69.7 61.2 50.  70.3 70.  53.3 53.8 42.4 47.1 40.9 74.3
 62.9 61.5 70.9 32.8].
Reshape your data ether using array.reshape(-1,1) if your data has a single feature or array.reshape(1,-1) if it contains a single sample.

python machine-learning scikit-learn

2023-01-23 23:19

1 Answers

It's hard to diagnose unless you look at the actual data, but it seems like you're trying to fit with one feature, so follow the instructions in the error message Reshape your data ether using array.reshape(-1,1) if your data has a single feature or array.reshape(1,-1) if it contains a single sample.

X_train=X_train.reshape(1,-1)

You may want to run .


2023-01-23 23:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.