ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

分散・標準偏差

統計学3級取るぞ!!!

親の顔より何度もみた分散と標準偏差

$ python
>>> a = [5, 3, 4, 10, 3]
>>> a_ = sum(a) / len(a)  # 平均
>>> a_
5.0
>>> s = sum([(i - a_)**2 for i in a]) / len(a)  # 分散
>>> s
6.8
>>> import math
>>> math.sqrt(s)  # 分散をルートでかけると標準偏差
2.6076809620810595

www.youtube.com

もう1つの方法でとく

>>> x2_ = sum([i ** 2 for i in a])  / len(a)  # xの2乗の平均
>>> x2_
31.8
>>> x_2 = (sum([i for i in a]) / len(a))**2  # xの平均の2乗
>>> x_2
25.0
>>> x2_ - x_2
6.800000000000001

小数点...

www.youtube.com

ちなみにPythonの場合、正確に計算したい場合はDecimalを利用しましょ.

>>> from decimal import Decimal
>>> x2_ = Decimal(sum([i ** 2 for i in a]))  / Decimal(len(a))
>>> x2
Decimal('31.8')

>>> x_2 = (Decimal(sum([i for i in a])) / Decimal(len(a)))**2
>>> x_2
Decimal('25')

>>> x2_ - x_2
Decimal('6.8')
>>> float(x2_ - x_2)
6.8

distutils.errors.CompileError: command 'gcc' failed with exit status 1

環境はこちら

  • Ubuntu 16.04
  • Anaconda3をインストールしたあと
$ pip install -e .
Obtaining file:///home/ubuntu/workspace/my-module
Requirement already satisfied: numpy in /home/ubuntu/anaconda3/lib/python3.6/site-packages (from my_module==0.0.1) (1.14.3)
Requirement already satisfied: pandas in /home/ubuntu/anaconda3/lib/python3.6/site-packages (from my_module==0.0.1) (0.23.0)
Requirement already satisfied: scipy in /home/ubuntu/anaconda3/lib/python3.6/site-packages (from my_module==0.0.1) (1.1.0)
Collecting pyproj (from my_module==0.0.1)
  Downloading https://files.pythonhosted.org/packages/29/72/5c1888c4948a0c7b736d10e0f0f69966e7c0874a660222ed0a2c2c6daa9f/pyproj-1.9.5.1.tar.gz (4.4MB)
    100% |████████████████████████████████| 4.4MB 9.7MB/s
    Complete output from command python setup.py egg_info:
    using bundled proj4..
    Traceback (most recent call last):
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/unixccompiler.py", line 118, in _compile
        extra_postargs)
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/ccompiler.py", line 909, in spawn
        spawn(cmd, dry_run=self.dry_run)
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/spawn.py", line 36, in spawn
        _spawn_posix(cmd, search_path, dry_run=dry_run)
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/spawn.py", line 159, in _spawn_posix
        % (cmd, exit_status))
    distutils.errors.DistutilsExecError: command 'gcc' failed with exit status 1

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-l31v9gll/pyproj/setup.py", line 72, in <module>
        objects = cc.compile(['nad2bin.c', 'src/pj_malloc.c'])
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/ccompiler.py", line 574, in compile
        self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
      File "/home/ubuntu/anaconda3/lib/python3.6/distutils/unixccompiler.py", line 120, in _compile
        raise CompileError(msg)
    distutils.errors.CompileError: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-l31v9gll/pyproj/

つらい

自分でモジュール作成したけど、pyprojというものが入らずつらい

github.com

こちら参考にした。

$ sudo apt update
$ sudo apt install libffi-dev g++ libssl-dev

取り急ぎはおk。

が、もう一度pip installを実行すると

$ pip install -e .

# ...
distributed 1.21.8 requires msgpack, which is not installed.
# ...

???

stackoverflow.com

ふむ?

$ pip install msgpack

これを実行したらエラーが消えた。

pre-existing shared memory block (key 5432001, ID 65536) is still in use

postgresqlが起動しないのでぐぐった

2018-06-13 18:35:21.338 JST [8583] FATAL:  pre-existing shared memory block (key 5432001, ID 65536) is still in use
2018-06-13 18:35:21.338 JST [8583] HINT:  If you're sure there are no old server processes still running, remove the shared memory block or just delete the file "postmaster.pid".

akd.air-nifty.com

なるほど。が、postmaster.pidがどこに存在しているかわからなかったので、findで探す

$ sudo find / -name postmaster.pid
Password:

# ...

/usr/local/var/postgres/postmaster.pid

見つけた。削除する

$ rm -rf /usr/local/var/postgres/postmaster.pid

# 起動する
$ pg_ctl -D /usr/local/var/postgres -l postgres.log start

起動した。わーい

「緑本」勉強中. 5章までおわった

ねむい。あんまり頭入っていないと思うけど、雑な振り返り

  • Pythonで書いているけど、GLMが実装できなくてあとでやりたい(簡単にできるのかね
  • 2章はポアソン分布の性質みたいなものを学んだ
  • 3章はポアソン回帰で、パラメータの推定と推定したパラメータで用意したモデルで予測まで学ぶ
    • これまでscikit-learnでなんとなーく叩いていた中身を学んだ気持ち
  • 4章はAICの意味を理解
    • これまで仕事でなんとなーく使っていたAICがやっと理解
    • しかし、なんかまだ腑に落ちていない気はする
  • 5章は帰無仮説と対立仮説の検定?を学ぶ
    • これまではココらへん意味不明でよくわからんかったからスルーしてた
    • とりあえず飲み込むことはできた。わかりやすいとは思う
    • が、結局のところ使い所がまだまだ見えないので、ちゃんと理解していないのかなと思った

後半も引き続きやるんだけど、前半やった雑感としてはこれからデータをいっぱいみていかないといけないのではないのかなという気持ち。統計学覚えてもビジネス(ドメイン)知識ないと終わる気が