網站首頁 編程語言 正文
排序
排序是指以特定格式排列數據。排序算法指定以特定順序排列數據的方式。最常見的順序是數字或字典順序。在 Numpy 中,我們可以使用庫中提供的各種函數(如 sort、lexsort、argsort 等)執行各種排序操作。
numpy.sort(): 此函數返回數組的排序副本。
# 導入庫
import numpy as np
# 沿第一軸排序
a = np.array([[12, 15], [10, 1]])
arr1 = np.sort(a, axis = 0)
print ("Along first axis : \n", arr1)
# 沿最后一個軸排序
a = np.array([[10, 15], [12, 1]])
arr2 = np.sort(a, axis = -1)
print ("\nAlong first axis : \n", arr2)
a = np.array([[12, 15], [10, 1]])
arr1 = np.sort(a, axis = None)
print ("\nAlong none axis : \n", arr1)
輸出 :
Along first axis :?
?[[10 ?1]
?[12 15]]
Along first axis :?
?[[10 15]
?[ 1 12]]
Along none axis :?
?[ 1 10 12 15]
numpy.argsort(): 此函數返回將對數組進行排序的索引。
# 演示 numpy.argsort 工作的 Python 代碼
import numpy as np
# 已創建 Numpy 數組
a = np.array([9, 3, 1, 7, 4, 3, 6])
# 未排序的數組打印
print('Original array:\n', a)
# 排序數組索引
b = np.argsort(a)
print('Sorted indices of original array->', b)
# 要使用排序索引獲取排序數組 c 是由與 b 相同的 len 創建的臨時數組
c = np.zeros(len(b), dtype = int)
for i in range(0, len(b)):
c[i]= a[b[i]]
print('Sorted array->', c)
在 IDE 上運行
輸出:
Original array:
?[9 3 1 7 4 3 6]
Sorted indices of original array-> [2 1 5 4 6 3 0]
Sorted array-> [1 3 3 4 6 7 9]
numpy.lexsort(): 此函數使用一系列鍵返回間接穩定排序。
# 演示 numpy.lexsort() 工作的 Python 代碼
import numpy as np
# numpy數組創建第一列
a = np.array([9, 3, 1, 3, 4, 3, 6])
# 第二欄
b = np.array([4, 6, 9, 2, 1, 8, 7])
print('column a, column b')
for (i, j) in zip(a, b):
print(i, ' ', j)
# 按 a 然后按 b 排序
ind = np.lexsort((b, a))
print('Sorted indices->', ind)
輸出 :
column a, column b
9 ? 4
3 ? 6
1 ? 9
3 ? 2
4 ? 1
3 ? 8
6 ? 7
Sorted indices-> [2 3 1 5 4 6 0]
功能 | 描述 |
---|---|
numpy.ndarray.sort() | 就地對數組進行排序。 |
numpy.msort() | 返回沿第一個軸排序的數組的副本。 |
numpy.sort_complex() | 首先使用實部對復數數組進行排序,然后使用虛部。 |
numpy.partition() | 返回數組的分區副本。 |
numpy.argpartition() | 使用 kind 關鍵字指定的算法沿給定軸執行間接分區。 |
搜索
搜索是一種操作或技術,可幫助查找給定元素或值在列表中的位置。根據是否找到正在搜索的元素,任何搜索都被稱為成功或不成功。在 Numpy 中,我們可以使用庫中提供的各種函數(如 argmax、argmin、nanaargmax 等)執行各種搜索操作。
numpy.argmax(): 此函數返回特定軸中數組的最大元素的索引。
# 說明 argmax() 工作的 Python 程序
import numpy as geek
# 處理二維數組
array = geek.arange(12).reshape(3, 4)
print("INPUT ARRAY : \n", array)
# 沒有提到軸,所以適用于整個陣列
print("\nMax element : ", geek.argmax(array))
# 根據索引返回最大元素的索引
print(("\nIndices of Max element : "
, geek.argmax(array, axis=0)))
print(("\nIndices of Max element : "
, geek.argmax(array, axis=1)))
輸出 :
INPUT ARRAY :?
?[[ 0 ?1 ?2 ?3]
?[ 4 ?5 ?6 ?7]
?[ 8 ?9 10 11]]
Max element : ?11
Indices of Max element : ?[2 2 2 2]
Indices of Max element : ?[3 3 3]
numpy.nanargmax(): 此函數返回忽略 NaN 的特定軸中數組的最大元素的索引。如果切片僅包含 NaN 和 Infs,則結果不可信。
# 說明 nanargmax() 工作的 Python 程序
import numpy as geek
# 處理一維數組
array = [geek.nan, 4, 2, 3, 1]
print("INPUT ARRAY 1 : \n", array)
array2 = geek.array([[geek.nan, 4], [1, 3]])
# 根據忽略 NaN 的索引返回最大元素的索引
print(("\nIndices of max in array1 : "
, geek.nanargmax(array)))
# 處理二維數組
print("\nINPUT ARRAY 2 : \n", array2)
print(("\nIndices of max in array2 : "
, geek.nanargmax(array2)))
print(("\nIndices at axis 1 of array2 : "
, geek.nanargmax(array2, axis = 1)))
輸出 :
INPUT ARRAY 1 :?
?[nan, 4, 2, 3, 1]
Indices of max in array1 : ?1
INPUT ARRAY 2 :?
?[[ nan ? 4.]
?[ ?1. ? 3.]]
Indices of max in array2 : ?1
Indices at axis 1 of array2 : ?[1 1]
numpy.argmin(): 此函數返回沿軸的最小值的索引。
# 說明 argmin() 工作的 Python 程序
import numpy as geek
# 處理一維數組
array = geek.arange(8)
print("INPUT ARRAY : \n", array)
# 根據索引返回 min 元素的索引
print("\nIndices of min element : ", geek.argmin(array, axis=0))
在 IDE 上運行
輸出 :
INPUT ARRAY :?
?[0 1 2 3 4 5 6 7]
Indices of min element : ?0
功能 | 描述 |
---|---|
numpy.nanargmin() | 返回指定軸中最小值的索引,忽略 NaN。 |
numpy.argwhere() | 查找按元素分組的非零數組元素的索引。 |
numpy.nonzero() | 返回非零元素的索引。 |
numpy.flatnonzero() | 在 a 的扁平化版本中返回非零索引。 |
numpy.where() | 根據條件返回從 x 或 y 中選擇的元素。 |
numpy.searchsorted() | 查找應插入元素以保持順序的索引。 |
numpy.extract() | 返回滿足某個條件的數組元素。 |
Counting
numpy.count_nonzero() :計算數組中非零值的數量。
# 說明 count_nonzero() 工作的 Python 程序
import numpy as np
# 計算多個非零值
a = np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
b = np.count_nonzero(([[0,1,7,0,0],[3,0,0,2,19]]
, axis=0))
print("Number of nonzero values is :",a)
print("Number of nonzero values is :",b)
在 IDE 上運行
輸出 :
Number of nonzero values is : 5
Number of nonzero values is : [1, 1, 1, 1, 1]
原文鏈接:https://juejin.cn/post/7137536257814429704
相關推薦
- 2022-03-22 C++對象與繼承使用中一些問題介紹_C 語言
- 2022-04-07 c++中的volatile和variant關鍵字詳解_C 語言
- 2022-10-04 淺析C++模板類型中的原樣轉發和可變參數的實現_C 語言
- 2022-05-06 golang mongo-driver 模糊查詢
- 2022-08-04 淺析.net?core?拋異常對性能影響_實用技巧
- 2022-07-14 C++深入淺出講解希爾排序算法的實現_C 語言
- 2023-10-26 video標簽未自動播放,autoplay無效,video不能自動播放的原因分析
- 2022-07-21 CentOS 網絡設置修改
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支