1 回答

TA貢獻2039條經驗 獲得超8個贊
手動將其轉換為 int 元組可以實現您所尋求的索引。測試下面的代碼。
import numpy as np
# your example had d=3, but you may have something else.
# this is just creation of some random data...
d=3
answers = np.random.normal(size=[2] * d)
truth = tuple(np.random.choice([True, False], replace=True, size=d))
# manual conversion to int-tuple is needed, for numpy to understand that it
# is a indexing tuple.
# the box at the top of https://numpy.org/doc/stable/reference/arrays.indexing.html
# says that tuple indexing is just like normal indexing, and in that case, we
# need to have normal ints.
idx = tuple(int(b) for b in truth)
answer = answers[idx]
print(answer)
添加回答
舉報