How nonzero () in Python numpy should be used

Source: Internet
Author: User
This time to bring you the Python numpy nonzero () should be used, Python numpy in nonzero () use of the notes are what, the following is the actual case, together to see.

The nonzero function returns a directory that is not a 0 element.

The return value is a tuple, with two values of two dimensions, containing the catalog values for the non-0 elements on the corresponding dimension.

  Import NumPy as np  A = Np.mat ([[[0,1,2,3,4,3,2,1,0],[0,1,2,3,4,5,6,7,0]])  x = A.nonzero () #取出矩阵中的非零元素的坐标  Print x  #输出是一个元组, two dimensions.  One by one corresponds,  #返回非零元素在矩阵中的位置, the previous list holds non-0 line coordinates, and the latter list holds non-0-element column coordinates # (array ([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1  ]), Array ([1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7]))  #放下来一一对应, i.e.  # (array ([0,  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1  ] ),   #array ([1,  2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5,  6,  7])    # (0,1) (0,2) ...) # (a) # (...)... . (1,6) (1,7) The elements on the coordinates are non-0   print a[x], ' \ n '  #取出矩阵中的非零元素  #[[1 2 3 4 3 2 1 1 2 3 4 5 6 7]]   B = Np.mat ([[1, 1, 0,1,0,1,0,0,1],[0,1,1,0,0,0,1,1,1]])  print Np.nonzero (B) # and B.nonzero () equivalent

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Python how to read and write TXT file line-wise

Python How to implement HMACMD5 encryption algorithm

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.