Skip to main content
 首页 » 编程设计

Python DeprecationWarning : Passing 1d arrays as data is deprecated in 0. 17 并将在 0.19 中引发 ValueError

2025年05月04日24sharpest

基于我之前的问题 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 建议我将缩放应用于整个数据框,但这不是一个选项。我需要按原样维护列,并且只想添加新的额外缩放列。

我该如何解决这个折旧错误?

请您参考如下方法:

做什么

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