numpy的从指数的列表创建二维面具[+再由屏蔽数组绘制] [英] numpy create 2D mask from list of indices [+ then draw from masked array]

查看:162
本文介绍了numpy的从指数的列表创建二维面具[+再由屏蔽数组绘制]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值的2-D阵列和需要从剩余的元素绘制随机样本无需更换之前掩盖该阵列的某些内容(从100k的元组对的〜列表采取指数)。

I have a 2-D array of values and need to mask certain elements of that array (with indices taken from a list of ~ 100k tuple-pairs) before drawing random samples from the remaining elements without replacement.

我需要的东西,既相当快/效率(希望避免for循环),并有一个小内存占用,因为在实践中,主数组是20000〜20000点¯x

I need something that is both quite fast/efficient (hopefully avoiding for loops) and has a small memory footprint because in practice the master array is ~ 20000 x 20000.

现在我很满足于像(仅供说明):

For now I'd be content with something like (for illustration):

xys=[(1,2),(3,4),(6,9),(7,3)]

gxx,gyy=numpy.mgrid[0:100,0:100]
mask = numpy.where((gxx,gyy) not in set(xys)) # The bit I can't get right

# Now sample the masked array
draws=numpy.random.choice(master_array[mask].flatten(),size=40,replace=False)

幸运的是,现在我不需要X,绘制的通量,y坐标 - 但加分,如果你知道一个有效的方式做到这一切在一个步骤(即,它是可以接受的,我首先确定这些坐标然后用它们来获取相应的master_array值;上面的图是一个快捷方式)

Fortunately for now I don't need the x,y coordinates of the drawn fluxes - but bonus points if you know an efficient way to do this all in one step (i.e. it would be acceptable for me to identify those coordinates first and then use them to fetch the corresponding master_array values; the illustration above is a shortcut).

谢谢!

链接的问题:

<一个href=\"http://stackoverflow.com/questions/13629061/numpy-mask-based-on-if-a-value-is-in-some-other-list\">Numpy面膜的基础上,如果值是其他一些列表

面膜基于索引 numpy的阵列

numpy的in1d二维数组?

推荐答案

您可以将它efficently使用稀疏矩阵首席运营官做

You can do it efficently using sparse coo matrix

from scipy import sparse
xys=[(1,2),(3,4),(6,9),(7,3)]

coords = zip(*xys)
mask = sparse.coo_matrix((numpy.ones(len(coords[0])), coords ), shape= master_array.shape, dtype=bool)
draws=numpy.random.choice( master_array[~mask.toarray()].flatten(), size=10)

这篇关于numpy的从指数的列表创建二维面具[+再由屏蔽数组绘制]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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