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, inprint("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()