使用Python展示統計學中的68-95-99.7規則

WBOY
發布: 2023-09-05 13:33:10
轉載
627 人瀏覽過

使用Python展示統計學中的68-95-99.7規則

統計學為我們提供了分析和理解數據的強大工具。統計學中的基本概念之一是 68-95-99.7 規則,也稱為經驗規則或三西格瑪規則。這條規則使我們能夠根據資料的標準差對資料的分佈做出重要的推論。在這篇文章中,我們將探討 68-95-99.7 規則並示範如何使用 Python 應用它。

68-95-99.7 規則概述

68-95-99.7 規則提供了一種方法來估計常態分佈中與平均值的一定標準差範圍內的資料百分比。根據這個規則 -

  • 大約 68% 的資料落在平均值的一個標準差範圍內。

  • 大約 95% 的資料落在平均值的兩個標準差範圍內。

  • 大約 99.7% 的資料落在平均值的三個標準差範圍內。

這些百分比適用於遵循常態分佈(也稱為鐘形曲線)的資料集。了解這項規則使我們能夠快速評估資料的傳播並識別異常值或異常觀察結果。

在 Python 中實作 68-95-99.7 規則

為了示範 68-95-99.7 規則的實際應用,我們將使用 Python 及其流行的資料分析函式庫 NumPy。 NumPy 提供高效率的數值運算和統計函數,幫助我們計算必要的值。讓我們先導入所需的函式庫

import numpy as np import matplotlib.pyplot as plt
登入後複製

接下來,我們將使用 numpy.random.normal() 函數產生一個遵循常態分佈的隨機資料集。我們將使用平均值 0 和標準差 1

#
np.random.seed(42) # Set the random seed for reproducibility data = np.random.normal(0, 1, 10000)
登入後複製

現在,我們可以計算資料集的平均值和標準差

#
mean = np.mean(data) std = np.std(data)
登入後複製

為了視覺化資料和 68-95-99.7 規則覆蓋的區域,我們可以使用 matplotlib.pyplot.hist() 函數建立直方圖

#
plt.hist(data, bins=30, density=True, alpha=0.7) # Plot the mean and standard deviations plt.axvline(mean, color='r', linestyle='dashed', linewidth=1, label='Mean') plt.axvline(mean - std, color='g', linestyle='dashed', linewidth=1, label='1 STD') plt.axvline(mean + std, color='g', linestyle='dashed', linewidth=1) plt.axvline(mean - 2*std, color='b', linestyle='dashed', linewidth=1, label='2 STD') plt.axvline(mean + 2*std, color='b', linestyle='dashed', linewidth=1) plt.axvline(mean - 3*std, color='m', linestyle='dashed', linewidth=1, label='3 STD') plt.axvline(mean + 3*std, color='m', linestyle='dashed', linewidth=1) plt.legend() plt.xlabel('Value') plt.ylabel('Density') plt.title('Histogram of the Dataset') plt.show()
登入後複製

產生的直方圖將顯示資料的分佈以及虛線標記的平均值和標準差。

為了計算每個範圍覆蓋的百分比,我們可以使用常態分佈的累積分佈函數(CDF)。 NumPy 函數 numpy.random.normal() 產生常態分佈的數據,但 NumPy 也提供 numpy.random.normal() 來計算 CDF

#
# Calculate the percentage within one standard deviation pct_within_1_std = np.sum(np.logical_and(data >= mean - std, data 7lt;= mean + std)) / len(data) # Calculate the percentage within two standard deviations pct_within_2_std = np.sum(np.logical_and(data >= mean - 2*std, data <= mean + 2*std)) / len(data) # Calculate the percentage within three standard deviations pct_within_3_std = np.sum(np.logical_and(data >= mean - 3*std, data <= mean + 3*std)) / len(data) print("Percentage within one standard deviation: {:.2%}".format(pct_within_1_std)) print("Percentage within two standard deviations: {:.2%}".format(pct_within_2_std)) print("Percentage within three standard deviations: {:.2%}".format(pct_within_3_std))
登入後複製

當您執行此程式碼時,您將看到資料的百分比落在平均值的 1、2 和 3 個標準差範圍內。

Percentage within one standard deviation: 68.27% Percentage within two standard deviations: 95.61% Percentage within three standard deviations: 99.70%
登入後複製

這些結果與 68-95-99.7 規則的預期百分比非常一致。

68-95-99.7 規則的解釋

每個範圍涵蓋的百分比都有特定的解釋。落在平均值的一個標準差之內的數據相對常見,而落在平均值的三個標準差之外的數據則被認為是罕見的。理解這些解釋有助於對數據做出有意義的推論。

68-95-99.7 規則的限制

雖然 68-95-99.7 規則是一個有價值的指南,但它可能無法準確地應用於顯著偏離常態分佈的資料集。在處理此類數據集時,考慮其他統計技術並進行進一步分析至關重要。

異常值和 68-95-99.7 規則

異常值會極大地影響每個範圍所涵蓋的百分比的準確性。這些極端值可能會扭曲分佈並影響規則的有效性。正確識別和處理異常值對於確保準確的統計分析非常重要。

現實生活中的例子

68-95-99.7 規則適用於各個領域。例如,它在品質控制流程中識別有缺陷的產品、在財務分析中評估風險和投資回報、在醫療保健研究中了解患者特徵以及在許多其他領域中了解資料分佈至關重要。

當您深入了解統計數據時,請考慮探索補充 68-95-99.7 規則的其他概念。偏度、峰度、置信區間、假設檢定和迴歸分析只是可以進一步增強您對資料的理解和分析的統計工具的幾個範例。

結論

68-95-99.7 規則是統計學中的一個強大概念,它使我們能夠根據資料的標準差來理解資料的分佈。透過應用此規則,我們可以估計落在平均值周圍特定範圍內的資料比例。在本部落格中,我們使用 Python 和 NumPy 庫產生隨機資料集,將其視覺化,並計算每個範圍覆蓋的百分比。了解這條規則使我們能夠對數據做出有意義的推論並識別潛在的異常值或異常觀察結果。

以上是使用Python展示統計學中的68-95-99.7規則的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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