當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy interpolate.RBFInterpolator用法及代碼示例

本文簡要介紹 python 語言中 scipy.interpolate.RBFInterpolator 的用法。

用法:

class  scipy.interpolate.RBFInterpolator(y, d, neighbors=None, smoothing=0.0, kernel='thin_plate_spline', epsilon=None, degree=None)#

N 維徑向基函數 (RBF) 插值。

參數

y (npoints, ndims) 類似數組

數據點坐標的二維數組。

d (npoints, …) 數組

N-D y 處的數據值數組。 d 沿第一軸的長度必須等於 y 的長度。與某些插補器不同,插補軸無法更改。

neighbors 整數,可選

如果指定,將僅使用這麽多最近的數據點計算每個評估點的插值值。默認使用所有數據點。

smoothing float 或 (npoints, ) 數組,可選

平滑參數。將其設置為 0 時,插值與數據完美擬合。對於較大的值,插值接近具有指定次數的多項式的最小二乘擬合。默認值為 0。

kernel str,可選

RBF 類型。這應該是其中之一

  • ‘linear’ : -r

  • ‘thin_plate_spline’ : r**2 * log(r)

  • ‘cubic’ : r**3

  • ‘quintic’ : -r**5

  • ‘multiquadric’ : -sqrt(1 + r**2)

  • ‘inverse_multiquadric’ : 1/sqrt(1 + r**2)

  • ‘inverse_quadratic’ : 1/(1 + r**2)

  • ‘gaussian’ : exp(-r**2)

默認為‘thin_plate_spline’。

epsilon 浮點數,可選

縮放 RBF 輸入的形狀參數。如果內核是‘linear’, ‘thin_plate_spline’、‘cubic’或‘quintic’,則默認為1並且可以忽略,因為它與縮放平滑參數具有相同的效果。否則,必須指定這一點。

degree 整數,可選

添加多項式的次數。對於某些 RBF,如果多項式次數太小,則插值可能不是well-posed。這些 RBF 及其相應的最小度數是

  • ‘multiquadric’ : 0

  • ‘linear’ : 0

  • ‘thin_plate_spline’ : 1

  • ‘cubic’ : 1

  • ‘quintic’ : 2

默認值為內核的最小度數,如果沒有最小度數,則為 0。將此設置為 -1 表示不添加多項式。

注意

RBF 是 N 維空間中的標量值函數,其在 處的值可以用 表示,其中 是 RBF 的中心。

來自位置 的數據值向量 的 RBF 插值是以 為中心的 RBF 加上具有指定次數的多項式的線性組合。 RBF 插值寫為

其中 是 RBF 的矩陣,其中心在 在點 處評估,而 是單項式矩陣,它跨越具有指定次數的多項式,在 處評估。係數 是線性方程組的解

其中 是一個非負平滑參數,用於控製我們想要擬合數據的程度。當平滑參數為 0 時,數據完全擬合。

如果滿足以下要求,則上述係統是唯一可解決的:

  • must have full column rank. always has full column rank when degree is -1 or 0. When degree is 1, has full column rank if the data point locations are not all collinear (N=2), coplanar (N=3), etc.

  • If kernel is ‘multiquadric’, ‘linear’, ‘thin_plate_spline’, ‘cubic’, or ‘quintic’, then degree must not be lower than the minimum value listed above.

  • If smoothing is 0, then each data point location must be distinct.

當使用非尺度不變的 RBF(‘multiquadric’, ‘inverse_multiquadric’、‘inverse_quadratic’ 或 ‘gaussian’)時,必須選擇適當的形狀參數(例如,通過交叉驗證)。較小的形狀參數值對應於較寬的 RBF。當形狀參數太小時,問題可能會變成ill-conditioned 或單數。

求解 RBF 插值係數所需的內存隨著數據點的數量呈二次方增加,當插值超過大約一千個數據點時,這可能變得不切實際。為了克服大型插值問題的內存限製,可以指定近鄰參數以僅使用最近的數據點為每個評估點計算 RBF 插值。

參考

[1]

Fasshauer, G.,2007 年。使用 Matlab 的無網格近似方法。世界科學出版公司

[3]

Wahba, G.,1990。觀測數據的樣條模型。暹。

例子

演示將分散的數據插值到二維網格中。

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy.interpolate import RBFInterpolator
>>> from scipy.stats.qmc import Halton
>>> rng = np.random.default_rng()
>>> xobs = 2*Halton(2, seed=rng).random(100) - 1
>>> yobs = np.sum(xobs, axis=1)*np.exp(-6*np.sum(xobs**2, axis=1))
>>> xgrid = np.mgrid[-1:1:50j, -1:1:50j]
>>> xflat = xgrid.reshape(2, -1).T
>>> yflat = RBFInterpolator(xobs, yobs)(xflat)
>>> ygrid = yflat.reshape(50, 50)
>>> fig, ax = plt.subplots()
>>> ax.pcolormesh(*xgrid, ygrid, vmin=-0.25, vmax=0.25, shading='gouraud')
>>> p = ax.scatter(*xobs.T, c=yobs, s=50, ec='k', vmin=-0.25, vmax=0.25)
>>> fig.colorbar(p)
>>> plt.show()
scipy-interpolate-RBFInterpolator-1.png

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.interpolate.RBFInterpolator。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。