Skip to main content
 首页 » 编程设计

python之能否提高海量时序数据关联分析的计算速度

2024年10月24日15dudu

首先,我将介绍我的目标和实现它的代码

  1. VALUE 是一个 3 维 numpy 数组,表示二维区域的时间变化。 (例如 value[:1000,2,3] = list [网格 (X = 3, Y = 2) 的值从 0 到 1000s]。)

    In my real work, VALUE is in the shape of (2812, 75, 90) ps:"2812" is the sum hour for 4 months

  2. 我调用SELECT 的某个点代表了一个有趣的点,我将对该区域中的每个网格进行相关分析。

    SELECT is a pandas dataframe including each interesting point's X and Y

  3. COV 是一个 3 维数组作为计数矩阵记录每个 SELECT 点与每个网格点的相关度

    Setting cut-off Pearson coefficient rc = 0.75,
    for SELECT point t,
    If r(i,j) > rc ==> cov[t,i,j] = 1, else cov[t,i,j] = 0

这是我的代码,但有点慢。我认为流程的某些部分可以改进:

start = timeit.default_timer() 
### SELECT is a pandas dataframe including each interesting point's X and Y 
cov = np.zeros(len(SELECT)*VALUE.shape[1]*VALUE.shape[2]).reshape(len(SELECT), VALUE.shape[1],VALUE.shape[2]) 
for t in range(0,len(SELECT),1): 
    select_grid = pd.DataFrame(VALUE[:,SELECT.Y.iloc[t],SELECT.X.iloc[t]]) 
for i in range(0,VALUE.shape[1],1): 
    for j in range(0,VALUE.shape[2],1): 
        data_grid = pd.DataFrame(VALUE[:,i,j]) 
         ## Using corr to compute the correlation r 
        r_sg  = select_grid[0].corr(data_grid[0]) 
        if r_sg > 0.75: 
            cov[t,i,j] = 1 
 
end = timeit.default_timer()                 
print end - start                                                                                                         

请您参考如下方法:

您的工作很耗时:SELECT 中的每个样本大约需要一秒钟。

  • 向量化不会给你带来很大的改进,因为耗时的 corr 函数在内循环中。

  • 不过,您可以使用更轻量的代码,这里并不一定需要 pandas。例如:

    VALUE=random((2812,5,5)) 
    select=pd.DataFrame(randint(0,5,(10,2))) 
    .... 
    for (x,y) in select.values: 
    .... 
         r=np.corrcoef(VALUE[:,x,y],VALUE[:,i,j])[0,1] 
    .... 
    

[0,1]用于选择r在这里,因为 corrcoef 计算一个 2x2 数组。

  • 您可以做的第一个优化是使用 numpy 数组而不是 DataFrames,以获得 2 倍的 corr 计算增益。

    DFexample = pd.DataFrame(VALUE[:,0,:]) 
     
    In [19]: %timeit np.corrcoef(VALUE[:,0,0],VALUE[:,0,1])  
    1000 loops, best of 3: 556 µs per loop 
     
    In [20]: %timeit DFexample[0].corr(DFexample[1]) 
    1000 loops, best of 3: 1.09 ms per loop 
    
  • 另一个是预先计算均值 ans std,因为 r(x,y) = (<xy>-<x><y>)/σx/σy获得 3 倍 yield :

    In [24]: s=VALUE.std(axis=0)  # 1 second 
     
    In [25]: m=VALUE.mean(axis=0) # 2 second 
     
    In [26]: %timeit ((VALUE[:,0,0]*VALUE[:,0,1]).mean() -m[0,1]*m[0,0])/s[0,0]/s[0,1] 
    10000 loops, best of 3: 172 µs per loop 
     
    In [31]: allclose(((VALUE[:,0,0]*VALUE[:,0,1]).mean() -m[0,1]*m[0,0])/s[0,0]/s[0,1],\ 
    DFexample[0].corr(DFexample[1])) 
    Out[31]: True 
    

因此您至少可以赢得 6 倍积分。