當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.take()用法及代碼示例

numpy.take(array, indices, axis = None, out = None, mode =’raise’) :沿提到的軸和索引從數組返回elememts。
參數:

array   : array_like, input array
indices : index of the values to be fetched
axis    : [int, optional] axis over which we need to fetch the elements; 
                  By Default[axis = None], flattened input is used
mode    : [{‘raise’, ‘wrap’, ‘clip’}, optional] mentions how out-of-bound indices will behave
                  raise : [default]raise an error 
                  wrap  : wrap around
                  clip  : clip to the range
out     : [ndarray, optional]to place result within array

返回值:

ndarray; array has the same type
# Python Program illustrating 
# numpy.take method 
  
import numpy as geek 
  
#array = geek.arange(10).reshape(2, 5) 
array = [[5, 6, 2, 7, 1], 
         [4, 9, 2, 9, 3]] 
print("Original array : \n", array) 
  
# indices = [0, 4] 
print("\nTaking Indices\n", geek.take(array, [0, 4])) 
  
# indices = [0, 4] with axis = 1 
print("\nTaking Indices\n", geek.take(array, [0, 4], axis = 1))

輸出:


Original array : 
 [[5, 6, 2, 7, 1], [4, 9, 2, 9, 3]]

Taking Indices
 [5 1]

Taking Indices
 [[5 1]
 [4 3]]


相關用法


注:本文由純淨天空篩選整理自 numpy.take() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。