问题描述
如何使用numpy广播重新编写此python循环?
>>> values.shape
(50000,)
>>> tests.shape # booleans
(200, 50000)
>>> extracted = values[tests]
# FAILES
>>> extracted = values[test] for test in tests]
>>> extracted.shape
(200,)
>>> extracted[0].shape
(33,)
>>> extracted[1].shape
(468,)
花式/布尔索引在这里不能像往常一样工作。
1楼
这样呢? 如果您只是想快速获取每行的大小。
>>> values.shape
(50000,)
>>> tests.shape # booleans
(200, 50000)
>>> extracted = np.prod((tests,values))
>>> extracted.shape
(200,50000)
>>> rows, cols = extracted.nonzero()
>>> cols[rows==0].shape
(33,)
>>> cols[rows==1].shape
(468,)