Python NumPy take()函数示例

Python NumPy take()是一个内置的NumPy函数,用于沿提到的轴和索引从数组中返回元素。就是说,我们将能够通过数组的索引获取数组的元素,如果提到了轴,那么在该索引处存在的所有元素都将沿轴打印。

Python NumPy take()

Python NumPy take()函数沿轴从数组中获取元素。 take()函数与“奇特”索引(使用数组索引数组)具有相同的作用;但是,如果您需要沿给定轴的项目,则使用起来会更容易。

句法

numpy.take(array, indices, axis = None, out = None, mode =’raise’)

参量

take()函数最多可以使用5个参数:

  • 数组:这是我们将要处理的数组。
  • 索引:这些是要收集的值的索引。
  • 轴:此字段是可选的。这是我们必须获取元素的轴。默认值为“无”;在这种情况下,阵列将变平。
  • 模式:三种类型的模式适用于越界工作方式:
    • 引发:在越界的情况下引发错误
    • 扭曲:扭曲
    • 剪辑:剪辑到范围

请记住,该字段是可选的。

返回值

take()函数返回具有相同类型的nD数组。

没有任何模式的take()工作

请参阅以下代码。

#Importing numpy
import numpy as np

#We will create a 2D array
#Of shape 4x3
arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
#Printing the array
print("The array is: ")
print(arr)

#Printing values without mentioning axis
indices = [2, 7]
print("Values at position 2 and 7 are: ", np.take(arr, indices))

#Printing valoues with axis
indices = [0, 2]
print("Values at position 0 and 2 are: n", np.take(arr, indices, axis=1))

输出量

The array is:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [50 51 52]]
Values at position 2 and 7 are:  [3 8]
Values at position 0 and 2 are:
 [[ 1  3]
 [ 4  6]
 [ 7  9]
 [50 52]]

说明

在此示例中,我们首先声明了一个3×4形状的数组,并打印了该数组。

然后,我们想在take()的帮助下通过给出其索引来打印值。

首先,我们没有提到轴就调用了take(),我们可以看到在索引中,我们给定了值 [2,7],因为未提及轴,所以将数组弄平,并打印索引2和7处的值。

在第二种情况下,我们提到axis = 1,它将按列打印值,而index =[0,2]。

我们可以看到我们得到的结果是2×4矩阵。 take()具有列的打印值,该列的索引分别为0和2。

在模式下使用take()

请参阅以下代码。

#Importing numpy
import numpy as np

#We will create a 2D array
#Of shape 4x3
arr = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9), (50, 51, 52)])
#Printing the array
print("The array is: ")
print(arr)

#Printing values mode=warp
indices = [0, 6]
print("Values at position 0 and 6 are [mode=warp]:n ",
      np.take(arr, indices, axis=1, mode='warp'))

#Printing values mode=clip
indices = [1, 5]
print("Values at position 1 and 5 are [mode=clip]:n ",
      np.take(arr, indices, axis=1, mode='clip'))

#Printing values mode=raise
indices = [2, 7]
print("Values at position 2 and 7 are [mode=raise]:n ",
      np.take(arr, indices, axis=1, mode='raise'))

输出量

The array is:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [50 51 52]]
Values at position 0 and 6 are [mode=warp]:
  [[ 1  1]
 [ 4  4]
 [ 7  7]
 [50 50]]
Values at position 1 and 5 are [mode=clip]:
  [[ 2  3]
 [ 5  6]
 [ 8  9]
 [51 52]]
Traceback (most recent call last):
  File "take2.py", line 21, in 
	print("Values at position 2 and 7 are [mode=raise]:n ",np.take(arr,indices,axis=1,mode='raise'))
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 189, in take
	return _wrapfunc(a, 'take', indices, axis=axis, out=out, mode=mode)
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 56, in _wrapfunc
	return getattr(obj, method)(*args, **kwds)
IndexError: index 7 is out of bounds for size 3

说明

在此示例中,我们声明了一个3×4形状的数组,并打印了该数组。

我们可以看到在第一种情况下,我们给定了索引值 [0,6] 并且轴= 1,我们没有这样一个索引为6的轴。

当我们调用take()时,我们已经传递了mode =’warp’,因为我们试图使索引越界,所以’warp’模式扭曲了数组并打印了值,而没有任何错误。

在第二种情况下,我们给定了mode =’clip’,我们可以看到该数组已被裁剪并打印了值。

最后,我们给定了mode =’raise’,因为这种模式在超出范围的情况下会引发错误,因此输出中会出现错误。

也可以看看

Python NumPy amin()

Python NumPy nanargmax()

Python NumPy nanargmin()

Python NumPy argmin()

Python NumPy argmax()

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