編寫Python程式碼,使用NumPy按第n列對陣列進行排序?


在本文中,我們將向您展示如何在 Python 中按升序和降序對 NumPy 陣列的第 n 列進行排序。

NumPy 是一個 Python 庫,旨在高效地處理 Python 中的陣列。它速度快、易於學習且儲存效率高。它還改進了資料處理的方式。在 NumPy 中,我們可以生成一個 n 維陣列。要使用 NumPy,我們只需將其匯入到我們的程式中,然後我們就可以輕鬆地在程式碼中使用 NumPy 的功能。

方法 1. 按第 1 列對 NumPy 陣列排序

在此方法中,我們使用隨機模組生成一個隨機 NumPy 陣列,並按升序(預設)對陣列的第 1 列進行排序。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 import 關鍵字,匯入帶有別名(np)的 numpy 模組。

  • 使用 random.randint() 方法(返回指定範圍內的隨機數)建立一個形狀為 6x6 的 NumPy 陣列,其中包含 0 到 19 之間的任意隨機數。

  • 列印輸入陣列。

  • 使用 argsort() 函式(沿著指定的軸使用 kind 關鍵字提供的演算法執行間接排序。它返回將對陣列進行排序的索引),按第 1 列對輸入陣列進行排序,並列印按第 1 列升序排序的結果陣列。

示例

以下程式使用 argsort() 函式按升序對 NumPy 陣列的第 1 列進行排序並返回結果:

# importing NumPy module with an alias name import numpy as np # creating a NumPy array of any random numbers from 0 to 19 of shape 6*6 inputArray = np.random.randint(0,20,(5,5)) # printing the input array print("The input random array is:") print(inputArray) # using argsort() function, to sort the input array by the 1st column print("Sorting the input array by the 1st column:") # Here in [:,1], ':' specifies all the rows and 1 specifies we need to sort by 1st column print(inputArray[inputArray[:,1].argsort()])

輸出

執行上述程式後,將生成以下輸出:

The input random array is:
[[ 8 16  7  5 13]
 [ 5  8  6  0  2]
 [11  7  3  3  6]
 [ 5  3 15  7  5]
 [15  3  9 14  3]]
Sorting the input array by the 1st column:
[[ 5  3 15  7  5]
 [15  3  9 14  3]
 [11  7  3  3  6]
 [ 5  8  6  0  2]
 [ 8 16  7  5 13]]

方法 2. 按第 n 列對 NumPy 陣列排序

在此方法中,我們使用隨機模組生成一個隨機 NumPy 陣列,並按升序(預設)對陣列的給定 第 n 列進行排序。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 random.randint() 方法建立一個形狀為 4x4 的 NumPy 陣列,其中包含 0 到 99 之間的任意隨機數,並列印輸入陣列。

  • 輸入 n 值並建立一個變數來儲存它。

  • 使用 argsort() 函式,按第 n 列對輸入陣列進行排序,並列印按第 1 列升序排序的結果陣列。

示例

以下程式使用 argsort() 函式按升序對 NumPy 陣列的給定第 n 列進行排序並返回結果:

# importing NumPy module with an alias name import numpy as np # creating a NumPy array of any random numbers from 0 to 100 of shape 4x4 inputArray = np.random.randint(0,100,(4,4)) # printing the input array print("The input random array is:") print(inputArray) # entering the value of n n = 2 # using argsort() function, to sort the input array by the nth column # here we are sorting the nth column of the array print("Sorting the input array by the",n,"column:") # Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column print(inputArray[inputArray[:,n].argsort()])

輸出

執行上述程式後,將生成以下輸出:

The input random array is:
[[32  5 68 67]
 [ 7  7 12 63]
 [49 49 10 15]
 [96  5 93 29]]
Sorting the input array by the 2 column:
[[49 49 10 15]
 [ 7  7 12 63]
 [32  5 68 67]
 [96  5 93 29]]

方法 3. 按第 n 列的逆序對 NumPy 陣列排序

在此方法中,我們使用隨機模組生成一個隨機 NumPy 陣列,並透過將其切片為逆序來按降序對陣列的給定第 n 列進行排序。

示例

以下程式使用 argsort() 函式按降序對 NumPy 陣列的給定第 n 列進行排序並返回結果:

# importing numpy module with an alias name import numpy as np # creating a numpy array of any random numbers from 0 to 100 of shape 5*5 inputArray = np.random.randint(0,100,(5,5)) # printing the input array print("The input random array is:") print(inputArray) # entering the value of n n = 3 print("Sorting the input array by the",n,"column:") # Here in [:,n], ':' specifies all the rows and n specifies we need to sort by nth column # [::-1] slices the result in reverse order(descending order) print(inputArray[inputArray[:,n].argsort()[::-1]])

輸出

執行上述程式後,將生成以下輸出:

The input random array is:
[[47  6 62 53 50]
 [ 5 30 13 10 33]
 [43 93 57 91 91]
 [84 40 21 55  9]
 [76 89 85  3  4]]
Sorting the input array by the 3 column:
[[43 93 57 91 91]
 [84 40 21 55  9]
 [47  6 62 53 50]
 [ 5 30 13 10 33]
 [76 89 85  3  4]]

結論

在本文中,我們學習瞭如何在 NumPy 中按第 n 列對陣列進行排序,以及如何使用切片按降序對陣列進行排序。

更新於:2022年10月20日

3K+ 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告