Python 区别np。在哪里和这个工作?

Python 区别np。在哪里和这个工作?,python,numpy,Python,Numpy,在问题“”中,要求将低密度区域替换为NAN。接受答案中的相关代码如下所示: hh, locx, locy = scipy.histogram2d(xdat, ydat, range=xyrange, bins=bins) posx = np.digitize(xdat, locx) posy = np.digitize(ydat, locy) #select points within the histogram ind = (posx > 0) & (posx <= bi

在问题“”中,要求将低密度区域替换为NAN。接受答案中的相关代码如下所示:

hh, locx, locy = scipy.histogram2d(xdat, ydat, range=xyrange, bins=bins)
posx = np.digitize(xdat, locx)
posy = np.digitize(ydat, locy)

#select points within the histogram
ind = (posx > 0) & (posx <= bins[0]) & (posy > 0) & (posy <= bins[1])
hhsub = hh[posx[ind] - 1, posy[ind] - 1] # values of the histogram where the points are
xdat1 = xdat[ind][hhsub < thresh] # low density points
ydat1 = ydat[ind][hhsub < thresh]
hh[hh < thresh] = np.nan # fill the areas with low density by NaNs
他也在工作。两者在结果执行方面有什么区别?

当结果相同时,高级索引(即您的原始方法)更有效

我将这两个选项与以下代码进行了比较:

import numpy as np
import time

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a[a>0.5] = np.nan
t2 = time.time()
print 'adv. idx.: ',t2-t1

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a = np.where(a>0.5,np.nan,a)
t2 = time.time()
print 'np.where: ',t2-t1
结果相当明显:

adv. idx.:  0.00600004196167
np.where:  0.0339999198914

np.其中
明显较慢!结果相同。但是,这无法通过比较进行验证,因为
np.nan==np.nan
会产生
False

您最初的方法称为高级索引,它是一种功能,而不是一种解决方法@这不是我的方法,我只是复制并粘贴了它,它成功了。后来我发现np.where()也做了这项工作,用更少的行(我理解这一点,最重要的是)来看一下
np.where(hh>thresh)
。尝试使用此元组选择数组元素。它可能有助于您理解较长形式的
的作用。但是索引的全部麻烦是什么,以及获取x/y数据的位置等等?
thresh
没有在您的代码片段中定义,因此我不知道
xdat,ydat
的处理如何影响
thresh
。在我看来,问题是关于
hh[hh
hh=np.where(hh>thresh,hh,np.nan)
之间的区别。实际上,
np.where(hh>thresh,hh,np.nan)
只能替换
hh[hh
,其余的工作必须在别处完成,例如,如代码片段中的第1-9行所示!
adv. idx.:  0.00600004196167
np.where:  0.0339999198914