不能通过scipy.interpolate.griddata对n维网格进行插值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不能通过scipy.interpolate.griddata对n维网格进行插值相关的知识,希望对你有一定的参考价值。

我有一个名为值的观察数组,它来自一个按以下方式定义的函数:

values = np.array([oscillatory(i) for i in range(points.shape[0])])

值具有形状(65,1),点是评估振荡函数的数组,它具有形状(65,7),7是在7维空间中作为维度的一些特征(7只是任意数字)。

我试图插入在这个空间中定义的一些任意点。我尝试用以下方法定义这些点:

grid_x = np.random.uniform(0,10, (100,7))

但它没有用。显然因为网格没有正确定义所以我尝试过:

grid_x= np.mgrid[-2:2, 5:99 , 4:5, 5:6, 5:7, 6:67, 7:67]

哪个不起作用。我通过以下方式调用插值函数:

grid_z1 = gdd(points, values, tuple(grid_x))

但是我总是遇到一个很大的错误,我有点难以理解。

奇怪的是,如果我以随机方式定义点和值,代码可以工作:

points = np.random.uniform(0,10, (65,3))
values = np.random.uniform(0,10,(points.shape[0],1))

grid_x= np.mgrid[0:2, 5:9 , 4:5]
grid_z1 = gdd(points, values, tuple(grid_x))

在这里我只尝试了3而不是7,因为它更快,但原理保持不变。如果我在7维中定义它,代码也可以。所以我的问题是:1)我怎样才能在7个维度中运行我的初始代码? 2)如果数组具有相同的形状,为什么随机声明与其他声明相比有效?

任何帮助将受到高度赞赏。非常感谢你。

我得到的错误如下:

    Traceback (most recent call last):
  File "/usr/lib/python3.6/code.py", line 91, in runcode
    exec(code, self.locals)
  File "<input>", line 2, in <module>
  File "/usr/local/lib/python3.6/dist-packages/scipy/interpolate/ndgriddata.py", line 222, in griddata
    rescale=rescale)
  File "interpnd.pyx", line 248, in scipy.interpolate.interpnd.LinearNDInterpolator.__init__
  File "qhull.pyx", line 1828, in scipy.spatial.qhull.Delaunay.__init__
  File "qhull.pyx", line 354, in scipy.spatial.qhull._Qhull.__init__
scipy.spatial.qhull.QhullError: QH6154 Qhull precision error: Initial simplex is flat (facet 1 is coplanar with the interior point)
While executing:  | qhull d Qbb Qt Qz Q12 Qc
Options selected for Qhull 2015.2.r 2016/01/18:
  run-id 526315618  delaunay  Qbbound-last  Qtriangulate  Qz-infinity-point
  Q12-no-wide-dup  Qcoplanar-keep  _pre-merge  _zero-centrum  Qinterior-keep
  Pgood  _max-width  4  Error-roundoff 1.2e-14  _one-merge 1.1e-13
  Visible-distance 7.3e-14  U-coplanar-distance 7.3e-14  Width-outside 1.5e-13
  _wide-facet 4.4e-13
precision problems (corrected unless 'Q0' or an error)
      2 flipped facets
      6 degenerate hyperplanes recomputed with gaussian elimination
     12 nearly singular or axis-parallel hyperplanes
      6 zero divisors during back substitute
    126 zero divisors during gaussian elimination
The input to qhull appears to be less than 4 dimensional, or a
computation has overflowed.
Qhull could not construct a clearly convex simplex from points:
- p2(v4):  -1.9     5     4 0.012
- p1(v3):  -1.9     5     4 0.0054
- p65(v2):     0   5.5   4.5     4
- p64(v1):     2     6     5     3
- p0(v0):    -2     5     4 -8.9e-16
The center point is coplanar with a facet, or a vertex is coplanar
with a neighboring facet.  The maximum round off error for
computing distances is 1.2e-14.  The center point, facets and distances
to the center point are as follows:
center point  -0.7625    5.309    4.309    1.407
facet p1 p65 p64 p0 distance=    0
facet p2 p65 p64 p0 distance= -8.9e-16
facet p2 p1 p64 p0 distance= -8.9e-16
facet p2 p1 p65 p0 distance= -8.9e-16
facet p2 p1 p65 p64 distance= -8.9e-16
These points either have a maximum or minimum x-coordinate, or
they maximize the determinant for k coordinates.  Trial points
are first selected from points that maximize a coordinate.
The min and max coordinates for each dimension are:
  0:        -2         2  difference=    4
  1:         5         6  difference=    1
  2:         4         5  difference=    1
  3:  -8.882e-16         4  difference=    4
If the input should be full dimensional, you have several options that
may determine an initial simplex:
  - use 'QJ'  to joggle the input and make it full dimensional
  - use 'QbB' to scale the points to the unit cube
  - use 'QR0' to randomly rotate the input for different maximum points
  - use 'Qs'  to search all points for the initial simplex
  - use 'En'  to specify a maximum roundoff error less than 1.2e-14.
  - trace execution with 'T3' to see the determinant for each point.
If the input is lower dimensional:
  - use 'QJ' to joggle the input and make it full dimensional
  - use 'Qbk:0Bk:0' to delete coordinate k from the input.  You should
    pick the coordinate with the least range.  The hull will have the
    correct topology.
  - determine the flat containing the points, rotate the points
    into a coordinate plane, and delete the other coordinates.
  - add one or more points to make the input full dimensional.
答案

我认为问题在于您需要确保维度与这些维度的“域”保持一致。插入到未采样的位置时,您将无法获得良好的结果。我认为你得到的错误与尝试在这些地方计算东西有关。

以下是如何在scipy.interpolate.griddata文档中创建类似于7维示例的示例。我正在使用一个更简单的函数,它只是总结了points数据中的'特征':

import numpy as np

def func(data):
    return np.sum(data, axis=1)

grid = np.mgrid[0:1:5j, 0:1:5j, 0:1:5j, 0:1:5j, 0:1:5j, 0:1:5j, 0:1:5j]

points = np.random.rand(100, 7)
values = func(points)

请注意,网格覆盖了坐标points的整个范围。也就是说,因为points中的每一列的值都在0到1的范围内,所以我应该确保我在相同的坐标上创建一个网格。

from scipy.interpolate import griddata
grid_z = griddata(points, values, tuple(grid), method='linear')

现在我有这个:

>>> grid_z[2, 2, 2, 2, 2, :, :]
array([[ nan,  nan,  nan,  nan,  nan],
       [ nan, 3.  , 3.25, 3.5 ,  nan],
       [ nan, 3.25, 3.5 , 3.75,  nan],
       [ nan, 3.5 , 3.75, 4.  ,  nan],
       [ nan,  nan,  nan,  nan,  nan]])

请注意,有很多NaN。如果你使用nearest作为方法,你总会得到一个解决方案,但当然linear插值需要两个内插插入,所以超立方体的'边'是无效的(并且7-D空间有很多边缘!)。

以上是关于不能通过scipy.interpolate.griddata对n维网格进行插值的主要内容,如果未能解决你的问题,请参考以下文章

为啥不能通过 DBLink 发送 Oracle XMLType?

为啥我们不能通过值传递数组来函数?

为啥我不能通过 LeetCode 练习 139?

为啥我不能通过 Postman 在 JSONPlaceholder 上“放置”数据?

WKWebView 不能通过键盘交互

AJAX 功能不能通过滚动工作