平均数、众数、中位数、极差、方差、标准差……
Python代码实现
第一步:添加数据到列表
def num(a):
if float(a) == int(a):
return int(a)
return float(a)
#添加数据
li = []
print("请逐条添加数据! (若退出请输入0000)")
while True:
print("请输入:")
x = input()
if x == "0000":
break
li.append(num(float(x)))
该自定义函数实现了取整功能,在不影响数值的情况下舍去小数部分。以上代码段用于添加一组数据。
功能一:最大值、最小值、总和
print("最大值:",max(li))
print("最小值:",min(li))
print("总和:",sum(li))
功能二:平均数
平均数,
一种统计学术语,
是指表示一组数据集中趋势的一种量度,
即通过将一组数据中的所有数值相加后除以该组数据的个数而得出的结果。
它反映了数据分布中心的重要特征。
解答平均数应用题的关键在于明确"总量"及其对应的"份数"关系。
avg = sum(li) / len(li)
print("平均数:",avg)
功能三:众数
在一组数据中出现次数最多、频次最高的数值被称为众数;然而,在某些情况下,这组数据可能有多个数值并列拥有最高频次。
d = {}
for i in li:
ss = d.get(i)
if ss == None:
d[i] = 1
else:
d[i] += 1
for i in d.items():
if i[1] == max(d.values()):
print("众数:",i[0])
其中,d 为字典,用于存储各个数据出现的次数,字典的键为数据,值为次数。
功能四:中位数
For a finite set of numbers, instead of directly finding the median, you can sort all the observations from low to high and then pick out the middle one. If there are an even number of observations, you can take the average of those two middle ones as the median.
lis = sorted(li)
if len(lis) % 2 == 1:
print("中位数:",lis[int((len(lis) - 1) / 2)])
else:
print("中位数:",(lis[int(len(lis) / 2 - 1)] + lis[int(len(lis) / 2)]) / 2)
功能五:极差
Range, commonly referred to as the range or total spread, is denoted by R and serves as a statistical measure to quantify the variability within a dataset. It is used as a statistical measure to quantify the variability within a dataset. Range, commonly referred to as the range or total spread, is denoted by R and serves as a measure of dispersion in statistical data.
print("极差:",max(li) - min(li))
功能六:方差与标准差
在统计学领域中,方差(样本方差)被定义为各个数据点与其均值之间差异平方后的平均数值。这一指标在分析数据波动性方面具有重要价值,在诸多实际应用中发挥着关键作用。
此外,在统计学中,
我们亦将之称为均方偏差,
其计算方法为对方程中的各项离散程度进行量化评估。
sum1 = 0
for i in li:
sum1 += (i - avg) *
print("方差:",sum1 / len(li))
print("标准差:",(sum1 / len(li)) ** (1 / 2))
以上所有代码的运行效果:

希望这些功能能对大家起到帮助!
