【发布时间】: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_train和X_test,可能更多。跨度>
标签: python python-2.7 numpy scikit-learn