大家好,描述性統(tǒng)計(jì)分析主要是指求一組數(shù)據(jù)的平均值、中位數(shù)、眾數(shù)、極差、方差和標(biāo)準(zhǔn)差等指標(biāo),通過這些指標(biāo)來發(fā)現(xiàn)這組數(shù)據(jù)的分布狀態(tài)、數(shù)字特征等內(nèi)在規(guī)律。在Python中進(jìn)行描述性統(tǒng)計(jì)分析,可以借助Numpy、Pandas、SciPy等科學(xué)計(jì)算模塊計(jì)算出指標(biāo),然后用繪圖模塊Matplotlib繪制出數(shù)據(jù)的分布狀態(tài)和頻率及頻數(shù)直方圖,以更直觀的方式展示數(shù)據(jù)分析的結(jié)果。
一、描述性統(tǒng)計(jì)指標(biāo)計(jì)算
用describe()函數(shù)可以計(jì)算出以下值,代碼如下:
import pandas as pd
data= pd.read_excel('D:/shujufenxi/jjj.xlsx',index_col='序號')
data1=data.describe()
print(data1)

除了此函數(shù)計(jì)算范圍此外,還可以計(jì)算以下值,代碼演示如下:
import pandas as pd
from numpy import mean,median,ptp,var,std
from scipy.stats import mode
data= pd.read_excel('D:/shujufenxi/jjj.xlsx',index_col='序號')
median=median(data['月薪(元)'])# 計(jì)算中位數(shù)
mode= mode(data['月薪(元)'])[0][0]#計(jì)算眾數(shù)
ptp=ptp(data['月薪(元)'])#極差
var=var(data['月薪(元)'])#方差
std=std(data['月薪(元)'])#標(biāo)準(zhǔn)差
print('中位數(shù):'+str(median))
print('眾數(shù):'+str(mode))
print('極差:'+str(ptp))
print('方差:'+str(var))
print('標(biāo)準(zhǔn)差:'+str(std))

二、數(shù)據(jù)的分布
根據(jù)數(shù)據(jù)的分布是否對稱,數(shù)據(jù)的分布狀態(tài)可分為正態(tài)分布與偏態(tài)分布。偏態(tài)分布又分為正偏態(tài)分布與負(fù)偏態(tài)分布;若眾數(shù)<中位數(shù)<均值則為正偏態(tài)分布;若均值<中位數(shù)<眾數(shù),則為負(fù)偏態(tài)分布;由此可見,根據(jù)上面我們所得出的結(jié)果,屬于正偏態(tài)分布。接下來引申兩個(gè)概念:
偏度--是指數(shù)據(jù)分布的偏斜方向和程度的度量,常用于衡量隨機(jī)分布的不均衡性。如果數(shù)據(jù)對稱分布,如標(biāo)準(zhǔn)正態(tài)分布,則偏度為0;如數(shù)據(jù)偏左分布,則偏度<0;如果數(shù)據(jù)右偏分布,則偏度>0
峰度--是用來描述數(shù)據(jù)分布陡峭情況或平滑的情況,可以理解為數(shù)據(jù)分布的高矮程度。我們來繪制標(biāo)準(zhǔn)正態(tài)分布圖和月薪分布圖,我們將Matolotlib模塊和Seaborn模塊結(jié)合使用,代碼如下:
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data= pd.read_excel('D:/shujufenxi/jjj.xlsx')
standard_normal=pd.Series(np.random.normal(0,1,size=1000000))
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
sns.kdeplot(standard_normal,fill=True,label='標(biāo)準(zhǔn)正態(tài)分布')
sns.kdeplot(data['月薪(元)'],label='月薪分布')
plt.show()

三、數(shù)據(jù)的頻數(shù)與頻率分析
頻數(shù)是指數(shù)據(jù)中的類別變量的每種取值出現(xiàn)的次數(shù)。頻率是指每個(gè)類別變量的頻數(shù)與總次數(shù)的比值,通常用百分比表示。
演示如下:
import pandas as pd
data= pd.read_excel('D:/shujufenxi/jjj.xlsx')
frequency=data['月薪(元)'].value_counts()#value_counts()函數(shù)用來計(jì)算數(shù)據(jù)的頻數(shù)
percentage=frequency/len(data['月薪(元)'])# len()函數(shù)用來計(jì)算所選數(shù)據(jù)列的長度
print(frequency.head())
print(percentage.head())

接下來,使用Matplotlib模塊中的hist()函數(shù)繪制頻數(shù)分布直方圖,演示代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-470849.html
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
data= pd.read_excel('D:/shujufenxi/jjj.xlsx')
plt.hist(data['月薪(元)'],bins=8,density=False,color='g',edgecolor='k',alpha=0.75)#bins參數(shù)用于指定繪制直方圖柱子的個(gè)數(shù),density參數(shù)為False時(shí)表示繪制頻數(shù)直方圖,反之則為頻率直方圖,alph用于設(shè)置柱子透明度
plt.xlabel('月薪')
plt.ylabel('頻數(shù)')
plt.title('月薪頻數(shù)分布直方圖')
plt.show()

從直方圖中可以很清晰地看出所實(shí)驗(yàn)數(shù)據(jù)中,月薪在6000-7000元區(qū)間內(nèi)的人數(shù)最多,從之前的分析中也可以看出月薪均值、中位數(shù)、眾數(shù)分別約為7564、6340、4646。大家亦可找實(shí)例進(jìn)行練習(xí)。文章來源地址http://www.zghlxwxcb.cn/news/detail-470849.html
到了這里,關(guān)于使用Python進(jìn)行數(shù)據(jù)分析——描述性統(tǒng)計(jì)分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!