Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分组、多索引、timeseris zscore计算速度慢(17秒),需要你提示加速吗_Python_Pandas_Performance_Pandas Groupby - Fatal编程技术网

Python 分组、多索引、timeseris zscore计算速度慢(17秒),需要你提示加速吗

Python 分组、多索引、timeseris zscore计算速度慢(17秒),需要你提示加速吗,python,pandas,performance,pandas-groupby,Python,Pandas,Performance,Pandas Groupby,我面临着熊猫滚动(扩展)10年历史记录的性能问题。太慢了 对于最近一天的zscore,它需要17秒 要计算整个历史记录,大约需要30分钟。(我已经将此历史记录重新采样到每周水平,以缩小到总记录。) 如果您有任何建议加快我的lastz功能,请随时分享您的想法 以下是详细信息。 1.数据集。10年的股票记录,已重新取样,以平衡大小和准确性。 总尺寸为(207376,8) 它涵盖了过去10年的大约500个指数数据。以下是样本: > Close PB1 PB2 PE

我面临着熊猫滚动(扩展)10年历史记录的性能问题。太慢了 对于最近一天的zscore,它需要17秒 要计算整个历史记录,大约需要30分钟。(我已经将此历史记录重新采样到每周水平,以缩小到总记录。)

如果您有任何建议加快我的lastz功能,请随时分享您的想法

以下是详细信息。 1.数据集。10年的股票记录,已重新取样,以平衡大小和准确性。 总尺寸为(207376,8) 它涵盖了过去10年的大约500个指数数据。以下是样本:

