Python| Pandas Series.mad() 计算系列的平均绝对偏差
Pandas 提供了一种非常容易计算 MAD(平均绝对偏差)的方法。 MAD 定义为每个值与平均值之间的平均距离。
用于计算 MAD 的公式为:
Syntax: Series.mad(axis=None, skipna=None, level=None)
Parameters:
axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation.
skipna: Includes NaN values too if False, Result will also be NaN even if a single Null value is included.
level: Defines level name or number in case of multilevel series.
Return Type: Float value
示例 #1:
在此示例中,使用 Pandas .Series() 方法从Python列表创建一个系列。 .mad() 方法在具有所有默认参数的系列上调用。
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# creating list
list =[5, 12, 1, 0, 4, 22, 15, 3, 9]
# creating series
series = pd.Series(list)
# calling .mad() method
result = series.mad()
# display
result
输出:
5.876543209876543
解释:
Calculating Mean of series mean = (5+12+1+0+4+22+15+3+9) / 9 = 7.8888
MAD = | (5-7.88)+(12-7.88)+(1-7.88)+(0-7.88)+(4-7.88)+(22-7.88)+(15-7.88)+(3-7.88)+(9-7.88)) | / 9.00
MAD = (2.88 + 4.12 + 6.88 + 7.88 + 3.88 + 14.12 + 7.12 + 4.88 + 1.12) / 9.00
MAD = 5.8755 (More accurately = 5.876543209876543)