为什么更改值不会更改Scipy Spline值?

时间:2016-02-12 09:27:53

标签: scipy interpolation spline

我有以下代码试图插入气温值

import numpy as np
from scipy.interpolate import RectBivariateSpline as spline

x = np.array([-7.52, -7.50, -7.48, -7.46])
y = np.array([41.88, 41.90, 41.92, 41.94])
z = np.array([
    [1.2, 1.1, 1.5, 1.3],
    [1.3, 1.4, 1.5, 1.1],
    [1.2, 1.1, 1.2, 1.6],
    [1.3, 1.1, 1.2, 1.5]
    ])
f = spline(x, y, z)

当我使用f.ev(41.89, -7.46)调用该函数时,即使我更改z的值,该值仍保持相同[ 1.3]。你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您是否在更改f的值后创建了新的z对象?显然f正在使用z的副本,而不是z本身。您需要更新评估者,看看更改z的效果。

如果需要,我可以添加演示。

In [373]: x = np.array([-7.52, -7.50, -7.48, -7.46])
In [374]: y = np.array([41.88, 41.90, 41.92, 41.94])
In [375]: z = np.array([
   .....:     [1.2, 1.1, 1.5, 1.3],
   .....:     [1.3, 1.4, 1.5, 1.1],
   .....:     [1.2, 1.1, 1.2, 1.6],
   .....:     [1.3, 1.1, 1.2, 1.5]
   .....:     ])
In [376]: f=interpolate.RectBivariateSpline(x,y,z)

In [378]:  f(41.89,-7.46)
Out[378]: array([[ 1.3]])
In [379]:  f.ev(41.89,-7.46)
Out[379]: array(1.3)

In [381]: z+=2
In [382]: z
Out[382]: 
array([[ 3.2,  3.1,  3.5,  3.3],
       [ 3.3,  3.4,  3.5,  3.1],
       [ 3.2,  3.1,  3.2,  3.6],
       [ 3.3,  3.1,  3.2,  3.5]])
In [383]:  f.ev(41.89,-7.46)
Out[383]: array(1.3)
In [384]: f=interpolate.RectBivariateSpline(x,y,z)
In [385]:  f.ev(41.89,-7.46)
Out[385]: array(3.3000000000000003)