Standard Deviation of NumPy Array in Python (4 Examples)
In this tutorial you’ll learn how to get the standard deviation of a NumPy array using the np.std function in the Python programming language.
The tutorial contains these contents:
It’s time to dive into the examples.
Example Data & Software Libraries
To be able to use the functions of the NumPy library, we first have to import NumPy:
import numpy as np # Import NumPy library in Python
Furthermore, consider the following example array:
my_array = np.array([[1, 2, 7, 2, 3], # Create example array [7, 1, 1, 5, 6], [5, 2, 5, 5, 8]]) print(my_array) # Print example array # [[1 2 7 2 3] # [7 1 1 5 6] # [5 2 5 5 8]]
Have a look at the previous output of the Python console. It shows that our example array contains 15 values in five columns and three rows.
Example 1: Standard Deviation of All Values in NumPy Array (Population Variance)
In this example, I’ll show how to calculate the standard deviation of all values in a NumPy array in Python.
For this task, we can apply the std function of the NumPy package as shown below:
print(np.std(my_array)) # Get standard deviation of all array values # 2.3380903889000244
As you can see, the result is 2.338.
Note that this result is based on the population variance.
In case you are looking for the standard deviation based on the sample variance, keep on reading!
Example 2: Standard Deviation of All Values in NumPy Array (Sample Variance)
In Example 2, I’ll illustrate how to compute the standard deviation based on the sample variance formula of a NumPy array in Python.
To achieve this, we have to specify the ddof argument to be equal to 1 as shown in the following example code:
print(np.std(my_array, ddof = 1)) # Get standard deviation of all array values # 2.4201534780139164
The previous result is slightly higher as in Example 1, since we have calculated the sample standard deviation instead of the population standard deviation.
Example 3: Standard Deviation of Columns in NumPy Array
The following Python code shows how to find the standard deviation of the columns of a NumPy array.
To do this, we have to set the axis argument equal to 0:
print(np.std(my_array, axis = 0)) # Get standard deviation of array columns # [2.49443826 0.47140452 2.49443826 1.41421356 2.05480467]
Example 4: Standard Deviation of Rows in NumPy Array
Similar to Example 3, we can calculate the standard deviation of a NumPy array by row.
This time, we have to set the axis argument to be equal to 1:
print(np.std(my_array, axis = 1)) # Get standard deviation of array rows # [2.0976177 2.52982213 1.8973666 ]
Video & Further Resources
I have recently released a video tutorial on my YouTube channel, which explains the contents of this page. You can find the video tutorial below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to read the related posts on my homepage.
- Standard Deviation in Python
- Variance of NumPy Array in Python
- Get Median of Array with np.median Function of NumPy Library in Python
- Standard Deviation in Python in R
- Convert pandas DataFrame to NumPy Array in Python
- Standard Deviation by Group in Python in R
- All Python Programming Examples
You have learned in this tutorial how to find the standard deviation of a NumPy array using the np.std function in the Python programming language. Please tell me about it in the comments section, if you have additional comments and/or questions.