Python中的统计函数 |第 1 组(中心位置的平均值和测量值)
Python能够使用文件“ statistics ”来操作一些统计数据并计算各种统计操作的结果,这在数学领域很有用。
中心位置函数的重要平均值和度量:
1. mean() :- 此函数返回其参数中传递的数据的平均值或平均值。如果传递的参数为空,则会引发StatisticsError 。
2. mode() :- 此函数返回出现次数最多的数字。如果传递的参数为空,则会引发StatisticsError 。
# Python code to demonstrate the working of
# mean() and mode()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 3, 3, 2, 2, 2, 1]
# using mean() to calculate average of list elements
print ("The average of list values is : ",end="")
print (statistics.mean(li))
# using mode() to print maximum occurring of list elements
print ("The maximum occurring element is : ",end="")
print (statistics.mode(li))
输出:
The average of list values is : 2.0
The maximum occurring element is : 2
3. median() :- 该函数用于计算中位数,即数据的中间元素。如果传递的参数为空,则会引发StatisticsError 。
4. median_low() :- 该函数在奇数个元素的情况下返回数据的中位数,但在偶数个元素的情况下,返回两个中间元素中的较低值。如果传递的参数为空,则会引发StatisticsError 。
5. median_high() :- 该函数在奇数个元素的情况下返回数据的中位数,但在偶数个元素的情况下,返回两个中间元素中较高的一个。如果传递的参数为空,则会引发StatisticsError 。
# Python code to demonstrate the working of
# median(), median_low() and median_high()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 2, 3, 3, 3]
# using median() to print median of list elements
print ("The median of list element is : ",end="")
print (statistics.median(li))
# using median_low() to print low median of list elements
print ("The lower median of list element is : ",end="")
print (statistics.median_low(li))
# using median_high() to print high median of list elements
print ("The higher median of list element is : ",end="")
print (statistics.median_high(li))
输出:
The median of list element is : 2.5
The lower median of list element is : 2
The higher median of list element is : 3
6. median_grouped() :- 该函数用于计算组中位数,即数据的第 50 个百分位数。如果传递的参数为空,则会引发StatisticsError 。
# Python code to demonstrate the working of
# median_grouped()
# importing statistics to handle statistical operations
import statistics
# initializing list
li = [1, 2, 2, 3, 3, 3]
# using median_grouped() to calculate 50th percentile
print ("The 50th percentile of data is : ",end="")
print (statistics.median_grouped(li))
输出:
The 50th percentile of data is : 2.5
Python中的统计函数 |第 2 组(点差测量)