gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:16:02 24 4
gpt4 key购买 nike

我想在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com