python数据统计模块

编程入门 行业动态 更新时间:2024-10-07 12:29:22

python<a href=https://www.elefans.com/category/jswz/34/1749293.html style=数据统计模块"/>

python数据统计模块

1、mean()

计算平均值

>>> import statistics

>>> statistics.mean([1, 2, 3, 4, 5, 6, 7, 8, 9])

5.0

>>> statistics.mean(range(1,10))

5.0

>>> import fractions

>>> x = [(3, 7), (1, 21), (5, 3), (1, 3)]

>>> y = [fractions.Fraction(*item) for item in x]

>>> y

[Fraction(3, 7), Fraction(1, 21), Fraction(5, 3), Fraction(1, 3)]

>>> statistics.mean(y)

Fraction(13, 21)

>>> import decimal

>>> x = ('0.5', '0.75', '0.625', '0.375')

>>> y = map(decimal.Decimal, x)

>>> y

>>> list(y)

[Decimal('0.5'), Decimal('0.75'), Decimal('0.625'), Decimal('0.375')]

>>> statistics.mean(y)

Traceback (most recent call last):

File "", line 1, in

statistics.mean(y)

File "C:\Python 3.5\lib\statistics.py", line 292, in mean

raise StatisticsError('mean requires at least one data point')

statistics.StatisticsError: mean requires at least one data point

>>> list(y)

[]

>>> y = map(decimal.Decimal, x)

>>> statistics.mean(y)

Decimal('0.5625')

2、median()、median_low()、median_high()、median_grouped()

各种中位数

>>> statistics.median([1, 3, 5, 7])

4.0

>>> statistics.median_low([1, 3, 5, 7])

3

>>> statistics.median_high([1, 3, 5, 7])

5

>>> statistics.median([1, 3, 7])

3

>>> statistics.median([5, 3, 7])

5

>>> statistics.median(range(1,10))

5

>>> statistics.median_low([5, 3, 7])

5

>>> statistics.median_high([5, 3, 7])

5

>>> statistics.median_grouped([5, 3, 7])

5.0

>>> statistics.median_grouped([5, 3, 7, 1])

4.5

>>> statistics.median_grouped([52, 52, 53, 54])

52.5

>>> statistics.median_low([52, 52, 53, 54])

52

>>> statistics.median_high([52, 52, 53, 54])

53

>>> statistics.median_high([1, 3, 3, 5, 7])

3

>>> statistics.median_low([1, 3, 3, 5, 7])

3

>>> statistics.median_grouped([1, 3, 3, 5, 7])

3.25

>>> statistics.median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])

3.7

>>> statistics.median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5], interval=2)

3.4

3、mode()

返回最常见数据或出现次数最多的数据(most common data)

>>> statistics.mode([1, 3, 5, 7])

Traceback (most recent call last):

File "", line 1, in

statistics.mode([1, 3, 5, 7])

File "C:\Python 3.5\lib\statistics.py", line 434, in mode

'no unique mode; found %d equally common values' % len(table)

statistics.StatisticsError: no unique mode; found 4 equally common values

>>> statistics.mode([1, 3, 5, 7, 3])

3

>>> statistics.mode([1, 3, 5, 7, 3, 5])

Traceback (most recent call last):

File "", line 1, in

statistics.mode([1, 3, 5, 7, 3, 5])

File "C:\Python 3.5\lib\statistics.py", line 434, in mode

'no unique mode; found %d equally common values' % len(table)

statistics.StatisticsError: no unique mode; found 2 equally common values

>>> statistics.mode([1, 3, 5, 7, 3, 5, 5])

5

>>> statistics.mode(["red", "blue", "blue", "red", "green", "red", "red"])

'red'

>>> statistics.mode(list(range(5)) + [3])

3

4、pstdev()

返回总体标准差(population standard deviation ,the square root of the population variance)。

>>> statistics.pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])

0.986893273527251

>>> statistics.pstdev(range(20))

5.766281297335398

>>> statistics.pstdev([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])

2.8722813232690143

5、pvariance()

返回总体方差(population variance)或二次矩(second moment)。

>>> statistics.pvariance([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])

0.9739583333333334

>>> statistics.pvariance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])

8.25

>>> x = [1, 2, 3, 4, 5, 10, 9, 8, 7, 6]

>>> mu = statistics.mean(x)

>>> mu

5.5

>>> statistics.pvariance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6], mu)

8.25

>>> statistics.pvariance(range(20))

33.25

>>> statistics.pvariance((random.randint(1,10000) for i in range(30)))

10903549.933333334

6、variance()、stdev()

计算样本方差(sample variance)和样本标准差(sample standard deviation,the square root of the sample variance,也叫均方差)。

>>> statistics.variance((random.randint(1,10000) for i in range(30)))

10229013.655172413

>>> statistics.stdev((random.randint(1,10000) for i in range(30)))

3106.2902337180203

>>> _ * _ #注意,上面的两个样本数据并不一样,因为都是随机数

9649039.016091954

>>> statistics.variance(range(20))

35.0

>>> statistics.stdev(range(20))

5.916079783099616

>>> _ * _

35.0

>>> statistics.variance([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])

9.166666666666666

>>> statistics.stdev([1, 2, 3, 4, 5, 10, 9, 8, 7, 6])

3.0276503540974917

>>> statistics.variance([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])

1.16875

>>> statistics.stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])

1.0810874155219827

>>> _ * _

1.1687500000000002

>>> statistics.variance([3, 3, 3, 3, 3, 3])

0.0

>>> statistics.stdev([3, 3, 3, 3, 3, 3])

0.0

更多推荐

python数据统计模块

本文发布于:2024-02-14 13:43:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1763517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据统计   模块   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!