用Python學分析:集中與分散

散點圖進階,結合箱體圖與直方圖對數據造成全面的認識dom

描述數據集中趨勢的分析量:ide

均值 - 所有數據的算術平均值spa

衆數 - 一組數據中出現次數最多的變量值code

中位數 - 一組數據通過順序排列後處於中間位置上的變量值orm

描述數據離散程度的分析量:blog

方差 - 一組數據各變量值與其平均值離差平方和的平均數rem

標準差 - 方差的平方根get

偏態 - 描述數據分佈形態的統計量,其描述的是某整體取值分佈的對稱性。偏度 = 三階中心距 / 標準差的三次方it

峯度 - 描述整體中全部取值分佈形態陡緩程度的統計量,這個統計量須要與正態分佈相比較。 峯度 = 四階中心距 / 方差平方(標準差四次方) - 3io

 

描述性分析數據的計算:

 1 # 準備數據
 2 import numpy as np
 3 import matplotlib.pyplot as plt
 4 
 5 n = 1000
 6 x = np.random.randn(n)
 7 y = [int((item)*100) for item in np.random.randn( n )] #100之內的正整數隨機數
 8 
 9 # 均值μ
10 mu = np.mean(y)
11 # 標準差δ  sigma = np.sqrt(np.sum(np.square( y - mu ))/n)
12 sigma = np.std(y)
13 # 峯度(公式準確度待確認)
14 kurtosis = np.sum(np.power((y - mu),4))/(n) # 四階中心距
15 kurtosis = kurtosis / np.power(sigma,4)-3 # 峯度 = 四階中心距 / 方差平方(標準差四次方) - 3
16 # 偏度
17 skewness = np.sum(np.power((y - mu),3))/(n) # 三階中心距
18 skewness = skewness / np.power(sigma,3) # 偏度 = 三階中心距 / 標準差的三次方
19 
20 print(mu, sigma,skewness, kurtosis)
View Code

結果:

-0.944 105.50647783 0.0750892544722 -0.171492308767

圖表顯示

 1 # 圖表顯示
 2 fig = plt.figure( figsize = ( 8, 6 )) # 設置圖表大小
 3 #設置圖表的大小:[左, 下, 寬, 高] 規定的矩形區域 (所有是0~1之間的數,表示比例)
 4 rect_1 = [0.15, 0.30, 0.7,  0.55]
 5 rect_2 = [0.85, 0.30, 0.15, 0.55]
 6 rect_3 = [0.15, 0.05, 0.7,  0.2]
 7 fig_1 = plt.axes(rect_1) # 第一個圖表
 8 fig_2 = plt.axes(rect_2) # 第二個圖表
 9 fig_3 = plt.axes(rect_3) # 第三個圖表
10 #設置圖表公共變量
11 title_size = 13
12 inner_color = 'cyan'
13 outer_color = 'teal'
14 # 第一個圖表:散點圖
15 fig_1.scatter( x, y, s = 20, color = inner_color, edgecolor = outer_color, alpha = 0.6)
16 fig_1.set_title('散點圖 Scatter', fontsize = title_size)
17 fig_1.set_ylim( min(y),max(y)+50 )
18 fig_1.grid(True)
19 
20 # 第二個圖表:箱體圖
21 fig_2.boxplot(y, 
22               widths = 0.55,
23               patch_artist = True, # 要求用自定義顏色填充盒形圖,默認白色填充
24               boxprops = {'color':outer_color,'facecolor':inner_color, }, # 設置箱體屬性,填充色和邊框色
25               flierprops = {'marker':'o','markerfacecolor':inner_color,'color':outer_color,}, # 設置異常值屬性,點的形狀、填充色和邊框色
26               meanprops = {'marker':'h','markerfacecolor':outer_color}, # 設置均值點的屬性,點的形狀、填充色 
27               medianprops = {'linestyle':'-','color':'red'} # 設置中位數線的屬性,線的類型和顏色
28              )
29 fig_2.set_ylim( fig_1.get_ylim()) #設置箱體圖與散點圖同一縱座標軸
30 fig_2.get_yaxis().set_visible(False) #關閉座標軸
31 fig_2.get_xaxis().set_visible(False) #關閉座標軸 
32 # 去除邊框顯示
33 remove_col = ['top','bottom','left','right']
34 for item in remove_col:
35     fig_2.spines[item].set_visible(False)
36     fig_2.spines[item].set_position(('data',0)) 
37 fig.text(0.86, 0.84,'箱體圖 Boxplot', fontsize = title_size )
38 
39 # 第三個圖表:直方圖
40 n, bins, patches = fig_3.hist( y, color = inner_color, alpha = 0.8, edgecolor = outer_color )
41 fig_3.set_ylim([0,max(n)+50])
42 fig_3.spines['top'].set_visible(False) # 去除邊框顯示
43 fig_3.spines['top'].set_position(('data',0)) # 去除邊框刻度顯示
44 fig_3.spines['right'].set_color('none') # 去除邊框顯示
45 fig_3.spines['right'].set_position(('data',0)) # 去除邊框刻度顯示
46 fig.text(0.17, 0.23,'直方圖 Hist', fontsize = title_size )
47 
48 # 文本信息
49 fig.text(0.9, .20, '均值 $\mu = {0:.2f}$'.format(mu))
50 fig.text(0.9, .15, '標準差 $\sigma = {0:.2f}$'.format(sigma))
51 fig.text(0.9, .10, '偏度 $\gamma 1 = {0:.2f}$'.format(skewness))
52 fig.text(0.9, .05, '峯度 $\gamma 2 = {0:.2f}$'.format(kurtosis))
53 plt.show()
View Code

結果:

相關文章
相關標籤/搜索