前提・実現したいこと
Wineデータセットを読み込み、MinMaaxスケーリングをしたい。
発生している問題・エラーメッセージ
ValueError Traceback (most recent call last) <ipython-input-163-4541c75c7333> in <module> 1 from sklearn.preprocessing import MinMaxScaler 2 mms = MinMaxScaler() ----> 3 X_train_norm = mms.fit_transform(X_train) 4 X_test_norm = mms.transform(X_test) ~\anaconda3\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params) 569 if y is None: 570 # fit method of arity 1 (unsupervised transformation) --> 571 return self.fit(X, **fit_params).transform(X) 572 else: 573 # fit method of arity 2 (supervised transformation) ~\anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in fit(self, X, y) 337 # Reset internal state before fitting 338 self._reset() --> 339 return self.partial_fit(X, y) 340 341 def partial_fit(self, X, y=None): ~\anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in partial_fit(self, X, y) 371 X = check_array(X, 372 estimator=self, dtype=FLOAT_DTYPES, --> 373 force_all_finite="allow-nan") 374 375 data_min = np.nanmin(X, axis=0) ~\anaconda3\lib\site-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, warn_on_dtype, estimator) 554 "Reshape your data either using array.reshape(-1, 1) if " 555 "your data has a single feature or array.reshape(1, -1) " --> 556 "if it contains a single sample.".format(array)) 557 558 # in the future np.flexible dtypes will be handled like object dtypes ValueError: Expected 2D array, got 1D array instead: array=[13.71 12.22 13.27 13.16 13.86 12.85 13.84 13.3 13.05 12.51 12.29 12.77 12.96 13.67 13.16 12.37 12.47 11.81 13.24 14.1 11.61 12.99 12.77 13.48 11.46 12.07 11.82 13.45 12.2 13.05 13.17 12.08 12.17 12.42 11.87 12.21 13.94 14.1 12.67 14.75 12.25 12.85 13.73 14.06 13.63 12.33 13.82 12.08 13.03 14.37 13.17 12.72 14.39 13.34 11.66 11.84 12.86 13.75 12.53 13.11 14.38 14.23 12. 13.23 14.38 14.12 12. 12.34 13.48 13.29 13.41 13.71 12.37 13.2 11.45 13.62 13.88 12.42 12.81 12.58 13.83 13.07 12.7 13.77 12.84 12.37 13.51 13.87 12.08 13.58 13.08 11.79 12.45 13.68 13.52 13.5 12.87 14.02 12.29 12.08 12.7 11.03 13.32 14.13 13.49 11.84 13.05 12.72 12.82 13.4 14.22 13.72 12.93 11.64 12.29 11.65 13.28 12.93 13.86 11.82 12.37 12.42 13.9 14.16]. 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.
該当のソースコード
Python
1df_wine = pd.read_csv( 2 'https://raw.githubusercontent.com/tirthajyoti/Machine-Learning-with-Python/master/Datasets/wine.data.csv', header = 0) 3 4df_wine.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash', 5 'Alcalinity of ash', 'Magnesium', 'Total phenols', 'Flavanoids', 6 'Nonflavanoid phenols', 'Proanthocyanis', 'Color intenesity', 'Hue', 7 'OD280/OD315 of diluted wines', 'Proline'] 8 9 10print('Class labels', np.unique(df_wine['Class label'])) 11df_wine.head() 12 13from sklearn.model_selection import train_test_split 14X, y = df_wine.iloc[:, 1].values, df_wine.iloc[:, 0].values 15 16X_train, X_test, y_train, y_test = \ 17 train_test_split(X, y, test_size = 0.3, random_state = 0) 18 19from sklearn.preprocessing import MinMaxScaler 20mms = MinMaxScaler() 21X_train_norm = mms.fit_transform(X_train) 22X_test_norm = mms.transform(X_test)
試したこと
教科書(Python machine learning 2nd edition)では、random_state = 0の直後にstratify = y、'~ csv'の直後にheader = Noneが入っており、それぞれエラーが発生していましたが、それは削除することで解決しました。しかし、このエラーだけはどのように解決すればよいのかがわかりません。
わかる方、お願いします。
補足情報(ツールのバージョンなど)
anaconda JupiterLab 1.2.6
回答1件
あなたの回答
tips
プレビュー