numpy ix_ 学习记录

官网上给出的例子

>>> a = np.arange(10).reshape(2, 5)
>>>a
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>>ixgrid = np.ix_([0, 1], [2, 4])
>>>ixgrid
(array([[0],
[1]]), array([[2, 4]]))
#也就是说取0行和1行,拼接2列和4列
>>> ixgrid[0].shape, ixgrid[1].shape
((2, 1), (1, 2))
>>> a[ixgrid]
array([[2, 4],
[7, 9]])

>>>ixgrid = np.ix_([True, True], [2, 4])
>>>a[ixgrid]
array([[2, 4],
[7, 9]])
>>>ixgrid = np.ix_([True, True], [False, False, True, False, True])
>>> a[ixgrid]
array([[2, 4],
[7, 9]])

除了用ix_外,还可以用花式索引

>>>test=a[np.ix_([0, 1], [:,[2, 4])]
>>>test
array([[2, 4],
[7, 9]])

你可能感兴趣的:(人生苦短我用python)