Python – 统计中的正态分布
概率分布决定了随机变量采用的所有结果的概率。分布可以是连续分布或离散分布,具体取决于随机变量采用的值。概率分布有几种类型,如正态分布、均匀分布、指数分布等。在本文中,我们将了解正态分布,还将了解如何使用 Python 绘制正态分布。
什么是正态分布
正态分布是一个连续概率分布函数,也称为高斯分布,它的平均值是对称的,并且具有钟形曲线。它是最常用的概率分布之一。它有两个参数
正态分布的公式为
正态分布公式
正态分布的性质
正态分布中的经验法则
使用 Python 的正态分布
Python 编程语言有几个库,可用于绘制正态分布并获取数据点的概率分布函数。
绘制和应用正态分布所需的模块
我们可以使用这些模块来绘制数据点的正态分布曲线。此外,我们
使用 Python 计算单个数据点的概率分布
import numpy as np def normal_dist(x, mean, sd): prob_density = (np.pi*sd) * np.exp(-0.5*((x-mean)/sd)**2) return prob_density mean = 0 sd = 1 x = 1 result = normal_dist(x, mean, sd) print(result)
输出:
1.9054722647301798
用于绘制正态分布的 Python 代码
import numpy as np import matplotlib.pyplot as plt # Mean of the distribution Mean = 100 # satndard deviation of the distribution Standard_deviation = 5 # size size = 100000 # creating a normal distribution data values = np.random.normal(Mean, Standard_deviation, size) # plotting histograph plt.hist(values, 100) # plotting mean line plt.axvline(values.mean(), color='k', linestyle='dashed', linewidth=2) plt.show()
输出:
正态分布图
Python 的正态分布示例
假设班上有 100 名学生,在其中一项数学测试中,学生在该科目中的平均分数为 78,标准差为 25。学生的分数服从正态概率分布。我们可以使用此信息来回答有关学生成绩的一些问题。
获得低于 60 分的学生百分比的 Python 代码
在这里,我们将使用 scipy.stats 模块中的 norm() 函数,使总体平均值的概率分布等于 78,标准差等于 25。
scipy.stats.norm() 是一个正常的连续随机变量。它是作为 rv_continuous 类的实例从泛型方法继承的。它使用特定于此特定发行版的详细信息完成方法。
q : 下尾和上尾概率
x : 分位数
loc : 平均值 .默认值 = 0
scale : [可选]scale 参数。默认 = 1
size : [tuple of ints, optional] shape or random variates.结果 : 正态连续随机变量
# import required libraries from scipy.stats import norm import numpy as np # Given information mean = 78 std_dev = 25 total_students = 100 score = 60 # Calculate z-score for 60 z_score = (score - mean) / std_dev # Calculate the probability of getting a score less than 60 prob = norm.cdf(z_score) # Calculate the percentage of students who got less than 60 marks percent = prob * 100 # Print the result print("Percentage of students who got less than 60 marks:", round(percent, 2), "%")
输出:
Percentage of students who got less than 60 marks: 23.58 %
它指出,大约 23% 的儿童的数学成绩低于 60 分。
分数超过 70 的学生百分比的 Python 代码
获取得分超过 70 的人的百分比。我们首先找到得分低于 70 的人数的概率,然后从 1 中减去概率,得到得分超过 70 的人数。
# import required libraries from scipy.stats import norm import numpy as np # Given information mean = 78 std_dev = 25 total_students = 100 score = 70 # Calculate z-score for 70 z_score = (score - mean) / std_dev # Calculate the probability of getting a more than 70 prob = norm.cdf(z_score) # Calculate the percentage of students who got more than 70 marks percent = (1-prob) * 100 # Print the result print("Percentage of students who got more than / 70 marks: ", round(percent, 2), " %")
输出:
Percentage of students who got more than 70 marks: 62.55 %
分数高于 75 分和低于 85 分的学生百分比的 Python 代码
# import required libraries from scipy.stats import norm import numpy as np # Given information mean = 78 std_dev = 25 total_students = 100 min_score = 75 max_score = 85 # Calculate z-score for 75 z_min_score = (min_score - mean) / std_dev # Calculate z-score for 85 z_max_score = (max_score - mean) / std_dev # Calculate the probability of getting less than 70 min_prob = norm.cdf(z_min_score) # Calculate the probability of getting less than 85 max_prob = norm.cdf(z_max_score) percent = (max_prob-min_prob) * 100 # Print the result print("Percentage of students who got marks between 75 and 85 is", round(percent, 2), "%")
输出:
Percentage of students who got marks between 75 and 85 is 15.8 %
作者:你是不是觉得我很丑