Python 了解scipy.ndimage.map_坐标上的输入和输出

Python 了解scipy.ndimage.map_坐标上的输入和输出,python,scipy,Python,Scipy,我试图在网格中的一组点上绘制一条直线。数据位于x、y、z坐标的列表中。我认为map_坐标是我想要的,但我不理解输入和输出的形式。。。任何帮助都将不胜感激 list = [[x1, y1, z1] [x2, y2, z2] ... [xn, yn, zn]] 我试图查找的值是x和y值 look_up_values= [[x1, y1] [x2, y2] ...

我试图在网格中的一组点上绘制一条直线。数据位于x、y、z坐标的列表中。我认为map_坐标是我想要的,但我不理解输入和输出的形式。。。任何帮助都将不胜感激

list = [[x1, y1, z1]
        [x2, y2, z2]
        ...
        [xn, yn, zn]]
我试图查找的值是x和y值

look_up_values= [[x1, y1]
                 [x2, y2]
                 ...
                 [xn, yn]]
我的问题是,为什么map_坐标需要
(2,2)
数组,以及输出中有什么信息(2值列表)

以下是一个可行的例子:

from scipy.ndimage.interpolation import map_coordinates
import numpy as np
in_data = np.array([[0.,0.,0.]
                    ,[0.,1.,.2]
                    ,[0.,2.,.4]
                    ,[1.,0.,.2]
                    ,[1.,3.,.5]
                    ,[2.,2.,.7]])
z = map_coordinates(in_data, np.array([[1.,1.],[1.,2.]]), order=1)
print z #I do not understand this output...
#[1. .2]

如果我不得不猜测,我会说它在网格上的两个点之间插值,那么输出意味着什么?

地图坐标的输出是在指定坐标处原始数组值的插值

在您的示例中,输入
(1,1)、(1,2)
。这意味着您希望插值位于两个位置:点x=1,y=1和x=1,y=2。它需要两个数组,因为每个数组都是x坐标和y坐标。也就是说,您需要两个坐标:1,1处的x坐标和1,2处的y坐标


您的输入可以任意长或短,但阵列必须是相同长度的,因为它们是耦合的。

让我先尝试回答一个逐步1d插值案例:

import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import numpy as np

### 1d example of interpolation ###

in_data_x = np.array([1., 2., 3., 4., 5., 6.])
in_data_y = np.array([1.5, 2., 2.5, 3.,  3.5,  4.])  # y = .5 x - 1
f = interp1d(in_data_x, in_data_y, kind='linear')

print(f)
# f in all of the points of the grid (in_data_x): output coincides with in_data_y

    
print(f(1), f(1.), f(1.5), f(2.), f(2.5), f(3.))
# f in a point outside the grid:
print(f(1.8))
# this is equal to y = .5 x - 1 for x = 1.8, up to some point.
assert round(0.5 * 1.8 + 1, ndigits=10) == round(f(1.8), ndigits=10)

# plot up to this point
xnew = np.arange(1, 6, 0.1)
ynew = f(xnew)
plt.plot(in_data_x, in_data_y, 'o', xnew, ynew, '-')
# close the image to move forward.
plt.show()

### another 1d example of interpolation ###

in_data_x = np.array([1., 2., 3., 4., 5., 6.])
in_data_y = np.array([-1.8, -1.2, -0.2, 1.2, 3., 5.2])  # y = .2 x**2 - 2
f = interp1d(in_data_x, in_data_y, kind='cubic')

print(f)
# f in all of the points of the grid (in_data_x): output coincides with in_data_y
print(f(1), f(1.), f(1.5), f(2.), f(2.5), f(3.))
# f in a point outside the grid:
print(f(1.8))
# this is equal to y = .2 x**2 - 2 for x = 1.8, up to some precision.
assert round(0.2 * 1.8 ** 2 - 2, ndigits=10) == round(f(1.8), ndigits=10)

# plot up to this point
xnew = np.arange(1, 6, 0.1)
ynew = f(xnew)
plt.plot(in_data_x, in_data_y, 'o', xnew, ynew, '-')
plt.show()
函数interp1d为您提供了一个插值器,该插值器为您提供一个值,该值通过某种算法(在本例中为线性)对通过x=[1,2,3,4,5,6.]y=[-1.8,-1.2,-0.2,1.2,3,5.2]的函数进行插值

地图坐标也是这样。当数据具有多个维度时。 第一个主要区别是结果不是插值器,而是数组。 第二个主要区别是x坐标由数据维度的矩阵坐标给出。 第三个区别是输入必须作为列向量给出。 看这个例子

from scipy.ndimage.interpolation import map_coordinates
import numpy as np


in_data = np.array([[0., -1., 2.],
                    [2., 1., 0.],
                    [4., 3., 2.]])  # z = 2.*x - 1.*y

# want the second argument as a column vector (or a transposed row)
# see on some points of the grid:
print('at the point 0, 0 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 0.]]).T, order=1))
print('at the point 0, 1 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 1.]]).T, order=1))
print('at the point 0, 2 of the grid the function z is: ')
print(map_coordinates(in_data, np.array([[0., 2.]]).T, order=1))

# see some points outside the grid
print()
print('at the point 0.2, 0.2 of the grid, with linear interpolation z is:')
print(map_coordinates(in_data, np.array([[.2, .2]]).T, order=1))
print('and it coincides with 2.*.2 - .2')
print()
print('at the point 0.2, 0.2 of the grid, with cubic interpolation z is:')
print(map_coordinates(in_data, np.array([[0.2, .2]]).T, order=3)
最后回答你的问题,你给出了答案

in_data = np.array([[0., 0., 0.],
                    [0., 1., .2],
                    [0., 2., .4],
                    [1., 0., .2],
                    [1., 3., .5],
                    [2., 2., .7]])
这是一个函数z(x,y),在矩阵坐标给出的网格上计算:z(0,0)=0。 z(2,2)=.7 询问

表示询问z(1,1)和z(1,2),其中第二个输入数组由列读取

z = map_coordinates(in_data, np.array([[.5, .5]]).T, order=1)
意思是问z(0.5,0.5)。注意输入中的transpose.T。
希望它确实有意义,并且有帮助。

这对我来说是有意义的,但是输出说明了什么?所以如果我理解正确,我希望它会给出一个z值或一个xyz值……但它似乎是xy?对于二维数组,没有“z”值,但是如果我理解正确,你需要给定坐标下数组的实际值。输出与输入数组相关,以便输出的第一个元素是第一个输入坐标处的值。你可以使用类似于
np.concatenate
的东西来创建[x,y,value]数组。谢谢你,伙计,我认为地图坐标并没有像我想象的那样。所以我使用了griddata,它是一个插值函数。谢谢你花时间为我做这件事。我很高兴。我经常使用
map\u坐标
,但我也花了一些时间才弄明白它的一些微妙之处=)+1啊哈!你的第一句话正是我需要的。对我来说,比“坐标数组用于为输出中的每个点查找输入中的相应坐标”更容易理解。从。我理解坐标有些困难。我希望x、y、z是行中的第0、第1和第2个元素。在中间代码块中,写的是z=2×*-1*y,但它与第三个数据不匹配。最后写的是z(2,2)=.4,但在_ata的最后一行是[2,2,7]。@horrorvaugi它实际上是一个打字错误。谢谢你抓住它。
z = map_coordinates(in_data, np.array([[.5, .5]]).T, order=1)