使用 NumPy 在Python中计算平均值、方差和标准差
麻木的 在Python中是一个通用的数组处理包。它提供了一个高性能的多维数组对象和用于处理这些数组的工具。它是使用Python进行科学计算的基础包。 Numpy 提供了非常简单的方法来计算平均值、方差和标准差。
平均的
平均表示一组数据中的中心值或典型值的数字,特别是众数、中位数或(最常见的)平均值,其计算方法是将集合中值的总和除以它们的数字。 n 个数 x 1 , x 2 , ……x n的平均值的基本公式是
例子:
假设有 8 个数据点,
这8个数据点的平均值是,
使用 Numpy 在Python中的平均值:
可以使用Python中的numpy.average()函数计算平均值。
Syntax:
numpy.average(a, axis=None, weights=None, returned=False)
Parameters:
a: Array containing data to be averaged
axis: Axis or axes along which to average a
weights: An array of weights associated with the values in a
returned: Default is False. If True, the tuple is returned, otherwise only the average is returned
示例 1:
Python
# Python program to get average of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculating average using average()
print(np.average(list))
Python
# Python program to get average of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 40, 2, 502, 177, 7, 9]
# Calculating average using average()
print(np.average(list))
Python
# Python program to get variance of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculating variance using var()
print(np.var(list))
Python
# Python program to get variance of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [212, 231, 234, 564, 235]
# Calculating variance using var()
print(np.var(list))
Python
# Python program to get
# standard deviation of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculating standard
# deviation using var()
print(np.std(list))
Python
# Python program to get
# standard deviation of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [290, 124, 127, 899]
# Calculating standard
# deviation using var()
print(np.std(list))
输出:
5.0
示例 2:
Python
# Python program to get average of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 40, 2, 502, 177, 7, 9]
# Calculating average using average()
print(np.average(list))
输出:
105.57142857142857
方差
方差是所有数字和平均值之间差异的平方和。方差的数学公式如下,
Where,
? is Mean,
N is the total number of elements or frequency of distribution.
例子:
让我们考虑我们平均采用的相同数据集。首先,计算每个数据点与平均值的偏差,并对每个结果求平方,
使用 Numpy 在Python中的差异:
可以使用Python中的numpy.var()函数计算方差。
Syntax:
numpy.var(a, axis=None, dtype=None, out=None, ddof=0, keepdims=
Parameters:
a: Array containing data to be averaged
axis: Axis or axes along which to average a
dtype: Type to use in computing the variance.
out: Alternate output array in which to place the result.
ddof: 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
示例 1:
Python
# Python program to get variance of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculating variance using var()
print(np.var(list))
输出:
4.0
示例 2:
Python
# Python program to get variance of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [212, 231, 234, 564, 235]
# Calculating variance using var()
print(np.var(list))
输出:
18133.359999999997
标准差
标准偏差是方差的平方根。它衡量数据与平均值的差异程度。计算标准差的数学公式如下,
例子:
上述数据的标准差,
使用 Numpy 在Python中的标准偏差:
可以使用Python中的numpy.std()函数计算标准偏差。
Syntax:
numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=
Parameters:
a: Array containing data to be averaged
axis: Axis or axes along which to average a
dtype: Type to use in computing the variance.
out: Alternate output array in which to place the result.
ddof: 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
示例 1:
Python
# Python program to get
# standard deviation of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [2, 4, 4, 4, 5, 5, 7, 9]
# Calculating standard
# deviation using var()
print(np.std(list))
输出:
2.0
示例 2:
Python
# Python program to get
# standard deviation of a list
# Importing the NumPy module
import numpy as np
# Taking a list of elements
list = [290, 124, 127, 899]
# Calculating standard
# deviation using var()
print(np.std(list))
输出:
318.35750344541907