【问题标题】:Python ValueError: non-broadcastable output operand with shape (124,1) doesn't match the broadcast shape (124,13)Python ValueError:形状(124,1)的不可广播输出操作数与广播形状(124,13)不匹配
【发布时间】:2017-01-16 05:29:13
【问题描述】:

我想在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. 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)

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

【问题讨论】:

  • 当你有形状错误时,你需要做的第一件事是显示所有进入你的问题的数组的形状在这种情况下X_trainX_test,可能更多。跨度>

标签: python python-2.7 numpy scikit-learn


【解决方案1】:

必须按照与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])

【讨论】:

  • 所以他正在训练 13 个功能集,并测试 1 个功能集。这解释了不寻常的错误消息。 sklearn 问题中的形状错误很常见,但涉及non-broadcastable 的问题则不然。
  • 如果他的密集层与他的特征数量不匹配,那么这也会导致不可广播的错误。
猜你喜欢
  • 1970-01-01
  • 2018-05-09
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2020-08-03
相关资源
最近更新 更多