numpy布尔索引

2024-09-28 22:31:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用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,)

花哨/布尔索引在这里不起作用。你知道吗


Tags: intestnumpyfortestsvaluesshape花哨
1条回答
网友
1楼 · 发布于 2024-09-28 22:31:22

这样怎么样? 如果您只想快速获得每行的大小。你知道吗

>>> 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,)

相关问题 更多 >