0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PyMC3がうまくいかない場合(numpyのモジュールがないといわれたときの解決法)

Posted at

概要

ベイズ推論の考え方の説明とそれを行うことのできるPythonライブラリPyMC3を使ってみようとしたらうまいことできなかったため,その対処法を載せる.ちなみにインストール方法は以下に書いてあります
https://docs.pymc.io/

各種バージョン

・OS:windows10
・anaconda環境下
・editor:jupyter notebook

・numpy:1.16.4
・pandas:0.24.2
・pymc3:3.6

後に記述するが

・theano:1.0.3

インストール

インストールは上記に載せたURLの通り

conda install -c conda-forge pymc3

と,anaconda promptを使ってインストールした.(pipとconda混ぜるな危険,windowsユーザーはcondaで入らないときは積極的にconda-forgeを使いましょう)

今回の問題

よーし,これでベイズ推論の検証ができるぞとばかり思い,以下のコードを実行してみた

import numpy as np
import scipy.stats as stats
import pymc3 as pm

import warnings
warnings.simplefilter('ignore')

# 乱数のseed固定
random_state = 0

np.random.seed(random_state)
n_trials = 4
theta_real = 0.35
data = stats.bernoulli.rvs(p=theta_real, size=n_trials)
data

# withブロックの内側すべてが一つのモデル
with pm.Model() as model:
    # モデリング
    theta = pm.Beta('θ', alpha=1., beta=1.) # 事前確率
    y = pm.Bernoulli('y', p=theta, observed=data) # 尤度
    
    # 推論
    trace = pm.sample(1000, random_seed=random_state)

すると,以下のエラーが出てきた.

AttributeError: module 'numpy.core.multiarray' has no attribute '_get_ndarray_c_version'

pm.Model()でエラーが出た.
どうやらnumpyで関連で怒られているらしい…
調べてみると,https://github.com/pymc-devs/pymc3/issues/3340 ここで言われているようにいろんな人に起きてるみたい.読んでみると,theanoというパッケージを1.0.3から1.0.4にあげないとダメとのことで,やってみた.

conda install theano==1.0.4

これでもダメそう…

結局,numpyを入れ直し,pymc3を3.6から3.7に無理やりあげるとうまくいった.また,pandasやnumpyのバージョンはこれでないといろんなところでエラーが出たので,けっこうデリケートな設計だなPyMC3って感じでした.

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?