Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use numpy's nonzero to find out non-zero elements

Shulou Source: shulou.com Published: 2022-06-01 12:04:47 10月04日 Update

This article mainly introduces the relevant knowledge of "how to use numpy's nonzero to find out non-zero elements". The editor shows you the operation process through actual cases. The method of operation is simple and fast, and it is practical. I hope this article "how to use numpy's nonzero to find out non-zero elements" can help you solve the problem.

Let's go straight to the code ~ import numpy as npa = np.array ([[30Ling 40jing70], [80Ling 20jing10], [50jin90]]) print (a) print (np.nonzero (a)) [[304070] [802010] [509060]] (array ([0,0,0lle 1,1,2,2,2], dtype=int64), array ([0,20,10,10,10,1,2], dtype=int64)

The first array is the x-axis and the second array is the y-axis

Add: [Numpy Learning] python search matrix index (np.nonzero ()) that is not a zero element

When using the matrix decomposition method to make the model, it is necessary to verify the results of the model.

The mask training set, verification set, and test set are required during the verification process.

At this point, you need the index values of the non-zero element and the zero element in the original matrix S SS. This method is the find method in matlab, and np.nonzero () is required when implemented in python.

Look at the following code: import numpy as npa = np.arange (12) .reshape (3,4) print (a) [[0123] [4 567] [8 9 10 11]] print (np.nonzero (a)) (array ([0,0,0,1,1,1,1,2,2,2]), array ([1,2,3,0,1,2,3,0,1,2,3]))

It is worth noting that np.nonzero (a) outputs two array values in the first array that refer to rows and the second to columns.

For example, 0minute 1 indicates that the value of the first column in row 0 is not 0.

This is the end of the introduction to "how to use numpy's nonzero to find non-zero elements". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Elements methods knowledge matrices validation code models indexes industries processes different practical two content practicality practical articles more cases knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Xiaomi vpn Huawei NVidia