Python DeprecationWarning:传递1d数组作为数据在0.17中被弃用,并将在0.19中引发ValueError

时间:2017-04-18 14:49:08

标签: python pandas scikit-learn deprecation-warning

根据我之前的问题python pandas standardize column for regression,我将数据框中的特定列重新调整为0到1之间。

scaler = preprocessing.MinMaxScaler(feature_range=(0,1))
email['scaled_quantity'] = scaler.fit_transform(email['Quantity'])

不幸的是,我收到此错误

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.
  warnings.warn(DEPRECATION_MSG_1D, DeprecationWarning)

@Grr建议我将缩放应用于整个数据帧,但这不是一个选项。我需要按照它们的方式维护列,并且只想添加新的其他缩放列。

如何解决此折旧错误?

1 个答案:

答案 0 :(得分:5)

做什么

scaler.fit_transform(email[['Quantity']])

而不是

scaler.fit_transform(email['Quantity'])

演示:我使用了上一个问题中的示例数据集:

In [56]: scaler.fit_transform(df[['Event_Counts']])
Out[56]:
array([[ 0.99722347],
       [ 1.        ],
       [ 0.        ]])

注意 - 它生成了一个形状为(3,1)而不是(3,)

的数组

作为新专栏:

In [58]: df['scaled_event_counts'] = scaler.fit_transform(df[['Event_Counts']])

In [59]: df
Out[59]:
       Date  Event_Counts  Category_A  Category_B  scaled_event_counts
0  20170401        982457           0           1             0.997223
1  20170402        982754           1           0             1.000000
2  20170402        875786           0           1             0.000000