如何在Python中建立鐘形曲線?
眾所周知,如果我們對隨機變數的許多觀測值取平均值,隨著觀測值數量的增加,分佈會收斂到正態分佈。例如,如果我們擲兩個骰子並計算每次骰子顯示的隨機值之和,隨著測量的值的增加,它將顯示一個鐘形曲線,並呈現正態趨勢。還觀察到,如果只進行50次,它不會顯示完美的鐘形曲線;如果重複1000次或更多次,它將顯示一個良好的鐘形曲線。
正態分佈在其有限均值周圍是對稱的。有時,任務是顯示此鐘形曲線以進行資料視覺化。在本文中,使用三個不同的示例,Python庫Plotly與Python程式碼一起用於建立鐘形曲線。在第一個示例中,編寫了Python程式碼來使用兩個骰子的隨機結果之和建立資料集,然後將其用於建立曲線。在另外兩個示例中,使用了來自Kaggle的身高和體重資料集,以表明身高和體重也顯示這些正態鐘形曲線。
示例1:新增兩個骰子上的結果,並使用此資料使用Python和Plotly建立鐘形曲線
示例1的Python程式碼設計步驟
步驟1 - 首先匯入pandas和plotly。Plotly是用於建立不同型別圖表的Python開源繪相簿。
步驟2 - 現在使用random.randint(1, 6)獲取骰子一和骰子二的隨機值。新增這些值。
步驟3 - 將所有這些總和插入dice_oneplustwo[]中。
步驟4 - 列印儲存在dice_oneplustwo[]中的值。
步驟5 - 從 plotly.figure_factory 中,使用名為create_distplot()的函式建立圖表。
步驟6 - 使用cmd視窗執行程式。該圖將在新標籤頁中開啟並在瀏覽器中顯示鐘形曲線。
建立一個名為dice.py的檔案。在此檔案中編寫以下程式碼
import plotly.figure_factory as ffdemo import random dice_oneplustwo = [] #throw 2 dices at same time and add their outcomes #Repeat this 2000 times for i in range(0, 2000): diceone = random.randint(1, 6) dicetwo = random.randint(1, 6) dice_oneplustwo.append(diceone + dicetwo) print(dice_oneplustwo) dice_bellcurve = ffdemo.create_distplot([dice_oneplustwo],["Result"],show_hist = False) dice_bellcurve.show()
在命令視窗中執行Python檔案
檢視結果的鐘形曲線 - 示例1
示例2:使用Kaggle資料集中的身高資料顯示鐘形曲線
Python程式碼設計步驟
步驟1 - 首先匯入pandas和plotly。Plotly是用於建立不同型別圖表的Python開源繪相簿。
步驟2 - 登入Kaggle並下載資料集
(SOCR-HeightWeight.csv) 獲取身高和體重資料。步驟3 - 讀取此csv檔案並建立一個數據框。列印此資料框。
步驟4 - 從 plotly.figure_factory 中,使用名為create_distplot()的函式。
步驟5 - 使用身高資料列建立圖表。(示例2) 或使用體重資料列建立圖表。(示例3)
步驟6 - 使用cmd視窗執行程式。該圖將在新標籤頁中開啟並在瀏覽器中顯示鐘形曲線。
儲存資料檔案/csv檔案,資料分析需要用到
為了建立鐘形曲線,我們將使用Kaggle上提供的關於身高的資料集(SOCR-HeightWeight.csv)。登入Kaggle,然後從此連結下載csv檔案:
https://www.kaggle.com/datasets/burnoutminer/heights-and-weightsdataset/download?datasetVersionNumber=1
建立一個名為Weight.py的檔案。在此檔案中編寫以下程式碼。
# import this library to make the bell curve import plotly.figure_factory as ffdemo # Pandas library import pandas as pdd # read the csv and make a dataframe datafrm = pdd.read_csv("C:\Users\saba2\Desktop\article\articles_py\bellcurve\SOCR-HeightWeight.csv") #print the dataframe print(datafrm) #make the bell curve bell_fig = ffdemo.create_distplot([datafrm["Height(Inches)"].tolist()], ["Height"], show_hist=False) #show the bell curve bell_fig.show()
在命令視窗中執行python檔案 -
檢視結果的鐘形曲線 - 示例2
示例3:使用Kaggle資料集中的體重資料顯示鐘形曲線。
Python程式碼設計步驟
步驟1 - 匯入所需的庫。Plotly是用於建立不同型別圖表的Python開源繪相簿。
步驟2 - 登入Kaggle並下載資料集
(SOCR-HeightWeight.csv) for heights and weights
步驟3 - 讀取此csv檔案並建立一個數據框。列印此資料框。
步驟4 - 從plotly.figure_factory中,使用名為create_distplot()的函式。
步驟5 - 利用體重資料列建立圖表。
步驟6 - 使用cmd視窗執行程式。該圖將在新標籤頁中開啟並在瀏覽器中顯示鐘形曲線。
儲存資料分析所需的資料檔案/csv檔案 -
為了建立鐘形曲線,我們將使用Kaggle上提供的關於身高的資料集(SOCR-HeightWeight.csv)。登入Kaggle,然後從此連結下載CSV檔案:
https://www.kaggle.com/datasets/burnoutminer/heights-and-weightsdataset/download?datasetVersionNumber=1
建立一個名為Weight.py的檔案。在此檔案中編寫以下程式碼。
# import this library to make the bell curve import plotly.figure_factory as ffdemo # Pandas library import pandas as pdd # read the csv and make a dataframe datafrm = pdd.read_csv("C:\Users\saba2\Desktop\article\articles_py\bellcurve\SO CR-HeightWeight.csv") #print the dataframe print(datafrm) #make the bell curve bell_fig = ffdemo.create_distplot([datafrm["Weight(Pounds)"].tolist()], ["Weight In Pounds"], show_hist=False) #show the bell curve bell_fig.show()
在命令視窗中執行python檔案 -
檢視結果的鐘形曲線 - 示例3
在這篇關於Python和Plotly的文章中,給出了三個不同的示例,說明了如何使用名為Plotly的Python庫建立鐘形曲線。首先,給出了一個方法,透過重複投擲兩個骰子2000次並新增其結果來建立一個具有正態趨勢的資料集。然後使用此資料集來建立鐘形曲線。在示例2和示例3中,根據從Kaggle獲取的身高和體重資料集編寫了Python程式,並使用此資料建立了鐘形曲線。