np.take()函数用法 python numpy

np.take()函数用法-简记

np.take(a, indices, axis=None, out=None, mode='raise')

作用: 沿轴从数组中获取元素。(见例题注释)

np.take?    #可查询官方文档

例题1:

import numpy as np

a = np.array([[1,2,4,([1,2,6])],
              [3,2,6,([6,5,1])],
              [6,9,4,([3,7,5])]], dtype=object)
print(a.take(indices=1,axis=0)) # np.take(a, indices, axis=None, out=None, mode='raise')
print(a.take(indices=2,axis=0))  # axis=0 按行;  axis=1 按列  (二维数组,so,只能取0或1)
print(a.take(indices=0,axis=1))
print(a.take(indices=3,axis=1))

输出:

[3 2 6 list([6, 5, 1])]
[6 9 4 list([3, 7, 5])]
[1 3 6]
[list([1, 2, 6]) list([6, 5, 1]) list([3, 7, 5])]

例题2:

import numpy as np

b = np.array([[1, 2, 4, ([1, 2, 5])], 
             [3, 2, 6, ([6, 5, 1])],
             [6, 9, 4, ([3, 7, 5])]], dtype=object)
print(b.take(1,1))  # 按列,取第1列
print(b.take(0,1)) # 按列,取第0列

print(b.take(0,0)) # 按行,取第0行
print(b.take(1,0)) # 按行,取第1行

输出:

[2 2 9]
[1 3 6]
[1 2 4 list([1, 2, 5])]
[3 2 6 list([6, 5, 1])]

你可能感兴趣的:(Python)