numpy - 获取array中数组元素的索引位置

numpy - 获取array中数组元素的索引

1. 函数原型

argwhere(array):找到非空数组array在满足某些条件下的索引,返回索引数组。


2. 应用

2.1 一维数组

返回一个一维数组,代表当前满足条件的元素出现的位置。

[python] view plain copy
print ?
  1. # -*- coding: utf-8 -*-  
  2. import numpy as np  
  3.   
  4. arr = np.random.randint(0,10, (5,))  
  5. index = np.argwhere(arr < 5)  
# -*- coding: utf-8 -*-
import numpy as np

arr = np.random.randint(0,10, (5,))
index = np.argwhere(arr < 5)


2. 2 二维数组

返回二维数组,代表当前满足条件的元素出现的位置。

[python] view plain copy
print ?
  1. # -*- coding: utf-8 -*-  
  2. import numpy as np  
  3.   
  4. ”“” 
  5. arr =  
  6.     9 3 7 0  
  7.     3 4 2 4  
  8.     3 6 4 4  
  9.      
  10. index =  
  11.     0   1 
  12.     0   3 
  13.     1   0 
  14.     1   1 
  15.     1   2 
  16.     1   3 
  17.     2   0 
  18.     2   2 
  19.     2   3 
  20. ”“”  
  21.   
  22. arr = np.random.randint(0,10, (3,4))  
  23. index = np.argwhere(arr < 5)  
# -*- coding: utf-8 -*-
import numpy as np

"""
arr = 
    9 3 7 0 
    3 4 2 4 
    3 6 4 4 

index = 
    0   1
    0   3
    1   0
    1   1
    1   2
    1   3
    2   0
    2   2
    2   3
"""

arr = np.random.randint(0,10, (3,4))
index = np.argwhere(arr < 5)


参考文献

http://blog.csdn.net/vernice/article/details/50990919

            
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZK_J1994/article/details/76707734
文章标签: Python numpy
个人分类: Python
所属专栏: Python

你可能感兴趣的:(深度学习)