賽馬正態分布圖
1.制作IQ數據圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#使用%matplotlib命令可以將matplotlib的圖表直接嵌入到Notebook之中,或者使用指定的界面庫顯示圖表,它有一個參數指定matplotlib圖表的顯示方式
*#inline表示將圖表嵌入到Notebook中。
%matplotlib inline
#為了使畫出來的圖支持 retina格式
%config InlineBackend.figure_format = 'retina'
iq_data = pd.read_csv('IQscore.csv')
len(iq_data)
70
iq = iq_data['IQ']
mean = iq.mean()
mean
100.82857142857142
std = iq.std()
std
15.015905990389498
#normfun正態分布函數,mu: 均值,sigma:標準差,pdf:概率密度函數,np.exp():概率密度函數公式
def normfun(x,mu, sigma):
pdf = np.exp(-((x - mu)**2) / (2* sigma**2)) / (sigma * np.sqrt(2*np.pi))
return pdf
# x的范圍為60-150,以1為單位,需x根據范圍調試
x = np.arange(60, 150,1)
# x數對應的概率密度
y = normfun(x, mean, std)
# 參數,顏色,線寬
plt.plot(x,y, color='g',linewidth = 3)
#數據,數組,顏色,顏色深淺,組寬,顯示頻率
plt.hist(iq, bins =7, color = 'r',alpha=0.5,rwidth= 0.9, normed=True)
plt.title('IQ distribution')
plt.xlabel('IQ score')
plt.ylabel('Probability')
plt.show()
智商正態分布圖
2. 制作賽馬數據圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
stakes_data = pd.read_csv('stakes.csv')
len(stakes_data)
89
stakes = stakes_data['time']
mean = stakes.mean()
mean
149.22101123595513
std = stakes.std()
std
1.6278164717748154
def normfun(x,mu, sigma):
pdf = np.exp(-((x - mu)**2) / (2* sigma**2)) / (sigma * np.sqrt(2*np.pi))
return pdf
stakes.max()
153.19999999999999
stakes.min()
146.0
x = np.arange(145, 155,0.2)
y = normfun(x, mean, std)
plt.plot(x,y,'g',linewidth = 3)
plt.hist(stakes, bins = 6,color = 'b',alpha=0.5, rwidth= 0.9, normed=True)
plt.title('stakes distribution')
plt.xlabel('stakes time')
plt.ylabel('Probability')
plt.show()
賽馬正態分布圖
結論:
1.概率密度函數是圖形中的一條線,而概率則是這條線下方一定數值內的面積。
2.求某一個精確數值的概率為0.因為對應的面積趨近于0
3.直方圖與正態分布圖不完全對應,只有當n充分大,才能更接近于正態分布。
參考資料:
1.使用ipython %matplotlib inline
2.python中求分布函數相關的包
3.python 數學繪圖工具 matplotlib 的優化配置
4.【python】matplotlib.pyplot介紹
5.正態分布