Python程序计算标准差

WBOY
发布: 2023-09-06 11:33:06
转载
5138人浏览过

python程序计算标准差

在本文中,我们将学习如何实现 Python 程序来计算数据集的标准差。

考虑在任意坐标轴上绘制的一组值。这些值集的标准偏差称为总体,定义为它们之间的变化。如果标准差较低,则绘制的值会接近平均值。但如果标准差较高,值就会离平均值更远。

它由数据集方差的平方根表示。标准差有两种类型 -

人口标准差是从人口的每个数据值计算得出的。因此,它是一个固定的值。数学公式定义如下 -

立即学习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是数据集中的元素数量。

输入输出场景

现在让我们看看不同数据集的一些输入输出场景 -

45°C商城系统
45°C商城系统

系统介绍 45°C 商城系统,以 Thinkphp5.0 + Uniapp + Layui2.9 + Vue 为技术基石,精心打造出的全新 MINI 商城应用。其功能覆盖全面,无论是 PC 商城、H5 商城,还是公众号商城、微信小程序以及抖音小程序的制作都能完美胜任。采用标准系统结合插件模式开发,用户能够极为便捷地定制专属的个性模块。整个系统,从程序设计到 UI 呈现,都秉持着一贯的小而美理念。程

45°C商城系统 0
查看详情 45°C商城系统

假设数据集仅包含正整数 -

Input: [2, 3, 4, 1, 2, 5]
Result: Population Standard Deviation: 1.3437096247164249
Sample Standard Deviation: 0.8975274678557505
登录后复制

假设数据集只包含负整数 -

Input: [-2, -3, -4, -1, -2, -5]
Result: Population Standard Deviation: 1.3437096247164249
Sample Standard Deviation: 0.8975274678557505
登录后复制

假设数据集仅包含正整数和负整数 -

Input: [-2, -3, -4, 1, 2, 5]
Result: Population Standard Deviation: 3.131382371342656
Sample Standard Deviation: 2.967415635794143
登录后复制

使用数学公式

我们在同一篇文章中已经看到了标准差的公式;现在让我们来看看用Python程序在各种数据集上实现数学公式。

示例

在下面的示例中,我们导入 math 库并通过应用 sqrt() 内置函数来计算数据集的标准差其方差的方法。

import math

#declare the dataset list
dataset = [2, 3, 4, 1, 2, 5]

#find the mean of dataset
sm=0
for i in range(len(dataset)):
   sm+=dataset[i]
   mean = sm/len(dataset)

#calculating population standard deviation of the dataset
deviation_sum = 0
for i in range(len(dataset)):
   deviation_sum+=(dataset[i]- mean)**2
   psd = math.sqrt((deviation_sum)/len(dataset))

#calculating sample standard deviation of the dataset
ssd = math.sqrt((deviation_sum)/len(dataset) - 1)

#display output
print("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.3437096247164249
Sample standard deviation of the dataset is 0.8975274678557505
登录后复制

在numpy模块中使用std()函数

在这种方法中,我们导入 numpy 模块,并且只使用 numpy.std() 函数计算一个 numpy 数组的元素的总体标准差。

示例

实现以下 python 程序来计算 numpy 数组元素的标准差 -

import numpy as np

#declare the dataset list
dataset = np.array([2, 3, 4, 1, 2, 5])

#calculating standard deviation of the dataset
sd = np.std(dataset)

#display output
print("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 list
dataset = [2, 3, 4, 1, 2, 5]

#calculating standard deviation of the dataset
sd = st.stdev(dataset)

#display output
print("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 list
dataset = [2, 3, 4, 1, 2, 5]

#calculating standard deviation of the dataset
sd = st.pstdev(dataset)

#display output
print("Standard Deviation of the dataset is", sd)
登录后复制

输出

作为输出获得的数据集的样本标准差如下 -

Standard Deviation of the dataset is 1.3437096247164249
登录后复制

以上就是Python程序计算标准差的详细内容,更多请关注php中文网其它相关文章!

相关标签:
python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号