griddata scipy插值不起作用(给出nan) [英] griddata scipy interpolation not working (giving nan)

查看:93
本文介绍了griddata scipy插值不起作用(给出nan)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试scipy.interpolation.griddata帮助文件中给出的2d示例.它适用于最近"插值.但是当使用任何其他插值(例如线性"或三次")时,它会给出一个填充有nan的矩阵.如果我给参数fill_value = 5,它将给出由5填充的矩阵.

I was trying out the 2d example given in the scipy.interpolation.griddata help file. It works for interpolation with 'nearest'. But it gives a matrix filled with nan while using any other interpolation like 'linear' or 'cubic'. If I give the argument fill_value=5 , it gives the matrix filled with 5.

这是由于某些安装问题吗?

Is this due to some installation problem?

我正在尝试他们在帮助文档中给出的完全相同的内容.但是不知何故,它给出的结果好像我要进行插值的点位于输入点之外.(不是!我按照示例进行操作)

I was trying the exact same thing they have given in the help document. But somehow it is giving the result as if the points i asked to interpolating is lying outside the input points. (which is not!! I followed the example)

我将发布示例以重现错误(取自doc)

I shall post the example to reproduce the error (taken form doc)

def func(x, y):
    return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

grid_x, grid_y = np.mgrid[0:1:10j, 0:1:10j]
points = np.random.rand(100, 2)
values = func(points[:,0], points[:,1])

from scipy.interpolate import griddata

grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

我正在将grid_z1和grid_z2变成一个填充有nan的矩阵.

I am getting grid_z1 and grid_z2 to be a matrix filled with nan.

更新:我将所有软件包安装在另一台Ubuntu 11.10机器上.同样的脚本给出了完全正确的答案.以前,我尝试过Porteus发行版(实时slackware系列).因此,我认为我可以肯定地说这是我的安装中的一些问题.有人知道什么地方可能出问题了吗?是否有任何库冲突导致这种行为?由于我的主机是Portues,所以除了修复其中的杂物外,我别无选择.

UPDATE : I installed all the packages in another Ubuntu 11.10 machine. And the same script gave perfectly correct answer. Previously I was trying on Porteus distro (live slackware family). Hence I think i can safely conclude that this was some problem in my installation. Anybody have any idea what could have gone wrong? Does any library conflict result in this kind of behavior? Since my main machine is Portues, i have no other option than to repair the scipy in it.

推荐答案

您说的是用nan填充",但实际上并没有填充.使用您的代码但添加

You say "filled with nan", but it's not really filled. Using your code but adding

np.random.seed(7)

一开始我发现我们使用相同的数据集

at the start so that we're working with the same dataset, I find

>>> np.isnan(grid_z1).sum()
744
>>> np.isnan(grid_z2).sum()
744

这些NaN全部出现在外面的一个波段上

And these NaNs occur all on a band on the outside:

>>> np.isnan(grid_z1[5:-5,5:-5]).sum()
0

这很可能是问题所在.给出NaN的点不在指定点之内,因此它不知道如何处理它们.对于最近"插值的特殊情况,您仍然可以找到附近的东西,因此不会丢失任何NaN.

which makes it likely what the problem is. The points which are giving NaN are outside the specified points, so it doesn't know what to do with them. For the special case of "nearest" interpolation, you can still find something that's near, so you don't get any NaNs out.

因此,当您说要在其上进行插值的点不在输入点之外时,我想作一下不同:

So when you say the points to be interpolated at aren't lying outside the input points, I beg to differ:

# brute force, because I'm too lazy
from collections import Counter
d = Counter()
for x, y, val in zip(grid_x.flat, grid_y.flat, grid_z1.flat):
    pg = (points >= [x, y])
    boxed = len(set(tuple(p) for p in pg)) == 4
    d[np.isnan(val), boxed] += 1

产生

>>> d
Counter({(False, True): 19189, (True, False): 744, (False, False): 67})

没有(True,True)案件.IOW,每个NaN在点上都没有边界框.在某些(False,False)情况下,该值没有边界框但没有结束NaN,这有点令人惊讶,但是如果他们假设所有内容都包含在内,则可能取决于无聊实施详细说明了如果不执行该怎么办.简短版:我认为这里的一切可能都可以正常工作,符合预期.

And there are no (True, True) cases. IOW, every NaN lacks a bounding box in the points. There are some (False, False) cases, where the value doesn't have a bounding box but doesn't wind up a NaN, which is mildly surprising, but if they've assumed that everything is contained it would probably depend upon boring implementation details what happens if they're not. Short version: I think everything here is probably working correctly, in the sense of as expected.

这篇关于griddata scipy插值不起作用(给出nan)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