【发布时间】:2016-04-29 11:35:47
【问题描述】:
我有一个大约有 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)
任何关于如何提高效率/速度的建议都很棒!
【问题讨论】:
标签: python pandas machine-learning scikit-learn statsmodels