Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scipy-interp2d返回函数自动对输入参数进行不必要的排序_Python_Scipy - Fatal编程技术网

Python Scipy-interp2d返回函数自动对输入参数进行不必要的排序

Python Scipy-interp2d返回函数自动对输入参数进行不必要的排序,python,scipy,Python,Scipy,以下是: 我继续评估f: xnew = np.arange(-5.01, 5.01, 1e-2) f(xnew, 0) 输出: array([ 0.95603946, 0.9589498 , 0.96176018, ..., -0.96443103, -0.96171273, -0.96171273]) array([ 0.95603946, 0.9589498 , 0.96176018, ..., -0.96443103, -0.96171273, -0.9617127

以下是:

我继续评估f:

xnew = np.arange(-5.01, 5.01, 1e-2)
f(xnew, 0)
输出:

array([ 0.95603946,  0.9589498 ,  0.96176018, ..., -0.96443103,
   -0.96171273, -0.96171273])
array([ 0.95603946,  0.9589498 ,  0.96176018, ..., -0.96443103,
   -0.96171273, -0.96171273])
颠倒论点会得到同样的结果!我希望得到相反的结果:

xnewrev=np.array(list(reversed(np.arange(-5.01, 5.01, 1e-2))))
f(xnewrev, 0)
输出:

array([ 0.95603946,  0.9589498 ,  0.96176018, ..., -0.96443103,
   -0.96171273, -0.96171273])
array([ 0.95603946,  0.9589498 ,  0.96176018, ..., -0.96443103,
   -0.96171273, -0.96171273])
预期:

array([-0.96171273, -0.96171273, -0.96443103, ...,  0.96176018,
    0.9589498 ,  0.95603946])
我在洗牌
xnew
后也得到了相同的结果。在求值之前,插值函数
f
似乎对
xnew
进行排序。如何使
f
返回值的顺序与输入列表中给出的顺序相同

不知何故,这不是interp1d的问题


我使用的是Jupyter笔记本,Python 2.7.12 | Anaconda 4.1.1(64位)

您的
f
callable接受一个
假定排序的
参数:

assume_sorted : bool, optional
    If False, values of `x` and `y` can be in any order and they are
    sorted first.
    If True, `x` and `y` have to be arrays of monotonically
    increasing values.
因此,是的,如果您之前没有对输入进行排序,那么输入将在内部进行排序。我看不出有什么办法能把排序后的坐标拿回来

x
y
interp2d
的输入也在使用前进行排序。显然,插值计算需要排序数组

您可以使用双
argsort
索引恢复预排序顺序

制作一个数组并洗牌:

In [415]: xnew = np.arange(-10,11,2)
In [416]: xnew
Out[416]: array([-10,  -8,  -6,  -4,  -2,   0,   2,   4,   6,   8,  10])
In [417]: np.random.shuffle(xnew)
In [418]: xnew
Out[418]: array([  0,   2,   6,  -2,  10,  -4,   8,  -8, -10,  -6,   4])
获取恢复索引:

In [419]: idx = np.argsort(np.argsort(xnew))
In [420]: idx
Out[420]: array([ 5,  6,  8,  4, 10,  3,  9,  1,  0,  2,  7], dtype=int32)
测试它:

In [421]: np.sort(xnew)[idx]
Out[421]: array([  0,   2,   6,  -2,  10,  -4,   8,  -8, -10,  -6,   4])
基于,您可以定义一个返回未排序数组的新类,例如

from scipy.interpolate import interp2d
import numpy as np

class unsorted_interp2d(interp2d):
    def __call__(self, x, y, dx=0, dy=0):
        unsorted_idxs = np.argsort(np.argsort(x))
        return interp2d.__call__(self, x, y, dx=dx, dy=dy)[unsorted_idxs]
然后,在你的例子中,你会:

x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx+yy)
f = unsorted_interp2d(x, y, z, kind='cubic')

xnewrev=np.array(list(reversed(np.arange(-5.01, 5.01, 1e-2))))
f(xnewrev, 0)

# the expected output!
array([-0.96171273, -0.96171273, -0.96443103, ...,  0.96176018,
        0.9589498 ,  0.95603946])

对x和y进行排序是否有损于在特定的新x,y坐标处插值的目的?例如,如果我想在点(x=0,y=3)和(x=2,y=1)处插值,那么x坐标是x=(0,2),y坐标是y=(3,1)。排序后,它们变成x=(0,2)和y=(1,3),这两个点(x=0,y=1)和(x=2,y=2)插值,这两个点都不是插值所需的坐标之一。此外,我相当确定,根据使用的scipy版本,“假定_排序”参数可能不存在。@BrianD,我在2017年回答了这个问题,因此,除非您使用的是真正的旧版本,
假设\u sorted
存在。当输入点可能分散时,插值返回“具有形状(len(y),len(x))的二维阵列”,即网格。使用
[0,2]
[1,3]
你可以得到4分-你想要的2分和2分奖励。我一直在使用0.12.1,这是从2013年开始的,而7年前的补丁预计会使用排序阵列@布兰德