網站首頁 編程語言 正文
在python中利用numpy array進行數據處理,經常需要找出符合某些要求的數據位置,有時候還需要對這些位置重新賦值。這里總結了幾種找出符合條件數據位置的方法。
這里以一個8*8的隨機數組舉例,來找出大于零的數。
import numpy as np
a = random.randint(-10,10,size=(8,8))
>>>
array([[ 5, 5, -7, 7, -8, -7, 0, -8],
[ -4, 9, 8, -3, 6, -4, -7, -5],
[ 7, 0, 6, 6, -4, -2, -8, 2],
[ 6, -5, 8, 4, 7, -8, -4, -4],
[ 0, 1, -1, -8, -1, 9, 4, 1],
[ 4, -8, -1, -8, -2, -6, -1, 9],
[ 7, 7, 9, -9, 4, 8, 3, 1],
[ -8, 4, -2, 4, -1, -4, -10, 0]])
1.直接利用條件索引
location= a[a>0]
print(location)
>>> array([5, 5, 7, 9, 8, 6, 7, 6, 6, 2, 6, 8, 4, 7, 1, 9, 4, 1, 4, 9, 7, 7, 9,
? ? ? ?4, 8, 3, 1, 4, 4])
# 直接輸出了大于0的數字
#--------------------------------------------------------------#
# 我們可以用下面的方法將小于0的數字都設置為零,留下大于零的數字
b = a.copy()
b[b<=0]=0
print(b)
>>>
[[5 5 0 7 0 0 0 0]
?[0 9 8 0 6 0 0 0]
?[7 0 6 6 0 0 0 2]
?[6 0 8 4 7 0 0 0]
?[0 1 0 0 0 9 4 1]
?[4 0 0 0 0 0 0 9]
?[7 7 9 0 4 8 3 1]
?[0 4 0 4 0 0 0 0]]
?# 這就將所有大于零的保留了下來
#--------------------------------------------------------------#
#還可以此類推,將大于零的位置都設置成1,可得到大于一的位置
b = a.copy()
b[b>0] = 1
b[b<=0] = 0
print(b)
>>>
[[1 1 0 1 0 0 0 0]
?[0 1 1 0 1 0 0 0]
?[1 0 1 1 0 0 0 1]
?[1 0 1 1 1 0 0 0]
?[0 1 0 0 0 1 1 1]
?[1 0 0 0 0 0 0 1]
?[1 1 1 0 1 1 1 1]
?[0 1 0 1 0 0 0 0]]
2.利用numpy.where()
# results = np.where(condition, [x, y])
# 當條件為真時,對應位置返回x中的值,條件不成立則返回y中的值
c = np.where(a>0,a,0) ?#滿足大于0的值保留,不滿足的設為0
print(c)
>>>
[[5 5 0 7 0 0 0 0]
?[0 9 8 0 6 0 0 0]
?[7 0 6 6 0 0 0 2]
?[6 0 8 4 7 0 0 0]
?[0 1 0 0 0 9 4 1]
?[4 0 0 0 0 0 0 9]
?[7 7 9 0 4 8 3 1]
?[0 4 0 4 0 0 0 0]]
# 大于零為1小于零為0
c = np.where(a>0,1,0) ?#滿足大于0的值保留,不滿足的設為0
print(c)
[[1 1 0 1 0 0 0 0]
?[0 1 1 0 1 0 0 0]
?[1 0 1 1 0 0 0 1]
?[1 0 1 1 1 0 0 0]
?[0 1 0 0 0 1 1 1]
?[1 0 0 0 0 0 0 1]
?[1 1 1 0 1 1 1 1]
?[0 1 0 1 0 0 0 0]]
3.直接邏輯運算
a > 0 ? # 得到判斷矩陣
array([[ True, ?True, False, ?True, False, False, False, False],
? ? ? ?[False, ?True, ?True, False, ?True, False, False, False],
? ? ? ?[ True, False, ?True, ?True, False, False, False, ?True],
? ? ? ?[ True, False, ?True, ?True, ?True, False, False, False],
? ? ? ?[False, ?True, False, False, False, ?True, ?True, ?True],
? ? ? ?[ True, False, False, False, False, False, False, ?True],
? ? ? ?[ True, ?True, ?True, False, ?True, ?True, ?True, ?True],
? ? ? ?[False, ?True, False, ?True, False, False, False, False]], dtype=bool)
原文鏈接:https://blog.csdn.net/u014636245/article/details/102574938
相關推薦
- 2022-09-19 Android實現斷點續傳功能_Android
- 2022-07-24 CSP?communicating?sequential?processes并發模型_Golang
- 2023-07-03 python?遍歷可迭代對象的實現方法_python
- 2022-10-27 Apache?Hive?通用調優featch抓取機制?mr本地模式_Linux
- 2022-03-23 android安裝后啟動出錯解決_Android
- 2024-04-03 @ExceptionHandler沒有報錯詳細信息
- 2023-07-26 webpack配置preload和prefetch預加載技術
- 2022-04-27 詳解如何在Flutter中用小部件創建響應式布局_Android
- 最近更新
-
- 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同步修改后的遠程分支