93532


sklearnでトレーニングテータとテストデータを分けて決定木アルゴリズムにフィットさせようとしたところエラーが起きました。  
解決方法がわからないため、もしよかったら指摘よろしくお願いします。

[code]

    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)
    
[/code]

[code]

    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.
    
[/code]


CC BY-SA 4.0
2023-01-23T04:36:32.457
93516
2023-01-23T15:37:38.680
2023-01-23T05:42:16.223
3060
56639
question
python 機械学習 scikit-learn
scikit-learn でエラー: ValueError: Expected 2D array, got 1D array instead:


実際のデータをみない限り診断しにくいですが、１つの特徴量でフィットしようとしているようですので、エラーメッセージの中にある「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.」という指示に従って`dtree.fit(X_train,y_train)`を実行する前に

[code]

    X_train = X_train.reshape(1, -1)
    
[/code]

を実行すれば良いのではないかと思われます。


CC BY-SA 4.0
2023-01-23T05:31:31.190
93518
2023-01-23T05:31:31.190
51363
93516
answer


エラーの内容から推測すると，`X` をデータフレームに変換すればエラーは生じなくなると思われます。

[code]

    # X = df.external
    X = pd.DataFrame(df.external)
    
[/code]

御参考として Iris データを基に類似の状況を作りエラーが発生する場合と（`X`
をデータフレームに変換して）エラーが発生しなくなる場合を切り替えられる記述例を示します。12行目と13行目のコメントアウトで切り替えられます。

[code]

    import pandas as pd
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.metrics import accuracy_score
    
    iris = load_iris()
    df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
    df['target'] = iris['target']
    # print(f'df:\n{df}')
    
    X = df['petal width (cm)']  # error occurs
    # X = pd.DataFrame(df['petal width (cm)'])  # no error occurs
    y = df['target']
    
    X_tv, X_test, y_tv, y_test = train_test_split(X, y, test_size=0.1,
                                                  random_state=11)
    X_train, X_val,  y_train, y_val = train_test_split(X_tv, y_tv, test_size=0.5,
                                                       random_state=11)
    dtree=DecisionTreeClassifier(random_state=0)
    dtree.fit(X_train, y_train)
    acc = accuracy_score(y_test, dtree.predict(X_test))
    print(f'accuracy: {acc:.4f}')
    
[/code]


CC BY-SA 4.0
2023-01-23T15:27:30.277
93532
2023-01-23T15:37:38.680
2023-01-23T15:37:38.680
54588
54588
93516
answer