【问题标题】:Using interpolate function over 2-D array在二维数组上使用插值函数
【发布时间】:2013-02-16 06:15:25
【问题描述】:

我有一个一维函数,它需要花费大量时间来计算一个包含“x”值的大型二维数组,因此使用 SciPy 工具创建一个插值函数然后使用它计算 y 非常容易,这会快得多。但是,我不能对一维以上的数组使用插值函数。

例子:

# First, I create the interpolation function in the domain I want to work
x = np.arange(1, 100, 0.1)
f = exp(x) # a complicated function
f_int = sp.interpolate.InterpolatedUnivariateSpline(x, f, k=2)

# Now, in the code I do that
x = [[13, ..., 1], [99, ..., 45], [33, ..., 98] ..., [15, ..., 65]]
y = f_int(x)
# Which I want that it returns y = [[f_int(13), ..., f_int(1)], ..., [f_int(15), ..., f_int(65)]]

但返回:

ValueError: object too deep for desired array

我知道我可以遍历所有 x 成员,但我不知道这是否是一个更好的选择...

谢谢!

编辑:

这样的功能也可以完成这项工作:

def vector_op(function, values):

    orig_shape = values.shape
    values = np.reshape(values, values.size)

    return np.reshape(function(values), orig_shape)

我已经尝试过 np.vectorize 但它太慢了...

【问题讨论】:

    标签: python numpy scipy interpolation


    【解决方案1】:

    如果f_int 想要一维数据,您应该展平您的输入,将其提供给插值器,然后重建您的原始形状:

    >>> x = np.arange(1, 100, 0.1)
    >>> f = 2 * x # a simple function to see the results are good
    >>> f_int = scipy.interpolate.InterpolatedUnivariateSpline(x, f, k=2)
    
    >>> x = np.arange(25).reshape(5, 5) + 1
    >>> x
    array([[ 1,  2,  3,  4,  5],
           [ 6,  7,  8,  9, 10],
           [11, 12, 13, 14, 15],
           [16, 17, 18, 19, 20],
           [21, 22, 23, 24, 25]])
    >>> x_int = f_int(x.reshape(-1)).reshape(x.shape)
    >>> x_int
    array([[  2.,   4.,   6.,   8.,  10.],
           [ 12.,  14.,  16.,  18.,  20.],
           [ 22.,  24.,  26.,  28.,  30.],
           [ 32.,  34.,  36.,  38.,  40.],
           [ 42.,  44.,  46.,  48.,  50.]])
    

    x.reshape(-1) 进行展平,.reshape(x.shape) 将其恢复为原始形式。

    【讨论】:

    • 这可能会在边缘产生怪异
    • 我在想插值会导致原始数组边缘的值不同,一旦数组被展平然后插值然后重塑。基本上原始数组的左边缘将与下一行的右边缘进行插值,这可能会也可能不会导致问题。
    • @reptilecus 这不是插值在这里的工作方式。 f_int 在内部使用插值,但是当应用于数组时,它一次只考虑一个值,而不考虑它的邻居。
    【解决方案2】:

    我想你想在 numpy 中做一个矢量化函数:

    #create some random test data
    test = numpy.random.random((100,100))
    
    #a normal python function that you want to apply
    def myFunc(i):
        return np.exp(i)
    
    #now vectorize the function so that it will work on numpy arrays
    myVecFunc = np.vectorize(myFunc)
    
    result = myVecFunc(test)
    

    【讨论】:

      【解决方案3】:

      我会使用list comprehensionmap 的组合(可能有一种方法可以使用我缺少的两个嵌套maps

      In [24]: x
      Out[24]: [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
      
      In [25]: [map(lambda a: a*0.1, x_val) for x_val in x]
      Out[25]: 
      [[0.1, 0.2, 0.30000000000000004],
       [0.1, 0.2, 0.30000000000000004],
       [0.1, 0.2, 0.30000000000000004]]
      

      这仅用于说明目的.... 将 lambda a: a*0.1 替换为您的函数 f_int

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多