【问题标题】:vectorized sum of interpolated values over many arrays许多数组上的插值的矢量化总和
【发布时间】:2016-09-20 12:02:31
【问题描述】:

我有一大组(数千条)平滑线(一系列 x,y 对),x 和 y 的采样不同,每条线的长度也不同,即

x_0 = {x_00, x_01, ..., }  # length n_0
x_1 = {x_10, x_11, ..., }  # length n_1
...
x_m = {x_m0, x_m1, ..., }  # length n_m

y_0 = {y_00, y_01, ..., }  # length n_0
y_1 = {y_10, y_11, ..., }  # length n_1
...
y_m = {y_m0, y_m1, ..., }  # length n_m

我想找到插值到一组常规 x 点的每条线的累积属性,即x = {x_0, x_1 ..., x_n-1}

目前我在for-循环每一行,创建一个插值,重新采样,然后取该结果的总和/中值/任何值。它有效,但它真的很慢。 有没有办法对这个操作进行向量化/矩阵化?

我在想,既然线性插值可以是矩阵运算,也许有可能。同时,由于每一行可以有不同的长度......这可能很复杂。 编辑:但是零填充较短的数组会很容易......


我现在正在做的事情看起来像,

import numpy as np
import scipy as sp
import scipy.interpolate

...

# `xx` and `yy` are lists of lists with the x and y points respectively
# `xref` are the reference x values at which I want interpolants
yref = np.zeros([len(xx), len(xref)])
for ii, (xi, yi) in enumerate(zip(xx, yy)):
    yref[ii] = sp.interp(xref, xi, yi)

y_med = np.median(yref, axis=-1)
y_sum = np.sum(yref, axis=-1)
...

【问题讨论】:

    标签: python numpy math scipy vectorization


    【解决方案1】:

    希望您可以根据自己的目的调整以下内容。

    我将 pandas 包括在内,因为它具有用于填充缺失值的插值功能。

    设置

    import pandas as pd
    import numpy as np
    
    x = np.arange(19)
    x_0 = x[::2]
    x_1 = x[::3]
    
    np.random.seed([3,1415])
    y_0 = x_0 + np.random.randn(len(x_0)) * 2
    y_1 = x_1 + np.random.randn(len(x_1)) * 2
    
    xy_0 = pd.DataFrame(y_0, index=x_0)
    xy_1 = pd.DataFrame(y_1, index=x_1)
    

    注意:

    • x 长度为 19
    • x_0 长度为 10
    • x_1 长度为 7

    xy_0 看起来像:

                0
    0   -4.259448
    2   -0.536932
    4    0.059001
    6    1.481890
    8    7.301427
    10   9.946090
    12  12.632472
    14  14.697564
    16  17.430729
    18  19.541526
    

    xy_0 可以通过reindexx 对齐

    xy_0.reindex(x)
    
                0
    0   -4.259448
    1         NaN
    2   -0.536932
    3         NaN
    4    0.059001
    5         NaN
    6    1.481890
    7         NaN
    8    7.301427
    9         NaN
    10   9.946090
    11        NaN
    12  12.632472
    13        NaN
    14  14.697564
    15        NaN
    16  17.430729
    17        NaN
    18  19.541526
    

    然后我们可以用interpolate填写缺失

    xy_0.reindex(x).interpolate()
    
                0
    0   -4.259448
    1   -2.398190
    2   -0.536932
    3   -0.238966
    4    0.059001
    5    0.770445
    6    1.481890
    7    4.391659
    8    7.301427
    9    8.623759
    10   9.946090
    11  11.289281
    12  12.632472
    13  13.665018
    14  14.697564
    15  16.064147
    16  17.430729
    17  18.486128
    18  19.541526
    

    xy_1

    xy_1.reindex(x)
    
                0
    0   -1.216416
    1         NaN
    2         NaN
    3    3.704781
    4         NaN
    5         NaN
    6    5.294958
    7         NaN
    8         NaN
    9    8.168262
    10        NaN
    11        NaN
    12  10.176849
    13        NaN
    14        NaN
    15  14.714924
    16        NaN
    17        NaN
    18  19.493678
    

    插值

    xy_0.reindex(x).interpolate()
    
                0
    0   -1.216416
    1    0.423983
    2    2.064382
    3    3.704781
    4    4.234840
    5    4.764899
    6    5.294958
    7    6.252726
    8    7.210494
    9    8.168262
    10   8.837791
    11   9.507320
    12  10.176849
    13  11.689541
    14  13.202233
    15  14.714924
    16  16.307842
    17  17.900760
    18  19.493678
    

    【讨论】:

    • +1,这真的很酷也很有用。但它并没有回答本身关于矢量化过程的问题 --- 我有数千个 xy 数组,我想一起处理它们。我已经编辑了问题以使其更清楚(对不起,如果以前不是)。
    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2020-02-11
    • 2012-09-12
    • 2020-05-18
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多