numpy从索引列表创建2D蒙版[+然后从蒙版数组中绘制]

时间:2014-10-21 12:28:45

标签: python arrays numpy

我有一个2-D数组值,需要屏蔽该数组的某些元素(索引取自~100k元组对的列表),然后从其余元素中抽取随机样本而不进行替换。

我需要一些非常快/高效的东西(希望避免循环)并且内存占用很小,因为实际上主阵列大约是20000 x 20000。

现在我会满足于(例如):

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值;上图是一个快捷方式。

谢谢!

相关问题:

Numpy mask based on if a value is in some other list

Mask numpy array based on index

Implementation of numpy in1d for 2D arrays?

1 个答案:

答案 0 :(得分:3)

你可以使用稀疏的铜矩阵

来有效地做到这一点
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)
相关问题