Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

python - 了解scipy.interpolate的interpn功能(Understanding interpn function of scipy.interpolate)

I am trying to understand the working of the function scipy.interpolate .

(我试图了解scipy.interpolate函数的工作scipy.interpolate 。)

I created a small setup but it is giving errors.

(我创建了一个小安装程序,但出现错误。)

Here is what I did:

(这是我所做的:)

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

x = np.arange(10)
y = np.arange(10)
gx, gy = np.meshgrid(x,y)
v = np.ones((10,10))


sample_at = np.random.random((10,10))

interpolated = scipy.interpolate.interpn((gx, gy), v, sample_at)


print(interpolated.shape)

This gives an error:

(这给出了一个错误:)

    Traceback (most recent call last):
File "test.py", line 13, in <module>
    interpolated = scipy.interpolate.interpn((gx, gy), v, sample_at)
File "/home/lib/python3.5/site-packages/scipy/interpolate/interpolate.py", line 2624, in interpn
    "1-dimensional" % i)
ValueError: The points in dimension 0 must be 1-dimensional

What is happening here?

(这是怎么回事)

  ask by Amanda translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are making a wrong assumption wrt the structure of the grid requested by interpn

(您对interpn请求的网格结构做出了错误的假设)

ValueError: The points in dimension 0 must be 1-dimensional

This message, a little bit obscure, references the elements pf the grid and say that they must be 1-dimensional, you must call interpn not with (gx, gy) as first argument but with (x, y) .

(此消息有点晦涩,它引用了网格中的元素并说它们必须是一维的,您必须不使用(gx, gy)作为第一个参数而是使用(x, y)来调用interpn 。)

scipy.interpolate.interpn((x, y), v, sample_at)

But there is another wrong assumption in your code, because if you use the call above you'll get a different error

(但是您的代码中还有另一个错误的假设,因为如果您使用上面的调用,则会得到不同的错误)

ValueError: The requested sample points xi have dimension 10, but this RegularGridInterpolator has dimension 2

that hopefully has a clearer meaning: if your grid has two dimensions ( x and y , that is) all your points on which interpn has to interpolate must be 2-dimensional as well... In other words, in your example the LAST dimension of the grid must be 2

(希望它具有更清晰的含义:如果网格具有二维(即xy ),则interpn必须在其上进行插值的所有点也都必须为2维...换句话说,在您的示例中LAST维的网格必须为2)

Putting it all together (I'll use a location matrix sample_at of 5 rows by 3 columns 0f 2-D points in this example) we have

(放在一起(在此示例中,我将使用5行乘3列0f 2维点的位置矩阵sample_at ),)

In [69]: import numpy as np 
    ...: import scipy.interpolate 
    ...: import matplotlib.pyplot as plt 
    ...:  
    ...: x = np.arange(10) 
    ...: y = np.arange(10) 
    ...: v = np.ones((10,10)) 
    ...: sample_at = 8*np.random.random((30)).reshape(5, 3, 2) 
    ...:  
    ...: scipy.interpolate.interpn((x, y), v, sample_at)                                                       
Out[69]: 
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

PS: read the source code 1 , the comments in it are much better than the error messages...

(PS:读取源代码1 ,其中的注释比错误消息要好得多...)


(1) for me the source code is located at

((1)对我来说,源代码位于)

~/lib/miniconda3/lib/python3.7/site-packages/scipy/interpolate/interpolate.py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...