1 回答
TA贡献1851条经验 获得超4个赞
我认为 pandas 不适合解决这个问题。
如果你有像api这样的数据生成源,这里由生成器模拟
import numpy as np
def gen():
while True:
yield np.random.rand((np.random.randint(1,10)))
输出不同大小的数据数组
for i in islice(gen(), 4):
print(i)
输出
[0.1591485]
[0.40462191 0.32921298 0.64704824 0.9433797 0.44754502 0.47600713
0.66130654]
[0.45582976 0.37764161 0.47205139 0.32354448 0.06795233 0.47943393
0.13395702]
[0.0967848]
例如,您可以使用 10 个样本的窗口来计算滚动测量值
import time
from itertools import islice
data = np.array([])
for new_data in islice(gen(), 5): # get data
for elem in new_data: # iterate through new data
data = np.concatenate((data, [elem])) # add new data row by row
print(data[-10:].mean()) # get mean of last 10 observations
time.sleep(.5)
输出
0.8251054981003462
0.5154331864262989
0.5677470477572374
0.6084844147856047
0.6532425615231122
0.6663683916931894
0.6768810511903373
0.6098697771903554
0.5976415974047367
0.5442112622703545
0.556721858529291
0.5851107975154073
0.6129548571751687
0.5519507890295304
0.47809901125252807
0.457599927037135
0.47739535574047764
0.5135494376774083
0.5620825459637069
0.5914086396034781
0.5554789093102113
0.6042456773490161
0.5860524867501515
0.6218627945520632
0.6509948271807725
0.6693775700674035
0.6657165569407465
0.6825455302579173
0.609296884720923
0.6708821735456445
添加回答
举报