簡體   English   中英

二維數組沿一個軸的線性插值

[英]Linear interpolation of 2D-arrays along one axis

我需要沿一個軸插入二維數組,在本例中axis=1 我嘗試使用scipy.interpolate.interp1d但我無法使其工作。

這是我用一個簡單的例子得到的:

import numpy as np
import scipy.interpolate

xp = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
               [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
yp = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
               [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]])

x = np.array([[1.5, 4.5, 7.5],
              [2.5, 6.5, 9.5]])

interpolate = scipy.interpolate.interp1d(xp, yp, axis=1)
    ValueError: x and y arrays must be equal in length along interpolation axis.

預期結果是:

[[ 1.5  4.5  7.5]
 [ 6.5 42.5 90.5]]

當然,最簡單的解決方案是沿axis=0進行迭代並進行多個一維數組插值,但由於我的實際 arrays 非常大,我想避免它。 關於如何解決這個問題的任何想法? 非常感謝!

您可以使用 scipy.interpolate.interpn 它專為更高維度的插值而設計。

對於您的示例,您將執行以下操作:

from scipy.interpolate import interpn

interpolated = interpn((xp[0], xp[1]), yp, x, method='linear')
print(interpolated)

結果:

# [[ 1.5  4.5  7.5]
#  [ 6.5 42.5 90.5]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM