在Python中计算字典的标准偏差
Python字典是一种通用的数据结构,可以轻松完成大量操作。计算标准偏差如下所示。
示例 #1:使用numpy.std()
首先,我们创建一个字典。然后我们通过迭代将所有值存储在列表中。在此之后,我们使用 NumPy 计算列表的标准偏差。
Python3
# importing numpy
import numpy as np
# creating our test dictionary
dicti = {'a': 20, 'b': 32, 'c': 12, 'd': 93, 'e': 84}
# declaring an empty list
listr = []
# appending all the values in the list
for value in dicti.values():
listr.append(value)
# calculating standard deviation using np.std
std = np.std(listr)
# printing results
print(std)
Python3
# creating our test dictionary
dicti = {'a': 20, 'b': 32, 'c': 12, 'd': 93, 'e': 84}
# declaring an empty list
listr = []
# appending all the values in the list
for value in dicti.values():
listr.append(value)
# Standard deviation of list
# Using sum() + list comprehension
mean = sum(listr) / len(listr)
variance = sum([((x - mean) ** 2) for x in listr]) / len(listr)
res = variance ** 0.5
print(res)
Python3
# importing the module
import statistics
# creating the test dictionary
dicti = {'a': 20, 'b': 32, 'c': 12, 'd': 93, 'e': 84}
# declaring an empty list
listr = []
# appending all the values in the list
for value in dicti.values():
listr.append(value)
# Standard deviation of list
# Using pstdev()
res = statistics.pstdev(listr)
print(res)
输出:
33.63569532505609
示例 2:使用列表理解
首先,我们使用循环从字典中创建一个值列表。然后我们计算均值、方差和标准差。
蟒蛇3
# creating our test dictionary
dicti = {'a': 20, 'b': 32, 'c': 12, 'd': 93, 'e': 84}
# declaring an empty list
listr = []
# appending all the values in the list
for value in dicti.values():
listr.append(value)
# Standard deviation of list
# Using sum() + list comprehension
mean = sum(listr) / len(listr)
variance = sum([((x - mean) ** 2) for x in listr]) / len(listr)
res = variance ** 0.5
print(res)
输出:
33.63569532505609
示例 #3:使用pstdev()
Python 内置的统计库提供了一个函数来计算给定列表的标准偏差。
蟒蛇3
# importing the module
import statistics
# creating the test dictionary
dicti = {'a': 20, 'b': 32, 'c': 12, 'd': 93, 'e': 84}
# declaring an empty list
listr = []
# appending all the values in the list
for value in dicti.values():
listr.append(value)
# Standard deviation of list
# Using pstdev()
res = statistics.pstdev(listr)
print(res)
输出:
33.63569532505609