Skip to main content
 首页 » 编程设计

用于回归的 python pandas 标准化列

2025年05月04日12zhoujg

我有以下 df:

Date       Event_Counts   Category_A  Category_B 
20170401      982457          0           1 
20170402      982754          1           0 
20170402      875786          0           1 

我正在为回归分析准备数据,并希望标准化 Event_Counts 列,使其与类别的规模相似。

我使用以下代码:

from sklearn import preprocessing 
df['scaled_event_counts'] = preprocessing.scale(df['Event_Counts']) 

虽然我收到了这个警告:

DataConversionWarning: Data with input dtype int64 was converted to float64 by the scale function. 
  warnings.warn(msg, _DataConversionWarning) 

这似乎奏效了;有一个新专栏。但是,它有负数,例如 -1.3

我认为 scale 函数的作用是从数字中减去平均值,然后除以每行的标准差;然后将结果的最小值添加到每一行。

那样对 Pandas 不起作用吗?或者我应该使用 normalize() 函数还是 StandardScaler() 函数?我希望标准化列的比例为 0 到 1。

谢谢

请您参考如下方法:

我认为您正在寻找 sklearn.preprocessing.MinMaxScaler .这将使您能够扩展到给定的范围。

所以在你的情况下会是:

scaler = preprocessing.MinMaxScaler(feature_range=(0,1)) 
df['scaled_event_counts'] = scaler.fit_transform(df['Event_Counts']) 

缩放整个 df:

scaled_df = scaler.fit_transform(df) 
print(scaled_df) 
[[ 0.          0.99722347  0.          1.        ] 
 [ 1.          1.          1.          0.        ] 
 [ 1.          0.          0.          1.        ]]