前提・実現したいこと
wineデータをL1正規化付きロジスティック回帰を適用したい。
発生している問題・エラーメッセージ
C:\Users\anaconda3\lib\site-packages\sklearn\utils\validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel(). y = column_or_1d(y, warn=True) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-144-5bb30b05e054> in <module> 1 lr = LogisticRegression(penalty = 'l1', C = 1.0, solver = 'liblinear') 2 lr.fit(X_train_std, y_train) ----> 3 print('Training accuracy:', lr.score(X_test_std, y_train)) 4 print('Test accuracy:', lr.score(X_test_std, y_test)) ~\anaconda3\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight) 367 """ 368 from .metrics import accuracy_score --> 369 return accuracy_score(y, self.predict(X), sample_weight=sample_weight) 370 371 ~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in accuracy_score(y_true, y_pred, normalize, sample_weight) 183 184 # Compute accuracy for each possible representation --> 185 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 186 check_consistent_length(y_true, y_pred, sample_weight) 187 if y_type.startswith('multilabel'): ~\anaconda3\lib\site-packages\sklearn\metrics\_classification.py in _check_targets(y_true, y_pred) 78 y_pred : array or indicator matrix 79 """ ---> 80 check_consistent_length(y_true, y_pred) 81 type_true = type_of_target(y_true) 82 type_pred = type_of_target(y_pred) ~\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays) 210 if len(uniques) > 1: 211 raise ValueError("Found input variables with inconsistent numbers of" --> 212 " samples: %r" % [int(l) for l in lengths]) 213 214 ValueError: Found input variables with inconsistent numbers of samples: [124, 54] [ ]:
該当のソースコード
Python
1import pandas as pd 2import numpy as np 3df_wine = pd.read_csv( 4 'https://raw.githubusercontent.com/tirthajyoti/Machine-Learning-with-Python/master/Datasets/wine.data.csv', header = 0) 5 6df_wine.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash', 7 'Alcalinity of ash', 'Magnesium', 'Total phenols', 'Flavanoids', 8 'Nonflavanoid phenols', 'Proanthocyanis', 'Color intenesity', 'Hue', 9 'OD280/OD315 of diluted wines', 'Proline'] 10 11 12print('Class labels', np.unique(df_wine['Class label'])) 13df_wine.head() 14 15from sklearn.model_selection import train_test_split 16X, y = df_wine.iloc[:, 1].values, df_wine.iloc[:, 0].values 17 18X_train, X_test, y_train, y_test = \ 19 train_test_split(X.reshape(-1, 1), y.reshape(-1, 1), test_size = 0.3, random_state = 0, stratify = y) 20 21from sklearn.preprocessing import MinMaxScaler 22mms = MinMaxScaler() 23X_train_norm = mms.fit_transform(X_train) 24X_test_norm = mms.transform(X_test) 25 26ex = np.array([0, 1, 2, 3, 4, 5]) 27print('standardized:', (ex - ex.mean()) / ex.std()) 28print('normalized:', (ex - ex.min()) / (ex.max() - ex.min())) 29 30from sklearn.preprocessing import StandardScaler 31stdsc = StandardScaler() 32X_train_std = stdsc.fit_transform(X_train) 33X_test_std = stdsc.transform(X_test) 34 35from sklearn.linear_model import LogisticRegression 36LogisticRegression(penalty = 'l1') 37 38lr = LogisticRegression(penalty = 'l1', C = 1.0, solver = 'liblinear') 39lr.fit(X_train_std, y_train) 40print('Training accuracy:', lr.score(X_test_std, y_train)) 41print('Test accuracy:', lr.score(X_test_std, y_test))
試したこと
教科書が発売された時から、若干の仕様変更が行われているみたいなので、なかなかうまくいきません。わかる方お願いします。
補足情報(ツールのバージョンなど)
anaconda JupiterLab 1.2.6
回答1件
あなたの回答
tips
プレビュー