4、Numpy排序、搜索和计数函数

18 篇文章 0 订阅

系列文章:Numpy系列文章-CSDN博客      

目录

np.sort:

示例 1:一维数组排序

示例 2:二维数组按行排序

示例 3:二维数组按列排序

示例 4:在排序中使用不同的排序算法

示例 5:对结构化数组排序

np.argsort

示例 1:一维数组排序索引

示例 2:二维数组按行排序索引

示例 3:二维数组按列排序索引

示例 4:使用索引数组对多个数组按相同顺序排序

np.argmax,np.argmin

示例 1:一维数组

示例 2:二维数组

示例 3:多维数组

np.nonzero

示例 1:一维数组

示例 2:二维数组

示例 3:多维数组

示例 4:使用非零索引访问元素

np.where

示例 1:获取满足条件的元素的索引

示例 2:使用 np.where() 进行条件选择

示例 3:多维数组中获取满足条件的元素的索引

示例 4:使用 np.where() 替换数组中的元素

示例 5:结合 np.where() 和 np.nonzero() 使用


numpy其余常用函数参考:

3、Numpy中的通用函数-CSDN博客在 NumPy 中,通用函数(通常称为ufunc)是一种对 ndarray 中的数据执行元素级运算的函数。这些函数是高度优化的、快速的向量化包装器,提供了简单的方法对数组中的所有元素执行相同的操作,而不需要编写循环语句。https://blog.csdn.net/sinat_34461199/article/details/135632987?spm=1001.2014.3001.5502

np.sort:

np.sort() 函数返回输入数组的排序副本。

示例 1:一维数组排序
import numpy as np 
# 创建一维数组 
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) 
# 对数组进行排序 
sorted_x = np.sort(x) 
print("Sorted array:", sorted_x)

输出:

Sorted array: [1 1 2 3 3 4 5 5 5 6 9] 
示例 2:二维数组按行排序
import numpy as np 
# 创建二维数组 
x = np.array([[7, 4, 2], [3, 9, 1]]) 
# 对数组的每一行进行排序 
sorted_x_by_row = np.sort(x, axis=1) 
print("Sorted array by row:") 
print(sorted_x_by_row)

输出:

Sorted array by row: [[2 4 7] [1 3 9]] 
示例 3:二维数组按列排序
import numpy as np 
# 创建二维数组 
x = np.array([[7, 4, 2], [3, 9, 1]]) 
# 对数组的每一列进行排序 
sorted_x_by_column = np.sort(x, axis=0) 
print("Sorted array by column:") 
print(sorted_x_by_column)

输出:

Sorted array by column: [[3 4 1] [7 9 2]] 
示例 4:在排序中使用不同的排序算法
import numpy as np 
# 创建一维数组 
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) 
# 对数组进行排序,使用快速排序算法 
sorted_x_quick = np.sort(x, kind='quicksort') 
print("Sorted array with quicksort:", sorted_x_quick) 
# 对数组进行排序,使用归并排序算法 
sorted_x_merge = np.sort(x, kind='mergesort') 
print("Sorted array with mergesort:", sorted_x_merge) 
# 对数组进行排序,使用堆排序算法 
sorted_x_heap = np.sort(x, kind='heapsort') 
print("Sorted array with heapsort:", sorted_x_heap)

输出:

Sorted array with quicksort: [1 1 2 3 3 4 5 5 5 6 9] 
Sorted array with mergesort: [1 1 2 3 3 4 5 5 5 6 9] 
Sorted array with heapsort: [1 1 2 3 3 4 5 5 5 6 9] 
示例 5:对结构化数组排序
import numpy as np 
# 创建一个结构化数组 
dtype = [('name', 'S10'), ('height', float), ('age', int)] 
values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] 
a = np.array(values, dtype=dtype) 
# 按 'height' 字段排序 
sorted_a_by_height = np.sort(a, order='height') 
print("Sorted array by height:") 
print(sorted_a_by_height) 
# 按 'age' 字段排序 
sorted_a_by_age = np.sort(a, order='age') 
print("Sorted array by age:") 
print(sorted_a_by_age)

输出:

Sorted array by height: [(b'Galahad', 1.7, 38) (b'Arthur', 1.8, 41) (b'Lancelot', 1.9, 38)] 
Sorted array by age: [(b'Lancelot', 1.9, 38) (b'Galahad', 1.7, 38) (b'Arthur', 1.8, 41)] 

        注意,np.sort() 函数默认使用快速排序算法(kind='quicksort'),但也可以指定其他排序算法,如归并排序(kind='mergesort')和堆排序(kind='heapsort')。排序算法的选择可能会根据数组的大小和内容影响性能。结构化数组的排序可以通过指定 order 参数来根据特定字段进行排序。

np.argsort

np.argsort() 函数返回的是数组元素从小到大排序后的索引值数组。这意味着如果使用这些索引来索引原始数组,你将得到一个排序后的数组。

示例 1:一维数组排序索引
import numpy as np 

# 创建一维数组 
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]) 

# 获取排序后的索引 
sorted_indices = np.argsort(x) 
print("Indices of sorted array:", sorted_indices) 

