给定一个大小为 MxN 的数组和一个大小为 Mx1 的数组,我想用 MxN 计算一个布尔数组。
import numpy as np
M = 2
N = 3
a = np.random.rand(M, N) # The values doesn't matter
b = np.random.choice(a=N, size=(M, 1), replace=True)
# b =
# array([[2],
# [1]])
# I found this way to compute the boolean array but I wonder if there's a fancier, elegant way
index_array = np.array([np.array(range(N)), ]*M)
# Create an index array
# index_array =
# array([[0, 1, 2],
# [0, 1, 2]])
#
boolean_array = index_array == b
# boolean_array =
# array([[False, False, True],
# [False, True, False]])
#
所以我想知道是否有一种更高级的pythonic方式来做到这一点