LoginSignup
1
1

More than 5 years have passed since last update.

NumPyに入門 その4 統計情報の計算

Posted at

Excelにあるデータ分析ツールの真似をして、
前回使用したアヤメのデータから統計情報を出力したいと思います。

NumPyに入門 3 CSVから抽出

>>> iris_data[:5]
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2]])

標準誤差

列毎に標準誤差を計算して出力します

>>> import math

>>> iris_data.std(axis = 0) / math.sqrt(iris_data.shape[0])
array([0.06738557, 0.03528462, 0.14358331, 0.06210376])

中央値

中央値を列毎に求めて出力します。

>>> np.median(iris_data, axis = 0)
array([5.8 , 3.  , 4.35, 1.3 ])

分散

分散を列毎に計算して表示します。

>>> iris_data.var(axis = 0)
array([0.68112222, 0.18675067, 3.09242489, 0.57853156])

尖度(Kurtosis)

正規分布に比べて尖っている度合いを計算します。

>>> N = len(iris_data)
>>> a = N * (N + 1) / (N - 1) / (N - 2) / (N - 3)
>>> b = 3 * (N - 1) ** 2 / (N - 2) / (N - 3)
>>> c = (((iris_data - iris_data.mean(axis = 0)) ** 4) / iris_data.std(axis = 0) ** 4).sum(axis = 0)
>>> a * c - b
array([-0.51826916,  0.33592727, -1.37957167, -1.31656779])


歪度(Skewness)

分布の非対称性を表す度合いを計算します。

>>> N = len(iris_data)
>>> a = N / (N - 1) / (N - 2)
>>> b = (((iris_data - iris_data.mean(axis = 0)) / iris_data.std(axis = 0)) ** 3).sum(axis = 0)
>>> a * b
array([ 0.31808651,  0.33742124, -0.27723195, -0.10605535])

信頼区間(95%)

先に標準誤差を求めてから信頼区間を求めます。

>>> ste = iris_data.std(axis = 0) / math.sqrt(iris_data.shape[0])
>>> ste * 1.96
array([0.13207571, 0.06915786, 0.28142328, 0.12172337])
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1