我想使用MinMaxScaler中的sklearn.preprocessing归一化训练和测试数据集。但是,该软件包似乎不接受我的测试数据集。

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)


执行此操作时,我得到一个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.和一个ValueError: operands could not be broadcast together with shapes (124,) (13,) (124,)

重塑数据仍然会产生错误。

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)

关于如何解决此错误的任何输入将有所帮助。

最佳答案

必须按照与train_test_split()函数的输入数组相同的顺序指定火车/测试数据的分区,以便根据该顺序解压缩它们。

显然,当订单指定为X_train, y_train, X_test, y_test时,y_trainlen(y_train)=54)和X_testlen(X_test)=124)的结果形状被交换,从而导致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])

关于python - Python ValueError:形状为(124,1)的不可广播的输出操作数与广播形状(124,13)不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41669995/

10-11 06:22