Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法
1.numpy.nonzero(condition),返回参数condition(为数组或者矩阵)中非0元素的索引所形成的ndarray数组,同时也可以返回condition中布尔值为True的值索引,其中,数值0为False,其余的都为True。
>>>b=np.mat(np.arange(10)).T
>>>b
matrix([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
>>>np.nonzero(b>2)
(array([3, 4, 5, 6, 7, 8, 9], dtype=int64),
array([0, 0, 0, 0, 0, 0, 0], dtype=int64))
>>>np.nonzero((b.A>2)*(b.A<8))
(array([3, 4, 5, 6, 7], dtype=int64), array([0, 0, 0, 0, 0], dtype=int64))>>> x = np.eye(3)
>>> x
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> np.nonzero(x)
(array([0, 1, 2]), array([0, 1, 2]))
>>>
>>> x[np.nonzero(x)]
array([ 1., 1., 1.])
>>> np.transpose(np.nonzero(x))
array([[0, 0],
[1, 1],
[2, 2]]其中np.nonzero((b.A>2)*(b.A<8))是返回数组b的值在范围2<b<8的索引。并且必须要求参数时数组,如果是矩阵会报错。
A common use fornonzerois to find the indices of an array, where a condition is True. Given an arraya, the conditiona> 3 is a boolean array and since False is interpreted as 0, np.nonzero(a > 3) yields the indices of theawhere the condition is true.这个功能和numpy.where()的一种用法一样。
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a > 3
array([[False, False, False],
[ True, True, True],
[ True, True, True]], dtype=bool)
>>> np.nonzero(a > 3)
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))2numpy.multiply()
相关推荐
ainizhongguoaa 2019-05-05
qingsongzdq 2019-10-24
coolphxnuaa 2019-04-28
深入理解css3 2D变换,请参考这篇文章 理解CSS3 transform中的Matrix(矩阵)深入理解css3 3D变换,请参考这篇文章 好吧,CSS3 3D transform变换,不过如此!
xiaohuli 2019-06-28
impress 2014-07-21
xingxinmanong 2012-03-09
peakyi 2019-02-27
withlonger 2014-06-15