使用

np.where的速度相当快:

>>> a

array([[0, 0, 0, 1, 1, 1, 1],

[0, 0, 0, 1, 1, 1, 1],

[0, 0, 0, 0, 1, 1, 1],

[0, 0, 0, 0, 0, 0, 1],

[0, 0, 0, 0, 0, 0, 1],

[0, 0, 0, 0, 0, 0, 1],

[0, 0, 0, 0, 0, 0, 0]])

>>> np.where(a>0)

(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5]), array([3, 4, 5, 6, 3, 4, 5, 6, 4, 5, 6, 6, 6, 6]))

这会将元组传递给大于0的坐标值.

您还可以使用np.where来测试每个子数组:

def first_true1(a):

""" return a dict of row: index with value in row > 0 """

di={}

for i in range(len(a)):

idx=np.where(a[i]>0)

try:

di[i]=idx[0][0]

except IndexError:

di[i]=None

return di

打印:

{0: 3, 1: 3, 2: 4, 3: 6, 4: 6, 5: 6, 6: None}

即,第0行:索引3> 0;第4行:索引4> 0;第6行:没有大于0的索引

如您所料,argmax可能更快:

def first_true2():

di={}

for i in range(len(a)):

idx=np.argmax(a[i])

if idx>0:

di[i]=idx

else:

di[i]=None

return di

# same dict is returned...

如果你可以处理所有naughts行中没有None的逻辑,那么这仍然更快:

def first_true3():

di={}

for i, j in zip(*np.where(a>0)):

if i in di:

continue

else:

di[i]=j

return di

这是一个在argmax中使用轴的版本(如评论中所示):

def first_true4():

di={}

for i, ele in enumerate(np.argmax(a,axis=1)):

if ele==0 and a[i][0]==0:

di[i]=None

else:

di[i]=ele

return di

对于速度比较(在您的示例数组上),我得到:

rate/sec usec/pass first_true1 first_true2 first_true3 first_true4

first_true1 23,818 41.986 -- -34.5% -63.1% -70.0%

first_true2 36,377 27.490 52.7% -- -43.6% -54.1%

first_true3 64,528 15.497 170.9% 77.4% -- -18.6%

first_true4 79,287 12.612 232.9% 118.0% 22.9% --

如果我将它扩展到2000 X 2000 np数组,这是我得到的:

rate/sec usec/pass first_true3 first_true1 first_true2 first_true4

first_true3 3 354380.107 -- -0.3% -74.7% -87.8%

first_true1 3 353327.084 0.3% -- -74.6% -87.7%

first_true2 11 89754.200 294.8% 293.7% -- -51.7%

first_true4 23 43306.494 718.3% 715.9% 107.3% --

python 第一行非零_python – 沿着已排序的二维numpy数组的轴查找第一个非零值相关推荐

  1. Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组、split函数垂直拆分二维numpy数组

    Python使用numpy函数vsplit垂直(行角度)拆分numpy数组(返回拆分后的numpy数组列表)实战:垂直拆分二维numpy数组.split函数垂直拆分二维numpy数组 目录

  2. numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row) 目录

  3. numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array)

    numpy使用[]语法索引二维numpy数组中指定数据行的数值内容(accessing the specific row in numpy array) 目录 numpy使用[]语法索引二维numpy ...

  4. numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range)

    numpy使用[]语法索引二维numpy数组中指定范围数据行的数值内容(accessing rows in numpy array with specific range) 目录

  5. numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row)

    numpy使用[]语法索引二维numpy数组中指定指定行之前所有数据行的数值内容(accessing rows in numpy array before specifc row) 目录

  6. python使用np.argsort对一维numpy概率值数据排序获取倒序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最大的头部数据

    python使用np.argsort对一维numpy概率值数据排序获取倒序索引.获取的top索引(例如top2.top5.top10)索引二维numpy数组中对应的原始数据:原始数据概率最大的头部数据 ...

  7. python使用np.argsort对一维numpy概率值数据排序获取升序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据

    python使用np.argsort对一维numpy概率值数据排序获取升序索引.获取的top索引(例如top2.top5.top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据 ...

  8. Python使用numpy函数hsplit水平(按列)拆分numpy数组(返回拆分后的numpy数组列表)实战:水平(按列)拆分二维numpy数组、split函数水平(按列)拆分二维numpy数组

    Python使用numpy函数hsplit水平(按列)拆分numpy数组(返回拆分后的numpy数组列表)实战:水平(按列)拆分二维numpy数组.split函数水平(按列)拆分二维numpy数组 目 ...

  9. python 三维矩阵乘以二维矩阵_二维numpy数组的乘法/除法生成三维数组

    我正在寻找一种快速(即矢量化)的方法来替换下面的循环.我有两个numpy数组,尺寸分别为:(20738,14)和(31,14).我需要将它们元素相乘得到一个数组(20738,31,14).我一直在尝试 ...

最新文章

  1. Niblack二值化方法的实现
  2. Oracle 11g R2安装过程中遇到的报错及解决办法
  3. spark安装测试过程中提示console:10: error: not found: value sc
  4. 遍历文件夹还原数据库SQL语句
  5. vue+elementUI 添加多个可以全选的多选框
  6. centos Ipython安装
  7. 2018.09.14python学习第四天part2
  8. 控制寄存器:指令指针寄存器IP:存放下一条指令的地址的偏移量,标志寄存器FR(16):
  9. 矩阵论及其应用_数值分析篇——向量和矩阵的范数
  10. 没有什么不可能!郴州女孩江梦南,从双耳失聪到清华博士!
  11. SQL Server 批量插入数据的两种方法 - 转
  12. oracle归档文件管理
  13. mongodb的基本使用_万金油_新浪博客
  14. 嵌入式(单片机方向)工程师如何做好定位和职业规划
  15. MySQL数据库执行Update卡死问题解决
  16. 2015年秋季腾讯校园招聘开发岗笔试题(四道大题)
  17. NYOJ-47 过河问题
  18. 阿里云OpenAPI调用时遇到Specified access key is not found怎么办?
  19. 游戏陪玩App,如雨后春笋般冒出,直接导致整个游戏陪玩源码市场的持续火爆
  20. Ethereum-ERC721智能合约和Dapp实践--以太猫CryptoKitties的简单实现

热门文章

  1. pandas使用shift方法进行特征差分
  2. R语言在可视化图像中添加文本(Adding Text to plot)
  3. 通过mrtrix3进行概率纤维追踪+核磁共振影像数据处理
  4. java连接Hbase数据库
  5. jupyter扩展插件Nbextensions的安装、使用
  6. Logstic与Softmax比较
  7. 影像组学视频学习笔记(7)-特征筛选之LASSO回归(代码)、Li‘s have a solution and plan.
  8. 大学计算机基础 学年教学计划,大学计算机基础教学计划.pdf
  9. PacBio SMRT Sequencing
  10. 如何把自己的经历写成小说_《诡秘小说》:读者与作者共同创作,难分真假,午夜不敢独自看!...