Python ValueError : non-broadcastable output operand with shape (124, 1) 与广播形状不匹配 (124,13)

标签 python python-2.7 numpy scikit-learn

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

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.如果您的数据具有单个特征,则使用 X.reshape(-1, 1) 或如果它包含单个样本,则使用 X.reshape(1, -1) reshape 您的数据。 以及 ValueError:操作数不能与形状一起广播 (124,) (13,) (124,)

reshape 数据仍然会产生错误。

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

此 reshape 会产生错误ValueError:形状为 (124,1) 的不可广播输出操作数与广播形状 (124,13) 不匹配

有关如何修复此错误的任何输入都会有所帮助。

最佳答案

训练/测试数据的分区必须按照与 train_test_split() 的输入数组相同的顺序指定函数让它按照那个顺序解压它们。

显然,当顺序指定为 X_train, y_train, X_test, y_test 时,y_train 的结果形状 (len(y_train)=54) 和 X_test (len(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 ValueError : non-broadcastable output operand with shape (124, 1) 与广播形状不匹配 (124,13),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41669995/

相关文章:

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int' (Python)

python - 按列减去 Numpy 数组

python - 访问冲突 (0xC0000005) NumPy 数组

python - 在 numpy 数组中以不规则的时间间隔加速移动时间增量

python - 为梯度直方图获取 0 到 180 之间无符号角度的正确方法

python - 如何重新配置​​ pandas 数据框?

python - 如何以 block 的形式旋转现有数据框?

python - 在 3 个列表的列表中搜索 2 个列表的等价性,并对第三个列表中列表 1 和 2 相等的所有值求平均值 (Python)

python - 将脚本转换为不同版本的 Python

python-2.7 - 如何在 Selenium 中导航到新网页?