루프없이이 결과를 얻는 방법이 있습니까? 나는 W[range(W.shape[0]),
으로 멋진 색인 생성에 몇 가지 시도를했지만 ... 지금까지 성공하지 못했습니다.반복하지 않고 itertools.combinations에서 numpy 배열을 만드는 방법
import itertools
import numpy as np
n = 4
ct = 2
one_index_tuples = list(itertools.combinations(range(n), r=ct))
W = np.zeros((len(one_index_tuples), n), dtype='int')
for row_index, col_index in enumerate(one_index_tuples):
W[row_index, col_index] = 1
print(W)
결과 :
[[1 1 0 0]
[1 0 1 0]
[1 0 0 1]
[0 1 1 0]
[0 1 0 1]
[0 0 1 1]]
가능한 중복 튜플리스트로 배열] (http : // stackoverflow .com/questions/28491230/indexing-a-numpy-array-of-a-a-list-of-tuples) – maxymoo