Python statistics统计模块
Python统计模块提供了计算数值数据的数学统计函数。此模块中定义了一些流行的统计函数。
mean()函数
mean()函数用于计算列表中数字的算术平均值。
示例
import statistics
# list of positive integer numbers
datasets = [5, 2, 7, 4, 2, 6, 8]
x = statistics.mean(datasets)
# Printing the mean
print("Mean is :", x)
输出:
Mean is : 4.857142857142857
median()函数
median()函数用于返回列表中数值数据的中间值。
示例
import statistics
datasets = [4, -5, 6, 6, 9, 4, 5, -2]
# Printing median of the
# random data-set
print("Median of data-set is : % s "
% (statistics.median(datasets)))
输出:
Median of data-set is : 4.5
mode()函数
mode()函数返回列表中出现最频繁的数据。
示例
import statistics
# declaring a simple data-set consisting of real valued positive integers.
dataset =[2, 4, 7, 7, 2, 2, 3, 6, 6, 8]
# Printing out the mode of given data-set
print("Calculated Mode % s" % (statistics.mode(dataset)))
输出:
Calculated Mode 2
stdev()函数
stdev()函数用于计算给定样本(以列表形式给出)的标准差。
示例
import statistics
# creating a simple data - set
sample = [7, 8, 9, 10, 11]
# Prints standard deviation
print("Standard Deviation of sample is % s "
% (statistics.stdev(sample)))
输出:
Standard Deviation of sample is 1.5811388300841898
median_low()
中位数低(median_low)函数用于返回列表中数值数据的低中位数。
示例
import statistics
# simple list of a set of integers
set1 = [4, 6, 2, 5, 7, 7]
# Note: low median will always be a member of the data-set.
# Print low median of the data-set
print("Low median of data-set is % s "
% (statistics.median_low(set1)))
输出:
Low median of the data-set is 5
median_high()
median_high函数用于返回列表中数值数据的高中位数。
示例
import statistics
# list of set of the integers
dataset = [2, 1, 7, 6, 1, 9]
print("High median of data-set is %s "
% (statistics.median_high(dataset)))
输出:
High median of the data-set is 6