【问题标题】:Can I create and interpolant using e.g. scipy griddata?我可以使用例如创建和插值吗? scipy网格数据?
【发布时间】:2020-09-15 12:05:47
【问题描述】:

我正在将 matlab 脚本转换为 python 脚本,在该脚本中我使用了函数 mappedInterpolant()。在 matlab 中,它有一个很好的属性,它创建了一个插值,我可以在几个选定的点进行评估,这比在整个域上创建插值网格数据要快得多。从 matlab 手册中它说:

% Fast to create interpolant F and evaluate multiple times
F = scatteredInterpolant(X,Y,V)
v1 = F(Xq1,Yq1)
v2 = F(Xq2,Yq2)

% Slower to compute interpolations separately using griddata
v1 = griddata(X,Y,V,Xq1,Yq1)
v2 = griddata(X,Y,V,Xq2,Yq2)

到目前为止,我只找到了后一种方法的 python 替代方法,无论我在多少点处评估插值,它都需要大约相同的时间:

import numpy as np
from scipy.interpolate import griddata
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# example surface
[theta, phi] = np.meshgrid(
    np.linspace(0, 2.0 * np.pi, 100),
    np.linspace(np.spacing(1), np.pi/2-np.pi/20, 100))
x = 1.0*np.multiply(np.cos(theta), np.sin(phi))
y = 1.0*np.multiply(np.sin(theta), np.sin(phi))
z = 1.0 * np.cos(phi)


[xi, yi] = np.meshgrid(np.linspace(-1, 1, 10),
                       np.linspace(-1, 1, 10))
%timeit zi = griddata((np.reshape(x, -1), np.reshape(y, -1)), np.reshape(z, -1), (xi, yi), method='linear')
%timeit zi_singlepoint = griddata((np.reshape(x, -1), np.reshape(y, -1)), np.reshape(z, -1), (0.0, 0.0), method='linear')

是否有可能使用 scipy 中的 griddata() 函数或 python 中的其他模块获得可以像 matlab 中一样评估的插值?

编辑:我刚刚发现基于 scipy.interpolate.BivariateSpline 的函数的工作方式与 matlabsscatterinterpolant() 类似。从文档中: "BivariateSpline 类是 UnivariateSpline 类的二维模拟。它及其子类以面向对象的方式实现上述 FITPACK 函数,允许实例化可调用的对象以计算样条通过将两个坐标作为两个参数传入来获得值。”我会试试这个并返回。

【问题讨论】:

    标签: python scipy interpolation


    【解决方案1】:

    可以使用给定坐标调用和评估的一些插值方法是:

    scipy.interpolate.SmoothBivariateSpline
    scipy.interpolate.Rbf
    scipy.interpolate.NearestNDInterpolator
    scipy.interpolate.LinearNDInterpolator
    scipy.interpolate.CloughTocher2DInterpolator
    

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-11-09
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      相关资源
      最近更新 更多