首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解scipy.ndimage.map_coordinates的输入和输出

理解scipy.ndimage.map_coordinates的输入和输出
EN

Stack Overflow用户
提问于 2015-02-13 18:14:52
回答 4查看 11.4K关注 0票数 9

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

代码语言:javascript
复制
list = [[x1, y1, z1]
        [x2, y2, z2]
        ...
        [xn, yn, zn]]

我试图查找的值是x和y值。

代码语言:javascript
复制
look_up_values= [[x1, y1]
                 [x2, y2]
                 ...
                 [xn, yn]]

我的问题是,为什么map_coordinates需要一个(2,2)数组,以及输出中有哪些信息(一个2值列表)。

下面是一个可行的例子:

代码语言:javascript
复制
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]

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-02-13 18:50:04

map_coordinates的输出是在指定的坐标处对原始数组的值进行插值。

在您的示例中,输入[[1, 1], [1, 2]]。这意味着你需要两个位置的插值值:点x=1,y=1和x=1,y=2。它需要两个数组,因为每个数组都是x和y坐标。也就是说,你要求的有两个坐标: 1,1处的X坐标和1,2处的y坐标。

您选择的具体示例有点混乱,因为它看起来像是[1, 1]对应于[x0, y0] --这个解释是不正确的--它实际上对应于[x0, x1]。这一点很明显,有超过2点:一般格式是[[x0, x1, x2, ...], [y0, y1, y2, ...]]

输入可以是长的或短的,但是数组必须是相同的长度,因为它们是耦合的。

票数 8
EN

Stack Overflow用户

发布于 2015-12-07 16:45:55

让我试着回答一下,先看一步一步的一维插值情况:

代码语言:javascript
复制
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、3、5.2传递的函数的插值值(在本例中为线性)。

map_coordinates也会这样做。当您的数据有多个维度时。第一个主要区别是,结果不是一个内插器,而是一个数组。第二个主要区别是,x坐标是由数据维数的矩阵坐标给出的。第三个区别是输入必须作为列向量给出。参见此示例

代码语言:javascript
复制
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)

最后回答你的问题,你给了我作为输入

代码语言:javascript
复制
in_data = np.array([[0., 0., 0.],
                    [0., 1., .2],
                    [0., 2., .4],
                    [1., 0., .2],
                    [1., 3., .5],
                    [2., 2., .7]])

这是由矩阵坐标z( 0,0) =0在网格上计算的函数z(x,y)。z(2,2) = .7询问

代码语言:javascript
复制
z = map_coordinates(in_data, np.array([[1., 1.], [1., 2.]]), order=1)

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

代码语言:javascript
复制
z = map_coordinates(in_data, np.array([[.5, .5]]).T, order=1)

意思是问z(0.5,0.5)。注意输入中的转置.T。希望这是有意义的,也是有帮助的。

票数 7
EN

Stack Overflow用户

发布于 2021-07-06 15:22:23

X和y坐标指的是向量索引。

因此,对于col 2和第2行= z=2*2-2=-2代替2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28506050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档