Python statistics模块

Python statistics模块提供了对数值数据进行数理统计的功能。本模块中定义了一些流行的统计函数。

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

原文:https://www.javatpoint.com/python-statistics-module