微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scipy版本能否更改scipy.interpolate.griddata结果?

如何解决scipy版本能否更改scipy.interpolate.griddata结果?

我的问题始于 scipy 1.2.3 及其功能scipy.interpolate.griddata,该函数执行插值并为我提供参考数据集。 (我对三次二维插值感兴趣,请参见下面的测试用例)

将scipy更新为 scipy 1.5.2 后,我无法生成与以前完全相同的结果...而且差异不可忽略。
通过测试可在anaconda发行版中使用的scipy的早期版本,如果安装 scipy 1.3.2 ,则可以精确地生成初始插值结果。

所以我认为griddata或其子组件之一是在 scipy 1.3.2 之后更新的。

但是我在Scipy发行说明中找不到任何解释:Scipy.org Release Notes ...关于stackoverflow的更多信息。

有人遇到过这个问题:更新scipy改变了scipy.interpolate.griddata给出的结果吗?


要进行测试,我从how-can-i-perform-two-dimensional-interpolation-using-scipy借了一些代码(非常感谢)

from scipy.interpolate import griddata
import numpy as np


# scipy 1.2.3 or (scipy 1.3.2) reference dataset   
z_griddata_scipy_1_2_3 = np.array([[1.22464680e-16,2.99260075e-02,4.64921877e-02,3.63387200e-02,-1.17334278e-02,-4.10790167e-02,-3.53276896e-02,-1.32599029e-02,6.57516828e-03,1.46193750e-02,1.29942167e-02,4.60176170e-03,-1.02398072e-02,-3.13455739e-02,-3.89274672e-02,-1.15549286e-02,3.59960447e-02,4.60537630e-02,2.96438015e-02,1.22464680e-16],[3.06593878e-01,2.94590471e-01,2.55311166e-01,1.72704804e-01,6.75755257e-02,-8.71796149e-02,-1.69793095e-01,-2.16754270e-01,-2.45929090e-01,-2.64204208e-01,-2.83893302e-01,-2.86038057e-01,-2.52505900e-01,-1.93389278e-01,-9.70877464e-02,6.22252315e-02,1.64062151e-01,2.49498113e-01,2.91797267e-01,3.07425460e-01]])


# auxiliary function for mesh generation
def gimme_mesh(n):
    minval = -1
    maxval = 1
    # produce an asymmetric shape in order to catch issues with transpositions
    return np.meshgrid(np.linspace(minval,maxval,n),np.linspace(minval,n+1))


# set up underlying test functions,vectorized
def fun_smooth(x,y):
    return np.cos(np.pi * x)*np.sin(np.pi * y)


def test_griddata_cubic():
    # sparse input mesh,6x7 in shape
    N_sparse = 6
    x_sparse,y_sparse = gimme_mesh(N_sparse)
    z_sparse_smooth = fun_smooth(x_sparse,y_sparse)

    # dense output mesh,20x21 in shape
    N_dense = 20
    x_dense,y_dense = gimme_mesh(N_dense)

    z_griddata_scipy_test = griddata(np.array([x_sparse.ravel(),y_sparse.ravel()]).T,z_sparse_smooth.ravel(),(x_dense,y_dense),method='cubic')

    try:
        np.testing.assert_almost_equal(z_griddata_scipy_1_2_3,z_griddata_scipy_test[:2],decimal=5)

    except AssertionError as err:
        print (err)


if __name__ == '__main__':
    """
    """
    test_griddata_cubic()

我的计算机上Windows 7,Python 3.7,scipy 1.5.2的测试结果:

Arrays are not almost equal to 5 decimals

Mismatched elements: 38 / 40 (95%)
Max absolute difference: 0.03821737
Max relative difference: 0.67726368
 x: array([[ 1.22465e-16,2.99260e-02,4.64922e-02,3.63387e-02,-1.17334e-02,-4.10790e-02,-3.53277e-02,-1.32599e-02,6.57517e-03,1.46194e-02,1.29942e-02,4.60176e-03,...
 y: array([[ 1.22465e-16,2.97398e-02,4.62030e-02,3.61127e-02,-1.15711e-02,-3.85005e-02,-3.03032e-02,-9.36536e-03,3.92018e-03,1.17290e-02,1.37729e-02,6.40206e-03,...

我发现差异不是可以忽略的!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。