2 回答
TA贡献1878条经验 获得超4个赞
itertools.combinations 是生成列索引组合的简便方法:
In [182]: itertools.combinations(range(4),3)
Out[182]: <itertools.combinations at 0x7f7dc41cb5e8>
In [183]: list(_)
Out[183]: [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]
使用它来生成子数组D:
In [184]: D = np.arange(12).reshape(3,4)
In [185]: alist = []
In [186]: for tup in itertools.combinations(range(4),3):
...: alist.append(D[:, tup])
...:
In [187]: alist
Out[187]:
[array([[ 0, 1, 2],
[ 4, 5, 6],
[ 8, 9, 10]]),
array([[ 0, 1, 3],
[ 4, 5, 7],
[ 8, 9, 11]]),
array([[ 0, 2, 3],
[ 4, 6, 7],
[ 8, 10, 11]]),
array([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])]
我不太确定你想用hstack. 将这些子数组组合成一个宽的可能不是您想要的:
In [188]: np.hstack(alist)
Out[188]:
array([[ 0, 1, 2, 0, 1, 3, 0, 2, 3, 1, 2, 3],
[ 4, 5, 6, 4, 5, 7, 4, 6, 7, 5, 6, 7],
[ 8, 9, 10, 8, 9, 11, 8, 10, 11, 9, 10, 11]])
添加回答
举报