Python统计模块用法示例

本文概述

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``

mid_low()

mid_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``

mid_high()

mid_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``