# 使用索引对原数组进行排序 
sorted_x = x[sorted_indices] 
print("Sorted array using indices:", sorted_x)

输出:

Indices of sorted array: [ 1 3 6 0 9 2 4 8 10 7 5] 
Sorted array using indices: [1 1 2 3 3 4 5 5 5 6 9] 
示例 2:二维数组按行排序索引
import numpy as np

# 创建二维数组 
x = np.array([[7, 4, 2], [3, 9, 1]]) 

# 对每一行的元素获取排序后的索引 
sorted_indices_by_row = np.argsort(x, axis=1) 
print("Indices of sorted array by row:") 
print(sorted_indices_by_row) 

# 使用索引按行对原数组进行排序,
sorted_x_by_row = np.take_along_axis(x, sorted_indices_by_row, axis=1) 
print("Sorted array by row using indices:") 
print(sorted_x_by_row)

输出:

Indices of sorted array by row: 
[[2 1 0] 
[2 0 1]] 
Sorted array by row using indices: 
[[2 4 7] 
[1 3 9]] 

 注意,使用np.take_along_axis() 函数来根据索引数组对原数组进行排序。

示例 3:二维数组按列排序索引
import numpy as np 

# 创建二维数组 
x = np.array([[7, 4, 2], [3, 9, 1]]) 

# 对每一列的元素获取排序后的索引 
sorted_indices_by_column = np.argsort(x, axis=0) 
print("Indices of sorted array by column:") 
print(sorted_indices_by_column) 

# 使用索引按列对原数组进行排序 
sorted_x_by_column = np.take_along_axis(x, sorted_indices_by_column, axis=0) 
print("Sorted array by column using indices:") 
print(sorted_x_by_column)

输出:

Indices of sorted array by column: 
[[1 0 1] 
[0 1 0]] 
Sorted array by column using indices: 
[[3 4 1] 
[7 9 2]] 
示例 4:使用索引数组对多个数组按相同顺序排序
import numpy as np 

# 创建两个一维数组 
x = np.array([3, 1, 2]) 
y = np.array([9, 8, 7]) 

# 对 x 数组获取排序后的索引 
sorted_indices = np.argsort(x) 

# 使用相同的索引对两个数组排序 
sorted_x = x[sorted_indices] 
sorted_y = y[sorted_indices] 
print("Sorted x:", sorted_x) 
print("Sorted y:", sorted_y)

输出:

Sorted x: 
[1 2 3] 
Sorted y: 
[8 7 9] 

np.argmaxnp.argmin

np.argmax()np.argmin() 函数分别用于找出数组中最大值和最小值的索引。

示例 1:一维数组
import numpy as np

# 创建一维数组
x = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])

# 找出最大值的索引
max_index = np.argmax(x)
print("Index of the maximum element:", max_index)

# 找出最小值的索引
min_index = np.argmin(x)
print("Index of the minimum element:", min_index)

输出:

Index of the maximum element: 5 
Index of the minimum element: 1 
示例 2:二维数组
import numpy as np

# 创建二维数组
x = np.array([[7, 4, 2], [3, 9, 1]])

# 找出全局最大值的索引
max_index_global = np.argmax(x)
print("Index of the maximum element (global):", max_index_global)

# 找出全局最小值的索引
min_index_global = np.argmin(x)
print("Index of the minimum element (global):", min_index_global)

# 沿特定轴(行)找出最大值的索引
max_index_by_row = np.argmax(x, axis=1)
print("Indices of the maximum elements by row:", max_index_by_row)

# 沿特定轴(列)找出最小值的索引
min_index_by_column = np.argmin(x, axis=0)
print("Indices of the minimum elements by column:", min_index_by_column)

输出:

Index of the maximum element (global): 4 
Index of the minimum element (global): 5 
Indices of the maximum elements by row: [0 1] 
Indices of the minimum elements by column: [1 0 1] 

注意,全局索引是将数组展平后的一维索引,如果需要将这个索引转换为二维索引,可以使用 np.unravel_index()。沿特定轴的索引是相对于该轴的,因此每个轴的索引从0开始。

示例 3:多维数组
import numpy as np

# 创建三维数组
x = np.array([[[1, 5, 2], [8, 7, 3]], [[4, 0, 9], [6, 10, 11]]])

# 找出全局最大值的索引
max_index_global = np.argmax(x)
print("Index of the maximum element (global):", max_index_global)

# 沿特定轴(深度)找出最大值的索引
max_index_by_depth = np.argmax(x, axis=0)
print("Indices of the maximum elements by depth:")
print(max_index_by_depth)

# 沿特定轴(行)找出最大值的索引
max_index_by_row = np.argmax(x, axis=1)
print("Indices of the maximum elements by row:")
print(max_index_by_row)

# 沿特定轴(列)找出最大值的索引
max_index_by_column = np.argmax(x, axis=2)
print("Indices of the maximum elements by column:")
print(max_index_by_column)

输出:

