您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Python程序计算标准差

2024/11/25 13:37:17发布41次查看
在本文中,我们将学习如何实现 python 程序来计算数据集的标准差。
考虑在任意坐标轴上绘制的一组值。这些值集的标准偏差称为总体,定义为它们之间的变化。如果标准差较低,则绘制的值会接近平均值。但如果标准差较高,值就会离平均值更远。
它由数据集方差的平方根表示。标准差有两种类型 -
人口标准差是从人口的每个数据值计算得出的。因此,它是一个固定的值。数学公式定义如下 -
$$\mathrm{sd\:=\:\sqrt{\frac{\sum(x_i\:-\:x_m)^2}{n}}}$$
where,
(在哪里)xm 是数据集的平均值。
xi 是数据集的元素。
n是数据集中的元素数量。
然而,样本标准差是仅针对人口的某些数据值计算的统计量,因此其值取决于所选择的样本。数学公式定义如下 −
$$\mathrm{sd\:=\:\sqrt{\frac{\sum(x_i\:-\:x_m)^2}{n\:-\:1}}}$$
where,
(在哪里)xm 是数据集的平均值。
xi 是数据集的元素。
n是数据集中的元素数量。
输入输出场景现在让我们看看不同数据集的一些输入输出场景 -
假设数据集仅包含正整数 -
input: [2, 3, 4, 1, 2, 5]result: population standard deviation: 1.3437096247164249sample standard deviation: 0.8975274678557505
假设数据集只包含负整数 -
input: [-2, -3, -4, -1, -2, -5]result: population standard deviation: 1.3437096247164249sample standard deviation: 0.8975274678557505
假设数据集仅包含正整数和负整数 -
input: [-2, -3, -4, 1, 2, 5]result: population standard deviation: 3.131382371342656sample standard deviation: 2.967415635794143
使用数学公式我们在同一篇文章中已经看到了标准差的公式;现在让我们来看看用python程序在各种数据集上实现数学公式。
示例在下面的示例中,我们导入 math 库并通过应用 sqrt() 内置函数来计算数据集的标准差其方差的方法。
import math#declare the dataset listdataset = [2, 3, 4, 1, 2, 5]#find the mean of datasetsm=0for i in range(len(dataset)): sm+=dataset[i] mean = sm/len(dataset)#calculating population standard deviation of the datasetdeviation_sum = 0for i in range(len(dataset)): deviation_sum+=(dataset[i]- mean)**2 psd = math.sqrt((deviation_sum)/len(dataset))#calculating sample standard deviation of the datasetssd = math.sqrt((deviation_sum)/len(dataset) - 1)#display outputprint(population standard deviation of the dataset is, psd)print(sample standard deviation of the dataset is, ssd)
输出得到的输出标准差如下 -
population standard deviation of the dataset is 1.3437096247164249sample standard deviation of the dataset is 0.8975274678557505
在numpy模块中使用std()函数在这种方法中,我们导入 numpy 模块,并且只使用 numpy.std() 函数计算一个 numpy 数组的元素的总体标准差。
示例实现以下 python 程序来计算 numpy 数组元素的标准差 -
import numpy as np#declare the dataset listdataset = np.array([2, 3, 4, 1, 2, 5])#calculating standard deviation of the datasetsd = np.std(dataset)#display outputprint(population standard deviation of the dataset is, sd)
输出标准差显示为以下输出 -
population standard deviation of the dataset is 1.3437096247164249
在统计模块中使用 stdev() 和 pstdev() 函数python 中的统计模块提供了名为 stdev() 和 pstdev() 的函数来计算样本数据集的标准差。 python 中的 stdev() 函数仅计算样本标准差,而 pstdev() 函数计算总体标准差。
两个函数的参数和返回类型是相同的。
示例 1:使用 stdev() 函数演示使用stdev()函数来计算数据集的样本标准差的python程序如下所示−
import statistics as st#declare the dataset listdataset = [2, 3, 4, 1, 2, 5]#calculating standard deviation of the datasetsd = st.stdev(dataset)#display outputprint(standard deviation of the dataset is, sd)
输出作为输出获得的数据集的样本标准差如下 -
standard deviation of the dataset is 1.4719601443879744
示例2:使用pstdev()函数演示如何使用 pstdev() 函数查找数据集总体标准差的 python 程序如下 -
import statistics as st#declare the dataset listdataset = [2, 3, 4, 1, 2, 5]#calculating standard deviation of the datasetsd = st.pstdev(dataset)#display outputprint(standard deviation of the dataset is, sd)
输出作为输出获得的数据集的样本标准差如下 -
standard deviation of the dataset is 1.3437096247164249
以上就是python程序计算标准差的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product