2017-01-16 17 views
1

sklearn.preprocessingMinMaxScalerを使用してトレーニングとテストデータセットを正規化します。ただし、パッケージはテストデータセットを受け入れていないようです。これを実行するとPython ValueError:シェイプ(124,1)の出力不可オペランドがブロードキャストシェイプと一致しません。(124,13)

import pandas as pd 
import numpy as np 

# Read in data. 
df_wine = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data', 
         header=None) 
df_wine.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash', 
        'Alcalinity of ash', 'Magnesium', 'Total phenols', 
        'Flavanoids', 'Nonflavanoid phenols', 'Proanthocyanins', 
        'Color intensity', 'Hue', 'OD280/OD315 of diluted wines', 
        'Proline'] 

# Split into train/test data. 
from sklearn.model_selection import train_test_split 
X = df_wine.iloc[:, 1:].values 
y = df_wine.iloc[:, 0].values 
X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.3, 
                random_state = 0) 

# Normalize features using min-max scaling. 
from sklearn.preprocessing import MinMaxScaler 
mms = MinMaxScaler() 
X_train_norm = mms.fit_transform(X_train) 
X_test_norm = mms.transform(X_test) 

、私はValueError: operands could not be broadcast together with shapes (124,) (13,) (124,)とともにDeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.を取得します。

データを再構成すると、エラーが発生します。

X_test_norm = mms.transform(X_test.reshape(-1, 1)) 

このように再構成すると、エラーValueError: non-broadcastable output operand with shape (124,1) doesn't match the broadcast shape (124,13)が返されます。

どのようにこのエラーを解決するための入力は参考になります。

+0

あなたは形状誤差を持っている場合は、あなたがする必要がある最初の事はでは、あなたの問題に入るすべてのアレイの形状が表示されていますこの場合、 'X_train'と' X_test'はもっと多くなるかもしれません。 – hpaulj

答えて

1

train/testデータの分割は、train_test_split()の入力配列と同じ順序で指定して、その順序に対応する配列を展開する必要があります。 y_trainlen(y_train)=54)とX_testlen(X_test)=124)の順序はX_train, y_train, X_test, y_testとして指定された明らか

、得られた形状はValueErrorもたらす入れ替えてしまいました。

代わりに、あなたは次の条件を満たす必要があります。

# Split into train/test data. 
#     _________________________________ 
#     |  |      \ 
#     |  |       \ 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)           
# |   |         /
# |__________|_____________________________________/ 
# (or) 
# y_train, y_test, X_train, X_test = train_test_split(y, X, test_size=0.3, random_state=0) 

# Normalize features using min-max scaling. 
from sklearn.preprocessing import MinMaxScaler 
mms = MinMaxScaler() 
X_train_norm = mms.fit_transform(X_train) 
X_test_norm = mms.transform(X_test) 

は生成します。

X_train_norm[0] 
array([ 0.72043011, 0.20378151, 0.53763441, 0.30927835, 0.33695652, 
     0.54316547, 0.73700306, 0.25  , 0.40189873, 0.24068768, 
     0.48717949, 1.  , 0.5854251 ]) 

X_test_norm[0] 
array([ 0.72849462, 0.16386555, 0.47849462, 0.29896907, 0.52173913, 
     0.53956835, 0.74311927, 0.13461538, 0.37974684, 0.4364852 , 
     0.32478632, 0.70695971, 0.60566802]) 
+0

彼は13のフィーチャセットについてトレーニングをしており、1つのフィーチャセットでテストしています。これは、異常なエラーメッセージの原因となります。 Sklearnの質問の形の誤りは一般的ですが、「放送不可能」というものは含みません。 – hpaulj

関連する問題