numpy中的nonzero()
来源:互联网 发布:加工中心编程学徒招聘 编辑:程序博客网 时间:2024/05/23 00:51
nonzero(a)
nonzero函数是numpy中用于得到数组array中非零元素的位置(数组索引)的函数。它的返回值是一个长度为a.ndim(数组a的轴数)的元组,元组的每个元素都是一个整数数组,其值为非零元素的下标在对应轴上的值。
(1)只有a中非零元素才会有索引值,那些零值元素没有索引值;
(2)返回的索引值数组是一个2维tuple数组,该tuple数组中包含一维的array数组。其中,一维array向量的个数与a的维数是一致的。
(3)索引值数组的每一个array均是从一个维度上来描述其索引值。比如,如果a是一个二维数组,则索引值数组有两个array,第一个array从行维度来描述索引值;第二个array从列维度来描述索引值。
(4)transpose(np.nonzero(x))函数能够描述出每一个非零元素在不同维度的索引值。
(5)通过a[nonzero(a)]得到所有a中的非零值
矩阵图:
a是一维数组:
>>> import numpy as np>>> a = [0,2,3]>>> b = np.nonzero(a)>>> b(array([1, 2], dtype=int64),)>>> np.array(b).ndim2>>>
说明:索引1和索引2的位置上元素的值非零
a是二维数组:
>>> a = np.array([[0,0,3],[0,0,0],[0,0,9]])>>> b = np.nonzero(a)>>> b(array([0, 2], dtype=int64), array([2, 2], dtype=int64))>>> np.array(b).ndim2>>> np.transpose(np.nonzero(a))array([[0, 2], [2, 2]], dtype=int64)>>>
说明:
(1)a中有2个非零元素,因此,索引值tuple中array的长度为2。因为,只有非零元素才有索引值。
(2)索引值数组是2 维的。实际上,无论a的维度是多少,索引值数组一定是2维的tuple,但是tuple中的一维array个数和a的维数一致。
(3)第1个array([0, 2])是从row值上对3和9进行的描述。第2个array([2, 2])是从col值上对3和9的描述。这样,从行和列上两个维度上各用一个数组来描述非零索引值。
(4)通过调用np.transpose()函数,得出3的索引值是[0 2],即第0行,第2列。
又一例子:
>>> b2 = np.array([[True, False, True], [True, False, False]]) >>> np.nonzero(b2)(array([0, 0, 1], dtype=int64), array([0, 2, 0], dtype=int64))>>>
说明:对于二维数组b2,nonzero(b2)所得到的是一个长度为2的元组。它的第0个元素是数组a中值不为0的元素的第0轴的下标,第1个元素则是第1轴的下标,因此从下面的结果可知b2[0,0]、b2[0,2]和b2[1,0]的值不为0
当布尔数组直接做为numpy数组下标时,相当于使用由nonzero()转换之后的元组作为下标对象:
>>> a = np.arange(3*4*5).reshape(3,4,5) >>> aarray([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]], [[20, 21, 22, 23, 24], [25, 26, 27, 28, 29], [30, 31, 32, 33, 34], [35, 36, 37, 38, 39]], [[40, 41, 42, 43, 44], [45, 46, 47, 48, 49], [50, 51, 52, 53, 54], [55, 56, 57, 58, 59]]])>>> a[b2]array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24]])>>> a[np.nonzero(b2)]array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24]])>>>
a是三维数组:
>>> a = np.array([[[0,0],[1,0]],[[0,0],[1,0]],[[0,0],[1,0]]])>>> b = np.nonzero(a)>>> b(array([0, 1, 2], dtype=int64), array([1, 1, 1], dtype=int64), array([0, 0, 0], dtype=int64))>>> np.array(b).ndim2>>>
说明:由于a是3维数组,因此,索引值数组有3个一维数组。
机器学习实战代码片段:
注意下面的代码是修改的代码:
# 二元切分def binSplitDataSet(dataSet, feature, value): # 三个参数:数据集合,待切分的特征,和该特征的某个值 mat0 = dataSet[nonzero(dataSet[:,feature] > value)[0],:] # 数组过滤 mat1 = dataSet[nonzero(dataSet[:,feature] <= value)[0],:] return mat0,mat1 # 返回两个子集
>>> testMat=mat(eye(4))>>> testMatmatrix([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])>>> nonzero(testMat[:,1] > 0.5)(array([1], dtype=int64), array([0], dtype=int64))>>> nonzero(testMat[:,1] > 0.5)[0]array([1], dtype=int64)>>> testMat[nonzero(testMat[:,1] > 0.5)[0],:]matrix([[ 0., 1., 0., 0.]])>>> testMat[nonzero(testMat[:,1] > 0.5)[0],:][0]matrix([[ 0., 1., 0., 0.]])>>> testMat[nonzero(testMat[:,1] <= 0.5)[0],:] # 根据特征分得样本集matrix([[ 1., 0., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])>>> testMat[nonzero(testMat[:,1] <= 0.5)[0],:][0] # 源代码有问题(最后的[0]应去掉)matrix([[ 1., 0., 0., 0.]])>>>
一个验证例子:
>>> testMat=mat(eye(4)) # 定义一个单位阵>>> testMatmatrix([[ 1., 0., 0., 0.], [ 0., 1., 0., 0.], [ 0., 0., 1., 0.], [ 0., 0., 0., 1.]])>>> nonzero(testMat >0.5) # 大于0.5的元素所在横纵轴的描述(array([0, 1, 2, 3], dtype=int64), array([0, 1, 2, 3], dtype=int64))>>> transpose(nonzero(testMat >0.5)) # 元素坐标array([[0, 0], [1, 1], [2, 2], [3, 3]], dtype=int64)>>> testMat[:,1] # 第一列matrix([[ 0.], [ 1.], [ 0.], [ 0.]])>>> a=transpose(nonzero(testMat[:,1] >0.5))# 第一列也就是上述列向量中大于0.5的坐标[1,0]>>> aarray([[1, 0]], dtype=int64)>>> a[0] # 由于a是[[1, 0]],所以a[0]是[1,0]array([1, 0], dtype=int64)>>> testMat[a[0],:] # 得到了testMat的第2行和第1行matrix([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.]])>>> testMat[a[0],:][0] # 得到了针对testMat第1列元素中大于0.5的元素所在整行matrix([[ 0., 1., 0., 0.]])>>>
- numpy中的nonzero()
- numpy中的nonzero()的用法
- numpy中的nonzero()的用法
- numpy.nonzero
- numpy nonzero
- numpy nonzero使用方法
- python numpy.nonzero
- numpy.nonzero函数
- numpy——nonzero
- numpy.nonzero()函数()
- numpy nonzero的用法
- Numpy.nonzero() 详解 numpy module中 nonzero()函数
- numpy中nonzero()的用法
- python中numpy.nonzero()用法
- Python的Numpy库中的 nonzero函数,及应用在 hardlim硬限幅函数中
- python numpy中nonzero()的用法
- python numpy中nonzero()的用法
- python numpy中nonzero()的用法
- 寻梦之路
- 吃透mp4格式系列——(七)
- Android程序签名打包
- 操作符重载
- 循环数组最大子段和
- numpy中的nonzero()
- git
- opencv2.4.9中stitching_detailed源码环境搭建
- 东南大学周张泉:基于知识图谱的推理技术 | 实录·Guru Talk
- 【追一科技校招】AI浪潮已来,你不可错过的澎湃风口!
- python核心编程读书笔记--第3章 基础语句语法
- OpenCV之轮廓检测
- Google Guava学习(6)-Guava集合工具 Multiset
- 图像和流媒体 -- 详解YUV数据格式