Python 按照NumPy中的第n列对数组进行排序

Python 按照NumPy中的第n列对数组进行排序

在本篇文章中,我们将展示如何使用Python在NumPy中按照第n列对数组进行升序和降序排序。

NumPy 是一个设计用于在Python中高效处理数组的库。它快速、易学,并且在存储上很高效。它还改进了数据处理的方式。在NumPy中,我们可以生成一个n维数组。要使用NumPy,我们只需将其导入到我们的程序中,然后我们可以轻松地在我们的代码中使用NumPy的功能。

方法1. 按照第1列对Numpy数组进行排序

在这种方法中,我们使用随机模块生成一个随机的NumPy数组,并按照数组的第1列进行升序排序(默认情况下)。

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字,导入别名为np的numpy模块。

  • 使用random.randint()方法(返回指定范围内的随机数),创建一个形状为6×6的NumPy数组,其中包含从0到19的任意随机数。

  • 打印输入数组。

  • 使用argsort()函数(使用由kind关键字提供的算法对指定轴进行间接排序。它返回将数组排序的索引),通过第1列对输入数组进行排序,并按照第1列的升序打印结果排序后的数组。

示例

以下程序使用argsort()函数按照第1列的升序对NumPy数组进行排序并返回结果:

# 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数组进行排序

在这个方法中,我们使用random模块生成一个随机的NumPy数组,并按照给定的第n列对数组进行升序排序(默认)。

步骤

下面是执行所需任务的算法/步骤:

  • 使用random.randint()方法创建一个4×4形状的NumPy数组,并使用0到99之间的任意随机数填充数组,并打印输入数组。

  • 输入n值并创建一个变量来存储它。

  • 使用argsort()函数按照第n列对输入数组进行排序,并按照第一列的升序打印结果排序后的数组。

示例

下面的程序使用argsort()函数按照给定的第n列按升序排序NumPy数组,并返回结果:

# 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数组进行逆序排序

在这种方法中,我们使用random模块生成一个随机的NumPy数组,并通过倒序切片的方式按照数组的第 n 列进行 降序 排序。

示例

以下程序使用argsort()函数按照给定的第n列对NumPy数组进行 降序 排序,并返回结果:

# 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列对数组进行排序,以及如何使用切片按降序进行排序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程