我有一个包含> 25万行的数据框,我想计算滚动回归斜率。我可以使用以下代码来完成此操作,但需要一分钟以上的时间。我有什么办法可以加快速度?
EXEC sp_executesql
N'select * from Employees where Id = @param1',
N'@param1 int'
,@param1 = 1
答案 0 :(得分:2)
使用np.polyfit
和as_strided
,您可以执行以下操作:
from numpy.lib.stride_tricks import as_strided
window = 15
ys = df.y.to_numpy()
stride = ys.strides
slopes, intercepts = np.polyfit(np.arange(window),
as_strided(ys, (len(df)-window+1, window),
stride+stride).T,
deg=1)
性能:
CPU times: user 148 ms, sys: 9.86 ms, total: 157 ms