Introduction to NumPy standard deviation
The average square deviation’s square root which is calculated from the mean is called standard deviation and the function in Python NumPy module which is used to calculate the standard deviation along a given axis is called numpy.std() function. The array elements standard deviation is returned using this numpy.std() function and the flattened array’s standard deviation is calculated by default using numpy.std() function and the formula used to calculate the average square deviation of a given array x is x.sum/N where N is the length of the array x and the standard deviation is calculated using the formula Standard Deviation=sqrt(mean(abs(x-x.mean( ))**2.
Syntax:
numpy.std(arrayname, axis=None, dtype=None, out=None, ddof=0, keepdims=<class numpy._globals._NoValue>)
Where,
- arrayname is the name of the array whose elements standard deviation is to be calculated.
- axis specifies the axis along which the standard deviation must be calculated and this value is optional.
- dtype specifies the data type used to compute the standard deviation and this value is optional.
- out specifies the name of the output array in which the result of the given array is going to be stored and this value is optional.
- ddof specifies the delta degrees of freedom and this value is optional.
Working of NumPy standard deviation
- The average square deviation’s square root which is calculated from the mean is called standard deviation.
- The function in Python NumPy module which is used to calculate the standard deviation along a given axis is called numpy.std() function.
- The array elements standard deviation is returned using this numpy.std() function.
- The flattened array’s standard deviation is calculated by default using numpy.std() function.
- The formula used to calculate the average square deviation of a given array x is x.sum/N where N is the length of the array x and the standard deviation is calculated using the formula Standard Deviation=sqrt(mean(abs(x-x.mean( ))**2.
Examples
Given below are the examples mentioned:
Example #1
Python program to demonstrate NumPy std function to create an array using NumPy array function and to calculate the standard deviation of the elements of the array using NumPy std() function.
Code:
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[1,2],[3,4]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
Output:
In the above program, in order to be able to use the array and std function, we are importing a package in python called NumPy function. Then we use array function to create an array and it is stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to std function to find the standard deviation value which is stored in a variable called stddev. Then the standard deviation value stored in stddev variable is displayed as the output on the screen.
Example #2
Python program to demonstrate NumPy std function to create an array using NumPy array function and to calculate the standard deviation of the elements of the array using NumPy std() function.
Code:
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[1.1,2.1,3.1],[4.1,5.1,6.1]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
Output:
In the above program, in order to be able to use the array and std function, we are importing a package in python called NumPy function. Then we use array function to create an array and it is stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to std function to find the standard deviation value which is stored in a variable called stddev. Then the standard deviation value stored in stddev variable is displayed as the output on the screen.
Example #3
Python program to demonstrate NumPy std function to create an array using array function and to calculate the standard deviation of the elements of the array using NumPy std() function.
Code:
#importing the package numpy
import numpy as no
#Creating an array by making use of array function in NumPy and storing it in a variable called arrayname
arrayname = no.array([[11,12],[14,15]])
#Displaying the elements of arrayname followed by one line space by making use of \n
print 'The elements of the given array are:'
print arrayname
print '\n'
#using std function of NumPy and passing the created array as the parameter to that function to find the standard deviation value of all the elements in the array and store it in a variable called stddev
stddev = no.std(arrayname)
#Displaying the standard deviation value stored in stddev variable
print 'The standard deviation of all the elements of the array is:'
print stddev
Output:
In the above program, in order to be able to use the array and std function, we are importing a package in python called NumPy function. Then we use array function to create an array and it is stored in the variable called arrayname. We then display the elements of the array arrayname on the screen. Then we pass the created arrayname as the parameter to std function to find the standard deviation value which is stored in a variable called stddev. Then the standard deviation value stored in stddev variable is displayed as the output on the screen.
Recommended Articles
This is a guide to NumPy standard deviation. Here we discuss the introduction, working of NumPy standard deviation and examples respectively. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses