In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Numpy how to check the array is all zero of several methods, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Summary
This paper briefly introduces several test methods to determine whether the numpy array is all zero or not.
1 numpy.any ()
The numpy.any () function is used to check whether there is any non-zero element in a numpy number, so inverting the result of numpy.any () gives the result of "whether the numpy array is all zero". For example:
Import numpy as np print ('Using numpy.any ()...') ajar 1D = np.zeros (5) print ('Is axi1D all zeros?:', not (np.any (aqui1D) print ('Is aura 1D all zeros?:', ~ (np.any (aqui1D) ajar 1D [2] =-1print ('Is ajar 1D all zeros?:' Not (np.any (aura 1D) axiom 2D = np.zeros ((2Magazo 3)) print (axiom 2D) print ('Is aura 2D all zeros?:', not (aura 2D) axiom 2D [1JI 2] = 0.1print ('Is axiom 2D all zeros?:', not (aura 2D)
Output result:
Using numpy.any ().
Is a_1D all zeros?: True
Is a_1D all zeros?: True
Is a_1D all zeros?: False
[[0. 0. 0.]
[0. 0. 0.]]
Is a_2D all zeros?: True
Is a_2D all zeros?: False
Note that logical reversals in python can use either "~" or "not", but not "!" ("! =" is the comparison operator-- comparison operator, which can only be used, for example, "bounded inverse c"). In addition, there is also a difference between "~" and "not", see section 4 below.
2 numpy.count_nonzero ()
Numpy.count_nonzero () is used to count the number of zero elements in an array, so it can also be used to determine whether it is all zero or not. The usage is as follows:
Print ('Using numpy.nonzero ()...') a = np.array ('Number of zeros in a =', np.count_nonzero (a)) print ('Is an all zeros?:', np.count_nonzero (a) = 0) a [:] = 0 # Force a to all-zeros arrayprint ('Is an all zeros?:', np.count_nonzero (a) = = 0) print ('Is an all zeros?:', not np.count_nonzero (a))
Using numpy.nonzero ().
Number of zeros in a = 4
Is an all zeros?: False
Is an all zeros?: True
Is an all zeros?: True
3 numpy.all ()
You can also do this with numpy.all (). The following example takes advantage of the automatic conversion of 0--False and 1--True within python.
Print ('') print ('Using numpy.all ()...') a = np.zeros (10) print ('Is an all zeros?:', np.all (astat0))
Using numpy.all ().
Is an all zeros?: True
4. Multi-dimensional array can be judged by axis.
For multi-dimensional arrays (this is where numpy is really strong) the above functions make a unified judgment of the entire array by default, but they can also be processed separately along the specified axis through the axis parameter. As shown in the following example:
Print ('') print ('Judge according to the specified axis') ajar 2D = np.zeros (2p3) ajar 2D [1Power2] = 0.1print (aura 2D) print (' Is each col of aura 2D all zeros?:', ~ (np.any (aura 2D, axis=0)) print ('Is each row of aura 2D all zeros?:', ~ (np.any (ajar 2D, axis=1)
Judge according to the specified axis
[[0. 0. 0. ]
[0. 0. 0.1]]
Is each col of a_2D all zeros?: [True True False]
Is each row of a_2D all zeros?: [True False]
When axis=0 is specified, it is equivalent to judging whether the 2-dimensional array is all zero by column, and when specifying axis=1, it is equivalent to judging whether the 2-dimensional array is all zero by row. Of course, the concept of rows and columns here is inherited from the traditional two-dimensional array or matrix, and when considering higher-dimensional arrays, the concept of rows and columns is no longer applicable. The axis of high-dimensional arrays (also known as Tensor, tensor) will be introduced in another article.
In addition, the previously mentioned "~" and "not", which denote logical inversion, are different. Specifically, not accepts only one Operand, so changing "~" to not in the above example will result in an error, as shown below:
Print ('Is each col of averse 2D all zeros?:', not (np.any (axium 2D, axis=0)) print ('Is each row of averse 2D all zeros?:', not (aura 2D, axis=1)
The error is as follows:
And "~" is the so-called Bitwise NOT operator.
If the input of "~" is an integer, it will reverse all the bits of the input number. If it is a numpy array, a bitwise logic inversion operation is performed on each of the numbers. If it is a numpy Boolean (True, False) array, a logical inversion operation is performed on each of these Boolean numbers.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.