Home »
Python »
Python Programs
How to calculate skewness and kurtosis in Python?
By Shivang Yadav Last updated : November 22, 2023
Skewness
It is the measure of how asymmetrical a distribution is. Based on the direction (+ or -) of the skewness values the distribution's bias is represented.
- Negative value: there are larger values on the left side of the distribution.
- Positive value: there are larger values on the right side of the distribution.
- Zero: Distribution is symmetrical.
Calculating Skewness
To calculate skewness in Python, use the skew() method of scipy.stats library. This method accepts the data set and computes the sample skewness of a given data set.
Syntax
Below is the syntax of skew() method -
scipy.stats.skew(a, axis = 0, bias = True,
nan_policy= 'propogatre' , keepdims = False)
Parameters
Here is the list of the parameters of skew() method -
- a: input array for calculation
- axis: (int or None, default = 0), the axis along with computation is to be made.
- bias: (boolean value, optional), states whether to make calculations statistical bias or not.
- nan_policy: defines how to handle Nn inputs.
- propagate: return NaN as the output of NaN input values.
- omit: omits all NaN values for calculation, and computes rest. If no values remain then return NaN.
- raise: if the NaN value is present, an error is raised.
- keepdims: (boolean, default: False) If this is set to True, the axes that are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Example: Python program to calculate skewness
# Import the library to use skew() method
from scipy.stats import skew
data = [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
print("The data in the dataset is", data)
skewVal = skew(data)
print("Skewness : ", skewVal)
The output is:
The data in the dataset is [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
Skewness : -0.057135276077302614
Kurtosis
It is the measure of the extent of distribution of data points in a dataset. It states whether the distribution is heavily-tailed or lightly-tailed.
The kurtosis value of a normal distribution is ideally 0. Negative values show that the distribution is less likely to produce extreme outliers, it is said to be playkurtic. Positive values show that the distribution is more likely to produce extreme outliers, it is said to be leptokurtic.
Calculating Kurtosis
To calculate the Kurtosis value for a dataset in Python, use the kurtosis() method of scipy.stats library. It accepts a dataset and calculates the kurtosis (Fisher or Pearson) of the given dataset.
Syntax
Below is the syntax of kurtosis() method -
scipy.stats.kurtosis(a, axis = 0, fisher= True, bias = True,
nan_policy= ‘propogatre’ , keepdims = False)
Parameters
Here is the list of the parameters of kurtosis() method -
- a: input array for calculation
- axis: (int or None, default = 0), the axis along with computation is to be made.
- fisher: (boolean value, optional), if true, kurtosis is the calculation for base value 0. For False value, it is calculated for base value 3.
- bias: (boolean value, optional), states whether to make calculations statistical bias or not.
- nan_policy: defines how to handle Nn inputs.
- propagate: return NaN as the output of NaN input values.
- omit: omits all NaN values for calculation, and computes rest. If no values remain then return NaN.
- raise: if the NaN value is present, an error is raised.
- keepdims: (boolean, default: False) If this is set to True, the axes that are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Example: Python program to calculate Kurtosis
# Import the library to use kurtosis() method
from scipy.stats import kurtosis
data = [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
print("The data in the dataset is", data)
kurtVal = kurtosis(data)
print("Skewness : ", kurtVal)
The output is:
The data in the dataset is [2, 5, 7, 1, 7, 4, 8, 11, 6, 8, 3, 10]
Skewness : -0.9772160911356353
Documentation references
Python SciPy Programs »