Index of the maximum element (global): 11 
Indices of the maximum elements by depth: 
[[1 1 1] 
[0 1 1]] 
Indices of the maximum elements by row: 
[[1 0 1] 
[1 1 1]] 
Indices of the maximum elements by column: 
[[1 0] 
[2 2]] 

        对于 np.argmax()np.argmin(),在不指定轴的情况下,默认返回展平后的一维索引;可以通过指定 axis 参数来获取特定轴上元素的索引。

np.nonzero

np.nonzero() 函数返回输入数组中非零元素的索引。对于一维数组,它返回一个包含非零元素索引的元组。对于多维数组,它返回一个索引数组的元组,每个数组对应一个维度。

示例 1:一维数组
import numpy as np 

# 创建一维数组 
x = np.array([0, 2, 0, 3, 0, 0, 5]) 

# 获取非零元素的索引 
nonzero_indices = np.nonzero(x) 
print("Indices of non-zero elements:", nonzero_indices[0])

输出:

Indices of non-zero elements: [1 3 6] 
示例 2:二维数组
import numpy as np 

# 创建二维数组 
x = np.array([[0, 1, 0], [2, 0, 3], [0, 0, 0]]) 

# 获取非零元素的索引 
nonzero_indices = np.nonzero(x) 
print("Row indices of non-zero elements:", nonzero_indices[0]) 
print("Column indices of non-zero elements:", nonzero_indices[1])

输出:

Row indices of non-zero elements: [0 1 1] 
Column indices of non-zero elements: [1 0 2] 
示例 3:多维数组
import numpy as np 

# 创建三维数组 
x = np.array([[[0, 1, 0], [2, 0, 3]], [[0, 0, 0], [4, 5, 0]]]) 

# 获取非零元素的索引 
nonzero_indices = np.nonzero(x) 
print("Depth indices of non-zero elements:", nonzero_indices[0]) 
print("Row indices of non-zero elements:", nonzero_indices[1]) 
print("Column indices of non-zero elements:", nonzero_indices[2])

输出:

Depth indices of non-zero elements: [0 0 0 1 1] 
Row indices of non-zero elements: [0 1 1 1 1] 
Column indices of non-zero elements: [1 0 2 0 1] 
示例 4:使用非零索引访问元素
import numpy as np 

# 创建二维数组 
x = np.array([[0, 1, 0], [2, 0, 3], [4, 0, 0]]) 5

# 获取非零元素的索引 
nonzero_indices = np.nonzero(x) 

# 访问非零元素 
nonzero_elements = x[nonzero_indices] 
print("Non-zero elements:", nonzero_elements)

输出:

Non-zero elements: [1 2 3 4]

np.where

np.where() 函数是一个多功能函数,它可以用于获取满足条件的元素的索引,或者根据一个条件从两个选项中选择元素。 

示例 1:获取满足条件的元素的索引
import numpy as np 

# 创建数组 
x = np.array([1, 2, 3, 4, 5]) 

# 获取满足条件的元素的索引(大于3的元素) 
indices = np.where(x > 3) 
print("Indices of elements greater than 3:", indices[0])

输出:

Indices of elements greater than 3: [3 4] 
示例 2:使用 np.where() 进行条件选择
import numpy as np 

# 创建数组 
x = np.array([1, 2, 3, 4, 5]) 

# 根据条件选择元素:如果元素大于3,选择该元素,否则选择-该元素 
result = np.where(x > 3, x, -x) 
print("Result of np.where with condition:", result)

预期输出:

Result of np.where with condition: [-1 -2 -3 4 5] 
示例 3:多维数组中获取满足条件的元素的索引
import numpy as np 

# 创建二维数组 
x = np.array([[0, 1, 2], [3, 4, 5]]) 

# 获取满足条件的元素的索引(等于4的元素) 
indices = np.where(x == 4) 
print("Indices of elements equal to 4:") 
print("Row indices:", indices[0]) 
print("Column indices:", indices[1])

输出:

Indices of elements equal to 4: 
Row indices: [1] 
Column indices: [1] 
示例 4:使用 np.where() 替换数组中的元素
import numpy as np 

# 创建数组 
x = np.array([1, 0, 3, 0, 5]) 

# 使用 np.where 替换所有0为-1 
result = np.where(x == 0, -1, x) 
print("Array after replacing 0s with -1s:", result)

输出:

Array after replacing 0s with -1s: [ 1 -1 3 -1 5] 
示例 5:结合 np.where() 和 np.nonzero() 使用
import numpy as np 

# 创建二维数组 
x = np.array([[0, 1, 2], [3, 4, 5]]) 

# 使用 np.nonzero 获取非零元素的索引 
nonzero_indices = np.nonzero(x) 

# 使用 np.where 进行同样的操作 
where_indices = np.where(x != 0) 

# 输出结果,两种方法应该给出相同的结果 
print("Indices from np.nonzero:", nonzero_indices) 
print("Indices from np.where:", where_indices)

预期输出:

Indices from np.nonzero: (array([0, 1, 1, 1]), array([1, 0, 1, 2])) 
Indices from np.where: (array([0, 1, 1, 1]), array([1, 0, 1, 2]))
  • 43
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值