[Python] numpy.nonzero

本文详细介绍了numpy库中nonzero()函数的使用方法及其返回值的意义。通过具体实例展示了如何获取数组中非零元素的索引,并解释了这些索引是如何按维度组织的。此外还提供了如何利用transpose函数配合nonzero()来直接获取非零元素位置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy.nonzero(a)

Return the indices of the elements that are non-zero.

Returns a tuple of arrays, one for each dimension of a, containing the indices of the non-zero elements in that dimension. The values in aare always tested and returned in row-major, C-style order. The corresponding non-zero  values can be obtained with:

 

 
import numpy as np

a = np.eye(3);

print np.nonzero(a)
#(array([0, 1, 2]), array([0, 1, 2]))

  

print np.transpose(np.nonzero(a))
#[[0 0]
# [1 1]
# [2 2]]

  

转载于:https://www.cnblogs.com/KennyRom/p/6621770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值