Python – 統(tǒng)計中的正態(tài)分布

?

概率分布決定了隨機變量采用的所有結(jié)果的概率。分布可以是連續(xù)分布或離散分布,具體取決于隨機變量采用的值。概率分布有幾種類型,如正態(tài)分布、均勻分布、指數(shù)分布等。在本文中,我們將了解正態(tài)分布,還將了解如何使用 Python 繪制正態(tài)分布。

什么是正態(tài)分布

正態(tài)分布是一個連續(xù)概率分布函數(shù),也稱為高斯分布,它的平均值是對稱的,并且具有鐘形曲線。它是最常用的概率分布之一。它有兩個參數(shù)

  • Mean(μ) - 它表示分布的中心
  • Standard Deviation(σ) – 它表示曲線中的分布

正態(tài)分布的公式為

正態(tài)分布公式?


Python – 統(tǒng)計中的正態(tài)分布的圖2 編輯

正態(tài)分布公式

正態(tài)分布的性質(zhì)

  • Symmetric distribution (對稱分布) – 正態(tài)分布相對于其平均值點是對稱的。這意味著分布在其平均值上完全平衡,兩側(cè)有一半的數(shù)據(jù)。
  • 鐘形曲線 – 正態(tài)分布的圖形采用鐘形曲線形式,其中大多數(shù)點累積在其平均位置。此曲線的形狀由分布的平均值和標準差決定
  • 經(jīng)驗法則 – 正態(tài)分布曲線遵循經(jīng)驗法則,其中 68% 的數(shù)據(jù)位于與圖表平均值的 1 個標準差范圍內(nèi),95% 的數(shù)據(jù)位于與平均值的 2 個標準差之間,99.7% 的數(shù)據(jù)位于與平均值的 3 個標準差之間。

正態(tài)分布中的經(jīng)驗法則?


Python – 統(tǒng)計中的正態(tài)分布的圖4 編輯

正態(tài)分布中的經(jīng)驗法則

  • Additive Rule (加法規(guī)則) – 兩個或多個正態(tài)分布之和將始終為正態(tài)分布。
  • 中心極限定理 – 它指出,如果我們?nèi)莫毩⒑拖嗤姆植际诫S機變量收集的大型無數(shù)據(jù)點的平均值,那么這些平均值將遵循正態(tài)分布,而不管它們的原始分布如何。

使用 Python 的正態(tài)分布

Python 編程語言有幾個庫,可用于繪制正態(tài)分布并獲取數(shù)據(jù)點的概率分布函數(shù)。

繪制和應(yīng)用正態(tài)分布所需的模塊

  • Numpy – 一個用于數(shù)值數(shù)學計算和處理多維 ndarray 的 Python 庫,它還具有非常大的數(shù)學函數(shù)集合來作此數(shù)組。
  •  Pandas  一個基于 NumPy 構(gòu)建的 Python 庫,用于有效的矩陣乘法和數(shù)據(jù)幀作,它還用于數(shù)據(jù)清理、數(shù)據(jù)合并、數(shù)據(jù)重塑和數(shù)據(jù)聚合
  • Matplotlib – 它用于繪制 2D 和 3D 可視化圖,它還支持多種輸出格式,包括圖表
  • Scipy – 用于求解數(shù)學方程式和算法的 Python 庫。它是統(tǒng)計和微積分函數(shù)最常用的庫之一。

我們可以使用這些模塊來繪制數(shù)據(jù)點的正態(tài)分布曲線。此外,我們

使用 Python 計算單個數(shù)據(jù)點的概率分布

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

用于繪制正態(tài)分布的 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()

輸出:

正態(tài)分布圖?

編輯

正態(tài)分布圖

Python 的正態(tài)分布示例

假設(shè)班上有 100 名學生,在其中一項數(shù)學測試中,學生在該科目中的平均分數(shù)為 78,標準差為 25。學生的分數(shù)服從正態(tài)概率分布。我們可以使用此信息來回答有關(guān)學生成績的一些問題。

獲得低于 60 分的學生百分比的 Python 代碼

在這里,我們將使用 scipy.stats 模塊中的 norm() 函數(shù),使總體平均值的概率分布等于 78,標準差等于 25。

scipy.stats.norm() 是一個正常的連續(xù)隨機變量。它是作為 rv_continuous 類的實例從泛型方法繼承的。它使用特定于此特定發(fā)行版的詳細信息完成方法。

q : 下尾和上尾概率
x : 分位數(shù)
loc : 平均值 .默認值 = 0
scale : [可選]scale 參數(shù)。默認 = 1
size : [tuple of ints, optional] shape or random variates.

結(jié)果 : 正態(tài)連續(xù)隨機變量

# 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% 的兒童的數(shù)學成績低于 60 分。

分數(shù)超過 70 的學生百分比的 Python 代碼

獲取得分超過 70 的人的百分比。我們首先找到得分低于 70 的人數(shù)的概率,然后從 1 中減去概率,得到得分超過 70 的人數(shù)。

# 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 %

分數(shù)高于 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 %

?

登錄后免費查看全文
立即登錄
App下載
技術(shù)鄰APP
工程師必備
  • 項目客服
  • 培訓(xùn)客服
  • 平臺客服

TOP