Standard Deviation tells you how the data set is spread. It helps you to normalize data for scaling. There is a method in NumPy that allows you to find the standard deviation. And it is numpy.std(). In this article, We will discuss it and find the NumPy standard deviation. But before that first of all learn the syntax of numpy.std().
Syntax for the Numpy Standard Deviation Method
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>)
a: The array you want to find the standard deviation.
axis: Useful to calculate standard deviation row-wise or column-wise. The default is None.
dtype: Type of the object. The default values in None.
out: It allows you to output the result to another array.
ddof: Means Delta Degrees of Freedom.
keepdims: If this is set to True, the axes which are reduced are left in the result as dimensions with size one.
Examples for Calculation of NumPy standard deviation
In this section, you will know the best example for the NumPy standard deviation Calculation. But before that first of all import all the necessary libraries for that. Here In our example, I will use only two python modules. One is numpy and the other is pandas for dataframe.
import numpy as np import pandas as pd
How to compute the standard deviation for 1-D Array
Let’s create a single dimension NumPy array for standard deviation calculation.
array_1d = np.array([10,20,30,40])
After that, you will pass this array as an argument inside the numpy.std().
Get standard deviation of Two Dimension or matrix
In this section, We will learn how to calculate the standard deviation of 2 Dimension or Matrix. Let’s create a 3×4 Matrix.
array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]]) array_3x4
If you will simply pass the matrix inside the numpy.std(), then you will get the single output.
It calculates the standard deviation using all the elements of the matrix.
Standard deviation of each column of a matrix
You have to use axis =1 to calculate the standard deviation for each column of the matrix.
Standard deviation of each row of a matrix
To calculate the standard deviation for each row of the matrix. You have to set axis =0.
Below is the output of the above code.
Calculate Standard Deviation in dataframe
In this section, you will know how to calculate the Standard Deviation in Dataframe. But before that let’s make a Dataframe from the NumPy array.
After that convert NumPy array to dataframe.
df = pd.DataFrame(numpy_array)
You can now use the same above method to calculate deviation. For example for each column use axis=0, and for each row use axis =1.
np.std(df,axis=0) #calculate standrad deviation for each column np.std(df,axis=1) #calculate standrad deviation for each row
Get Standard Deviation of each Column of CSV File
You can also calculate the standard deviation of each column of CSV File using Numpy and pandas. Here Pandas will be used for reading the CSV file.
In this example, I am using a car dataset.
csv_data = pd.read_csv("cars.csv") csv_data
You can find the deviation of any numerical column using the column name. For example, I want to use the column name “mpg” then I will use the below code.
mpg = csv_data["mpg"]
Now I can easily calculate the standard deviation of it using the numpy.std() method.
Below is the output of the example described here.
This way you can find deviation for each numerical column for your CSV dataset.
These are examples for calculating the standard deviation I have written for you. Just follow all the examples for deep understanding. Even if you have doubts then you can contact us. We are always ready to help you.
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.