gpt4 book ai didi

python - 更有效的方法是将 pandas 数据框中的列子集居中并保留列名

转载 作者:太空宇宙 更新时间:2023-11-04 00:56:17 25 4
gpt4 key购买 nike

我有一个包含大约 370 列的数据框。我正在测试一系列假设,这些假设要求我使用模型的子集来拟合三次回归模型。我打算使用 statsmodels 来模拟这些数据。

多项式回归过程的一部分涉及均值居中变量(从特定特征的每个案例中减去均值)。

我可以使用 3 行代码完成此操作,但它似乎效率低下,因为我需要为六个假设复制此过程。请记住,我需要从 statsmodel 输出的系数级别获取数据,因此我需要保留列名。

这里是数据的一瞥。这是我的一个假设检验所需的列子集。

      i  we  you  shehe  they  ipron
0 0.51 0 0 0.26 0.00 1.02
1 1.24 0 0 0.00 0.00 1.66
2 0.00 0 0 0.00 0.72 1.45
3 0.00 0 0 0.00 0.00 0.53

这是表示居中并保留列名的代码。

from sklearn import preprocessing
#create df of features for hypothesis, from full dataframe
h2 = df[['i', 'we', 'you', 'shehe', 'they', 'ipron']]

#center the variables
x_centered = preprocessing.scale(h2, with_mean='True', with_std='False')

#convert back into a Pandas dataframe and add column names
x_centered_df = pd.DataFrame(x_centered, columns=h2.columns)

关于如何提高效率/速度的任何建议都很棒!

最佳答案

df.apply(lambda x: x-x.mean())

%timeit df.apply(lambda x: x-x.mean())
1000 loops, best of 3: 2.09 ms per loop

df.subtract(df.mean())

%timeit df.subtract(df.mean())
1000 loops, best of 3: 902 µs per loop

都产生:

        i  we  you  shehe  they  ipron
0 0.0725 0 0 0.195 -0.18 -0.145
1 0.8025 0 0 -0.065 -0.18 0.495
2 -0.4375 0 0 -0.065 0.54 0.285
3 -0.4375 0 0 -0.065 -0.18 -0.635

关于python - 更有效的方法是将 pandas 数据框中的列子集居中并保留列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34953988/

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