sklearnでトレーニングテータとテストデータを分けて決定木アルゴリズムにフィットさせようとしたところエラーが起きました。
解決方法がわからないため、もしよかったら指摘よろしくお願いします。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
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>
----> 1 dtree.fit(X_train,y_train)
3 frames
/usr/local/lib/python3.8/dist-packages/sklearn/tree/_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
935 """
936
--> 937 super().fit(
938 X,
939 y,
/usr/local/lib/python3.8/dist-packages/sklearn/tree/_classes.py in fit(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/python3.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
--> 578 X = check_array(X, **check_X_params)
579 y = check_array(y, **check_y_params)
580 else:
/usr/local/lib/python3.8/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
767 # If input is 1D raise error
768 if array.ndim == 1:
--> 769 raise ValueError(
770 "Expected 2D array, got 1D array instead:\narray={}.\n"
771 "Reshape your data either using array.reshape(-1, 1) if "
ValueError: Expected 2D array, got 1D array instead:
array=[34.2 54.8 47.1 69.7 61.9 51.1 48.3 69.7 55.2 68.5 49.2 61.9 66. 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 either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.