Python程式計算標準差

WBOY
發布: 2023-09-06 11:33:06
轉載
2555 人瀏覽過

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
  • 是資料集中的元素數量。

輸入輸出場景

現在讓我們來看看不同資料集的一些輸入輸出場景 -

假設資料集只包含正整數 -

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中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!