Python 数学、統計、機械学習:基本統計量、分散と標準偏差

1.基本統計量

Python 数学、統計、機械学習:基本統計量、ヒストグラム で使用した、Bostonデータセット の住宅価格をヒストグラムに表示し、各基本統計量を出力してみる。

  1. from sklearn import datasets
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import math
  5.  
  6.  
  7. def var_stdev():
  8. """
  9. https://pythondatascience.plavox.info/matplotlib/%E6%95%A3%E5%B8%83%E5%9B%B3
  10. https://qiita.com/ynakayama/items/8d3b1f7356da5bcbe9bc
  11. https://teratail.com/questions/72586
  12. :return:
  13. """
  14. boston = datasets.load_boston()
  15.  
  16. df = pd.DataFrame(boston.target)
  17. df.columns = ['Price']
  18.  
  19. x = df['Price']
  20. # CSVに出力
  21. # x.to_csv('~/work/house_price.csv')
  22.  
  23. import pprint
  24. print(pprint.pprint(dir(x)))
  25.  
  26. label = """
  27. mean={0:f}
  28. median={1:f}
  29. mode={2:f}
  30. variance={3:f}
  31. standard deviation={4:f}
  32. standard error={5:f}
  33. kurtosis={6:f}
  34. skewness={7:f}
  35. """.format(x.mean(), x.median(), x.mode().iloc[0], x.var(), x.std(),
  36. x.sem(), x.kurtosis(), x.skew())
  37.  
  38. fig = plt.figure()
  39. ax = fig.add_subplot(1,1,1)
  40. ax.set_title('Boston house price')
  41.  
  42. ax.hist(x, bins=int(round(math.sqrt(x.count()), 0)),
  43. rwidth=0.8, label='house price(x $1,000)')
  44.  
  45. ax.text(25, 40, label)
  46.  
  47. ax.legend()
  48. plt.show()
  49.  
  50.  
  51. if __name__ == "__main__":
  52. var_stdev()

var_stdev

基本統計量 対応するDataFrameの関数 概要
平均(mean) mean() 観測値の総和を観測値の個数で割ったもの
中央値(median) median() 有限個のデータを小さい順に並べたとき中央に位置する値
最頻値(mode) mode() データ群や確率分布で最も頻繁に出現する値
分散(variance) var() 平均からのバラツキを示す指標、標準偏差の2乗、0 に近いほど散らばりは小さい
標準偏差(standard deviation) std() 平均からのバラツキを示す指標、分散の正の平方根
標準誤差(standard error) sem()

母集団からある数の標本を選ぶとき、選ぶ組み合わせに依って統計量がどの程度ばらつくかを、全ての組み合わせについての標準偏差で表したもの。

統計量を指定せずに単に「標準誤差」と言った場合、標本平均の標準誤差(standard error of the mean、SEM)のことを普通は指す。

尖度(kurtosis) kurtosis() 正規分布と比べて、尖度が大きければ鋭いピークと長く太い裾を持った分布を持ち、尖度が小さければより丸みがかったピークと短く細い尾を持った分布であるという事が判断できる
歪度(skewness) skew() 分布の非対称性を示す指標

Follow me!

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です