您可以使用
itertools.combinations()创建索引数组,然后使用NumPy的精美索引:
import numpy as npfrom itertools import combinations, chainfrom scipy.special import combdef comb_index(n, k): count = comb(n, k, exact=True) index = np.fromiter(chain.from_iterable(combinations(range(n), k)), int, count=count*k) return index.reshape(-1, k)data = np.array([[1,2,3,4,5],[10,11,12,13,14]])idx = comb_index(5, 3)print(data[:, idx])
输出:
[[[ 1 2 3] [ 1 2 4] [ 1 2 5] [ 1 3 4] [ 1 3 5] [ 1 4 5] [ 2 3 4] [ 2 3 5] [ 2 4 5] [ 3 4 5]] [[10 11 12] [10 11 13] [10 11 14] [10 12 13] [10 12 14] [10 13 14] [11 12 13] [11 12 14] [11 13 14] [12 13 14]]]