>               Close   PB1 PB2 PE1 PE2 TurnoverValue   TurnoverVol ROE  
>ticker tradeDate                               
>000001 2007-01-07  2678.526489 3.38135 2.87570 34.423700   61.361549 7.703712e+10  1.131558e+10    0.098227 
>2007-01-14 2755.759814 3.45878 3.09090 35.209019   66.407800   7.897185e+10    1.116473e+10    0.098236 
>2007-01-21 2796.761572 3.49394 3.31458 35.561800   70.449658   8.416415e+10    1.129387e+10    0.098250
  • 我想分析历史上的变化,并预测未来。 因此,lastz函数定义如下
  • 功能需要加速:

    ts_start=pd.to_date("20180831")
    
    @numba.jit
    def lastz(x):
        if x.index.max()[1]<ts_start:
            return np.nan
        else:
            freedom = 1 # it is sample, so the sample std degree of freedome should not be 0 but 1
    
            nlimit_interpolate = int(len(x)/100) #1% fill allowed
            #print(nlimit_interpolate, len(x))
            x=x.interpolate(limit=nlimit_interpolate+1 ) # plus 1 in case of 0 or minus
            x=x.loc[x.notnull()]
            Arry=x.values
            zscore = stats.zmap(Arry[-1],Arry,ddof=freedom)
            return zscore
    
    
    weekly = weekly.sort_index()
    %prun -s cumtime result = weekly.groupby(level="ticker").agg(lastz)
    
    ts_start=pd.截止日期(“20180831”)
    @麻木
    def lastz(x):
    如果x.index.max()
    >文件名:lineno(函数)
    >1 0.000 0.000 17.183 17.183{内置方法builtins.exec}
    >         1    0.000    0.000   17.183   17.183 :1()
    >1 0.000 0.000 17.176 17.176分组比:4652(合计)
    >1 0.000 0.000 17.176 17.176分组比:4086(合计)
    >1 0.000 0.000 17.176 17.176基准。py:562(聚合函数)
    >16/8 0.000 0.000 17.171 2.146分组比:3471(合计)
    >8 0.000 0.000 17.171 2.146 groupby.py:3513(_aggregate_multiple_funcs)
    >8 0.000 0.000 17.147 2.143 groupby.py:1060(_python_agg_general)
    >8 0.000 0.000 17.145 2.143 groupby.py:2668(agg_系列)
    >8 0.172 0.022 17.145 2.143 groupby.py:2693(_aggregate_series_pure_python)
    >4400.066 0.000 15.762 0.004 groupby.py:1062()
    >4400.1620.000 14.255 0.003:15(lastz)
    >4400.0350.000 8.982 0.002底座。py:807(最大值)
    >4400.070.000 7.955 0.002多重py:807(值)
    >4400.017 0.000 6.406 0.001日期时间。py:976(aType)
    >4400.007 0.000 6.316 0.001 datetimelike.py:1130(aType)
    >4400.0300.000 6.301 0.001 datetimelike.py:368(_box_value_as_index)
    >4400.009 0.000 5.613 0.001 datetimelike.py:362(框值)
    >4400.8600.000 5.602 0.001{熊猫.\u libs.lib.map\u infer}1659008 4.278 0.000 4.741
    >0.000 datetimes.py:606()
    >4328 0.096 0.000 1.774 0.000通用。py:5980(插值)
    >4336 0.015 0.000 1.696 0.000索引。py:1463(\uuuu getitem\uuuuuu)
    >4328 0.028 0.000 1.675 0.000索引。py:1854(_getitem_轴)
    
    我想知道datatime compare调用的频率是否太高,是否使用更好的方法跳过这些计算结果。我每周计算一次结果。因此,上周的数据已经存在,无需再次计算。index.max()[1]用于检查数据集是否晚于某一天。如果更新,则计算,否则返回nan

    如果我使用滚动或扩展模式,则需要半小时或2小时才能得到结果

    感谢您提供加快该功能的任何想法或线索。

    我更改了索引选择方法,使每次计算节省6秒


    但是,总运行时间仍然太长,无法接受。需要您提供优化的线索。

    看起来还没有人回答。我找到了一些有助于从13秒保存到5秒的单次计算的线索。看起来还没有人回答。我找到了一些有助于从13秒保存到5秒的单次计算的线索。正如您可能注意到的,6.4秒用于日期时间和索引检查。它运行了4400次。代码为index.max()[1]。我将其替换为索引[1][1]。与日期时间索引相关的时间节省了6.4秒。总的单次运行时间从13秒到5秒。它们的秘密是索引[1][1]仅使用14秒来获得结果,但index.max()[1]使用45ms,速度相差3000倍,但是我仍然认为应该有更好的方法来保存与滚动和扩展相关的聚合功能。
    >    ncalls  tottime  percall  cumtime  percall
    > filename:lineno(function)
    >         1    0.000    0.000   17.183   17.183 {built-in method builtins.exec}
    >         1    0.000    0.000   17.183   17.183 <string>:1(<module>)
    >         1    0.000    0.000   17.176   17.176 groupby.py:4652(aggregate)
    >         1    0.000    0.000   17.176   17.176 groupby.py:4086(aggregate)
    >         1    0.000    0.000   17.176   17.176 base.py:562(_aggregate_multiple_funcs)
    >      16/8    0.000    0.000   17.171    2.146 groupby.py:3471(aggregate)
    >         8    0.000    0.000   17.171    2.146 groupby.py:3513(_aggregate_multiple_funcs)
    >         8    0.000    0.000   17.147    2.143 groupby.py:1060(_python_agg_general)
    >         8    0.000    0.000   17.145    2.143 groupby.py:2668(agg_series)
    >         8    0.172    0.022   17.145    2.143 groupby.py:2693(_aggregate_series_pure_python)
    >      4400    0.066    0.000   15.762    0.004 groupby.py:1062(<lambda>)
    >      4400    0.162    0.000   14.255    0.003 <ipython-input-10-fdb784c8abd8>:15(lastz)
    >      4400    0.035    0.000    8.982    0.002 base.py:807(max)
    >      4400    0.070    0.000    7.955    0.002 multi.py:807(values)
    >      4400    0.017    0.000    6.406    0.001 datetimes.py:976(astype)
    >      4400    0.007    0.000    6.316    0.001 datetimelike.py:1130(astype)
    >      4400    0.030    0.000    6.301    0.001 datetimelike.py:368(_box_values_as_index)
    >      4400    0.009    0.000    5.613    0.001 datetimelike.py:362(_box_values)
    >      4400    0.860    0.000    5.602    0.001 {pandas._libs.lib.map_infer}   1659008    4.278    0.000    4.741   
    > 0.000 datetimes.py:606(<lambda>)
    >      4328    0.096    0.000    1.774    0.000 generic.py:5980(interpolate)
    >      4336    0.015    0.000    1.696    0.000 indexing.py:1463(__getitem__)
    >      4328    0.028    0.000    1.675    0.000 indexing.py:1854(_getitem_axis)