Python about array Matrix transform function Numpy.nonzero (), numpy.multiply () usage

Source: Internet
Author: User

1.numpy.nonzero (condition), returns the Ndarray array formed by the index of the non-0 element of the parameter condition (array or matrix), and can also return a value index with a Boolean value of true in condition, where The value 0 is false and the rest is true.

1>>>b=np.mat (Np.arange (10)). T2>>>b3 Matrix ([[0],4[1],5[2],6[3],7[4],8[5],9[6],Ten[7], One[8], A[9]]) ->>>np.nonzero (b>2) -(Array ([3, 4, 5, 6, 7, 8, 9], dtype=Int64), theArray ([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))
1>>> x = Np.eye (3)2>>>x3Array ([[1., 0., 0.],4[0., 1., 0.],5[0., 0., 1.]])6>>>Np.nonzero (x)7(Array ([0, 1, 2]), array ([0, 1, 2]))8>>>9>>>X[np.nonzero (x)]TenArray ([1., 1., 1.]) One>>>Np.transpose (Np.nonzero (x)) A array ([[0, 0], -[1, 1], -[2, 2]]

where Np.nonzero ((b.a>2) * (b.a<8)) is the index that returns the value of array B in the range 2<b<8. and the parameter must be required when the array, if the matrix will error.

A common use for nonzero are to find the indices of an array, where a condition is True. Given an array a, the condition a > 3 are a Boolean array and since False is interpreted as 0, Np.nonz Ero (A > 3) yields the indices of the a where the condition is true. This function is the same as the use of Numpy.where ().

>>> a = Np.array ([[1,2,3],[4,5,6],[7,8,9]]) >>> a > 3array ([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 ()

Python about array Matrix transform function Numpy.nonzero (), numpy.multiply () usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.