In this article, we will discuss how to count all values in a 2D numpy Array or Matrix in python, which satisfy a condition like greater than a given value etc.
Table of Contents
- Count all values greater than a value in 2D Numpy Array.
- Count all values greater than a value in each row of 2D Numpy Array.
- Count all values greater than a value in each column of 2D Numpy Array.
A background of problem in hand
Suppose we have a 2D Numpy array,
# Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]])
We want to count all the values in this numpy array which are greater than 3. To do that we can apply a condition on array like,
arr > 3
It will convert the 2D number array to a 2D bool array with True at places where condition returns True i.e. value is greater than 3. Bool array will be like,
[[False False False True] [ True True True True] [False False False True]]
Now if we count the number of True elements in this bool array, then it will give us the count of values which are greater than 3. To count True
elements we can use a function count_nonzero().
numpy.count_nonzero()
In Python, the numpy module provides a function count_nonzero(arr, axis=None), which returns the count of non zero values in a given numpy array. When the value of axis argument is None, then it returns the count of non zero values in complete array. But in case you are dealing with
multi-dimensional array, then you can use the axis argument to count occurrences along the given axis. Let’s use this function to count values in a numpy array that satisfy a condition.
Count all values greater than a value in 2D Numpy Array in python
Count all the values greater than 3 in complete 2D Numpy array,
import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in 2D Numpy array count = np.count_nonzero(arr > 3) print('Total values greater than 3 are: ', count)
Output:
Total values greater than 3 are: 6
As we didn’t provided the axis parameter, so count_nonzero() returned all the values which are greater than 3 in complete 2D numpy array.
Count all the values greater than a value in each row of 2D Numpy array in python
We have a 2D Numpy array and we will count all the values greater than 3 in each row of the 2D Numpy array,
import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in each row of 2D Numpy Array count = np.count_nonzero(arr > 3, axis=1) print('Count of all values greater than 3 in each row: ', count)
Output:
Count of all values greater than 3 in each row: [1 4 1]
As we provided the axis parameter as 1, so count_nonzero() returned an array containing all the values which are greater than 3 in each row of the 2D numpy array.
Count all the values greater than a value in each column of 2D Numpy array in python
We have a 2D Numpy array and we will count all the values greater than 3 in each column of the 2D Numpy array,
import numpy as np # Create 2D Numpy array of hard coded numbers & shape 3X4 arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [1, 3, 2, 9]]) # Count of all values greater than 3 in each column of 2D Numpy Array count = np.count_nonzero(arr > 3, axis=0) print('Count of all values greater than 3 in each column: ', count)
Output:
Count of all values greater than 3 in each column: [1 1 1 3]
As we provided the axis parameter as 0, so count_nonzero() returned an array containing all the values which are greater than 3 in each column of the 2D numpy array.
Summary:
In this article we learned how to count values greater than a vaue in 2D Numpy Array.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.