lesson1-繪制直方圖-體重

數據集1 weight(4.4)

代碼:

#Homework1 繪制直方圖-數據 import numpy from pandas import read_table import matplotlib import matplotlib.pyplot as pet

#設置字體 font={'family':'SimHei'} matplotlib.rc('font',**font)

#讀取文件 weight_data = read_table('weight.txt',encoding='UTF-8')

#繪制直方圖 plt.hist(weight_data['weight'],5)

#設置橫縱軸標簽 plt.xlabel('體重(公斤)') plt.ylabel('頻數') plt.title('體重直方圖') plt.show()

#調整bins組數為8 plt.hist(weight_data['weight'],10,rwidth=0.7) #rwidth設置柱與柱之間的間距
#調整bins組數為10 plt.hist(weight_data['weight'],8,rwidth=0.7)

直方圖:
![ ![體重直方圖.png](http://upload-images.jianshu.io/upload_images/5295621-05130372b093fbd9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)](http://upload-images.jianshu.io/upload_images/5295621-208fc4c3dd587fb1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#基本統計 weight_data.describe()
Out[3]: weight count 80.000000 mean 50.700000 std 6.267053 min 38.000000 25% 47.000000 50% 50.000000 75% 54.000000 max 69.000000
#小數點保留2位 weight_data.describe().round(2)
Out[4]: weight count 80.00 mean 50.70 std 6.27 min 38.00 25% 47.00 50% 50.00 75% 54.00 max 69.00

觀察結果:
  1. 統計量
    size: 共計80個數據;
    平均體重:50.7kg; 最輕體重為38kg, 最重體重為69kg;
    標準差:6.27(體重數據的波動程度。
    +-平均值1個std【44.43-56.97kg】正常數據;
    +-2std【<38.16或>63.24】特殊數據)
    第一四分位數 (Q1,又稱“較小四分位數”,該樣本中所有數值由小到大排列后第25%的數字):47kg
    第二四分位數 (Q2,又稱“中位數”,該樣本中所有數值由小到大排列后第50%的數字):50kg
    第三四分位數 (Q3,又稱“較大四分位數”,該樣本中所有數值由小到大排列后第75%的數字):69kg

  2. 直方圖
    以5組為例:
    分組區間[ 38. , 44.2, 50.4, 56.6, 62.8, 69. ]
    對應頻數[ 9., 38., 19., 10., 4.]

體重在44.2-50.4kg之間的人數最多;
直方圖向左側傾斜,which means(比平均值重的人多于輕的人??)

3.自動分組的bins不整齊,似乎不符合常見的統計分組方法,嘗試人為分組(人為定義bins)

#人為設置bins bins = [min(weight_data.weight)-1,40,45,50,55,60,65,max(weight_data['weight'])+1] plt.hist(weight_data['weight'],bins)

bin中50換成50.7試一下(50-55de柱形高度降了一半,也就是說50-50.7的人占了50-55的人的一半左右吧,其實9個23-14)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容