LoginSignup
21
21

More than 5 years have passed since last update.

Pythonで標準偏差と相関係数の計算

Posted at

標準偏差と相関係数をいくつかの方法で計算してみた。

Python 3.5.2rc1 (default, Jun 13 2016, 09:33:26) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [1, 2, 3, 4, 5]
>>> y = [0, 2, 4, 5, 8]

statisticsライブラリを使う。Python-3.4(?)から組み込み。

>>> import statistics
>>> statistics.pstdev(x), statistics.stdev(x)
(1.4142135623730951, 1.5811388300841898)

numpy

>>> import numpy
>>> numpy.std(x), numpy.std(x, ddof=1)
(1.4142135623730951, 1.5811388300841898)

scipyはnumpyと同じ?

>>> import scipy
>>> scipy.std(x), scipy.std(x, ddof=1)
(1.4142135623730951, 1.5811388300841898)

相関係数をnumpyで計算。

>>> numpy.corrcoef(x, y)[0, 1]
0.99044346677110506

次にscipy。

>>> import scipy.stats
>>> scipy.stats.pearsonr(x, y)
(0.99044346677110517, 0.0011198526620164956)

scipyが一番便利かな。

21
21
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
21
21