Numpy.nonzero()返回的是数组中,非零元素的位置。如果是二维数组就是描述非零元素在几行几列,三维数组则是描述非零元素在第几组中的第几行第几列。

举例如下:

二维数组:

a = np.array([[, , ], [, , ], [, , ]])
b = np.nonzero(a)
print(b)

结果为:(array([0, 0, 1, 2], dtype=int64), array([0, 2, 1, 2], dtype=int64))

第一个array描述行,第二个array描述列,我们把结果转变下以便理解:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

我们看到第一个非零元素1,在0行0列,对应为加粗数字:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

第二个非零元是3,在0行2列,对应:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

第三个非零元素是2,在1行1列,对应:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

第四个非零元素是9,在2行2列,对应:

array[0, 0, 1, 2]

array[0, 2, 1, 2]

再举一个三维数组的列子:

a = np.array([[[,],[,]],[[,],[,]],[[,],[,]]])
b = np.nonzero(a)
print(b)

结果为:(array([0, 0, 1, 1, 2], dtype=int64), array([0, 1, 0, 1, 1], dtype=int64), array([1, 0, 1, 0, 0], dtype=int64))

同样变形下:

array[0, 0, 1, 1, 2]     描述在第几组

array[0, 1, 0, 1, 1]     描述行

array[1, 0, 1, 0, 0]     描述列

第一个非零元是1,在0组0行1列,对应

array[0, 0, 1, 1, 2]

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

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

第二个非零元是2,在0组1行0列,对应

array[0, 0, 1, 1, 2]

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

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

第三个非零元是3,在1组0行1列,对应

array[0, 0, 1, 1, 2]

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

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

第四个非零元是4,在1组1行0列,对应

array[0, 0, 1, 1, 2]

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

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

第五个非零元素是5,在2组1行0列,对应

array[0, 0, 1, 1, 2]

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

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

更高维数计算类似,读者可以自己推导

Python中Numpy.nonzero()函数的更多相关文章

  1. Python中numpy.apply_along_axis()函数的用法

    numpy.apply_along_axis(func, axis, arr, *args, **kwargs): 必选参数:func,axis,arr.其中func是我们自定义的一个函数,函数fun ...

  2. python 中numpy dot函数的使用方法

    这个函数在的数字信号处理中用处还是比较广泛的,函数的具体定义如下所示: numpy.dot(a, b, out=None) 该函数的作用是获取两个元素a,b的乘积,表示的含义如下所示: dot(a, ...

  3. python中numpy.sum()函数

    讲解清晰,转载自:https://blog.csdn.net/rifengxxc/article/details/75008427 众所周知,sum不传参的时候,是所有元素的总和.这里就不说了. 1 ...

  4. python中numpy对函数进行矢量化转换

    在对numpy的数组进行操作时,我们应该尽量避免循环操作,尽可能利用矢量化函数来避免循环. 但是,直接将自定义函数应用在numpy数组之上会报错,我们需要将函数进行矢量化转换. def Theta(x ...

  5. python中numpy.concatenate()函数的使用

    numpy库数组拼接np.concatenate 原文:https://blog.csdn.net/zyl1042635242/article/details/43162031 思路:numpy提供了 ...

  6. Python中Numpy及Matplotlib使用

    Python中Numpy及Matplotlib使用 1. Jupyter Notebooks 作为小白,我现在使用的python编辑器是Jupyter Notebook,非常的好用,推荐!!! 你可以 ...

  7. python --- Python中的callable 函数

    python --- Python中的callable 函数 转自: http://archive.cnblogs.com/a/1798319/ Python中的callable 函数 callabl ...

  8. python中使用zip函数出现<zip object at 0x02A9E418>

    在Python中使用zip函数,出现<zip object at 0x02A9E418>错误的原因是,你是用的是python2点多的版本,python3.0对python做了改动 zip方 ...

  9. [转载]python中multiprocessing.pool函数介绍

    原文地址:http://blog.sina.com.cn/s/blog_5fa432b40101kwpi.html 作者:龙峰 摘自:http://hi.baidu.com/xjtukanif/blo ...

随机推荐

  1. b 解题报告

    本题已收录至2019/9/15 本周总结 题目 [问题描述] Hja有一棵\(n\)个点的树,树上每个点有点权,每条边有颜色.一条路径的权值是这条路径上所有点的点权和,一条合法的路径需要满足该路径上任 ...

  2. Stream中的map

    #map可以让一个对象A的流转换为宁外一种对象B的流(其实也是A对象元素组成的流) 1.对象转换为List集合 //若Eticket是一个对象,其中orderId是String类型 //eticket ...

  3. PHP SDK+Oss 上传文件流

    // Endpoint以杭州为例,其它Region请按实际情况填写. $endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // 云 ...

  4. LED Decorative Light Supplier Introduction - LED Track Light Products

    LED Decorative Light Supplier    introduction: LED track light is a track light with LED as the ligh ...

  5. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...

  6. MySQL 的两种存储引擎

    MyISAM 是MySQL的默认数据库引擎(5.5以后默认是InnoDB)性能极佳,但不支持事务处理. InnoDB 是MySQL的数据库常用的数据引擎. MyISAM 和 InnoDB 两者之间有明 ...

  7. Linux07——安装MySQL

    ①检查工作 CentOS6 rpm -qa|grep mysql  或者 rpm -qa | grep mysql 如果存在mysql-libs的旧版本包如下: 一定要执行卸载呀!!! 卸载命令:rp ...

  8. IntelliJ IDEA 2017.3尚硅谷-----主题

    http://www.riaway.com/

  9. code ELIFECYCLE 报错处理

    npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! m-kbs-vip@1.2.12 toserver: `tua -p toserver`npm ERR ...

  10. PHP 源码 —— is_array 函数源码分析

    is_array 函数源码分析 本文首发于 https://github.com/suhanyujie/learn-computer/blob/master/src/function/array/is ...