In [1]:
1 + 1 
Out[1]:
2
In [2]:
3 - 4
Out[2]:
-1
In [3]:
5 * 2
Out[3]:
10
In [4]:
6 / 3
Out[4]:
2.0
In [5]:
4 ** 2 
Out[5]:
16
In [6]:
import numpy as np
np.sqrt(16)
Out[6]:
4.0
In [7]:
# 参考
import numpy
numpy.sqrt(16)
Out[7]:
4.0
In [8]:
x = 1
In [9]:
x + 1
Out[9]:
2
In [10]:
x
Out[10]:
1
In [11]:
a
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-60b725f10c9c> in <module>()
----> 1 a

NameError: name 'a' is not defined

Markdownを選択すると、説明を記載することができます。

シャープ1つの見出し

シャープ2つの見出し

シャープ3つの見出し

In [12]:
# コメントです 
# 1 + 1
In [13]:
1 + 1
Out[13]:
2
In [14]:
"""複数行に
わたる
コメントです
1 + 1"""
Out[14]:
'複数行に\nわたる\nコメントです\n1 + 1'

CSVファイルからデータを読み込んで、一般化線形モデルを推定するデモ

In [15]:
# データを読み込むのに必要なライブラリ
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
In [16]:
# arrayを作る(numpyのimportが必須です)
sample_array = np.array([1,2,3,4,5])
sample_array
Out[16]:
array([1, 2, 3, 4, 5])
In [17]:
# DataFrameにする(pandasのimportが必須です)
pd.DataFrame({'col1':sample_array})
Out[17]:
col1
0 1
1 2
2 3
3 4
4 5
In [18]:
# 複数列のDataFrameにする
pd.DataFrame({'col1':sample_array, 'col2':np.array([6,7,8,9,0])})
Out[18]:
col1 col2
0 1 6
1 2 7
2 3 8
3 4 9
4 5 0
In [19]:
# CSVファイルの読み込み
data = pd.read_csv("poissonData.csv")
In [20]:
# えさの量と猫の数を調べた架空の調査データです
data
Out[20]:
neko esa
0 4 1
1 10 2
2 7 3
3 14 4
In [21]:
# クリップボードからも読み込めます
data2 = pd.read_clipboard()
data2
Out[21]:
neko esa
0 4 1
1 10 2
2 7 3
3 14 4
In [22]:
# 統計モデルを推定するライブラリ
import statsmodels.api as sm
import statsmodels.formula.api as smf 

# グラフを描画するライブラリ
from matplotlib import pyplot as plt
import seaborn as sns
# グラフをjupyter Notebook内に表示させるための指定
%matplotlib inline
In [23]:
# 散布図を書く
# fit_regをtrueにすると、回帰直線が引かれてしまうので注意
# 散布図の引き方はたくさんあるが、これが一番情報量が少なくてシンプルだと思います
sns.lmplot(x="esa", y="neko", data=data,fit_reg=False)
Out[23]:
<seaborn.axisgrid.FacetGrid at 0x246427798d0>
In [24]:
# ポアソン回帰のモデルの設定
glm_pois = smf.glm(formula="neko ~ esa", data=data, family=sm.families.Poisson())

# fitという関数を実行すると、モデルが推定される
result_pois = glm_pois.fit()

# 結果の表示
result_pois.summary()
Out[24]:
Generalized Linear Model Regression Results
Dep. Variable: neko No. Observations: 4
Model: GLM Df Residuals: 2
Model Family: Poisson Df Model: 1
Link Function: log Scale: 1.0
Method: IRLS Log-Likelihood: -8.9701
Date: Sun, 09 Apr 2017 Deviance: 2.2210
Time: 12:51:48 Pearson chi2: 2.27
No. Iterations: 7
coef std err z P>|z| [95.0% Conf. Int.]
Intercept 1.3138 0.485 2.707 0.007 0.363 2.265
esa 0.3173 0.158 2.013 0.044 0.008 0.626
In [25]:
# 予測用の説明変数を作る
esa_plot = np.arange(0, 5, 0.1)
# 結果の表示
esa_plot
Out[25]:
array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
        1.1,  1.2,  1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9,  2. ,  2.1,
        2.2,  2.3,  2.4,  2.5,  2.6,  2.7,  2.8,  2.9,  3. ,  3.1,  3.2,
        3.3,  3.4,  3.5,  3.6,  3.7,  3.8,  3.9,  4. ,  4.1,  4.2,  4.3,
        4.4,  4.5,  4.6,  4.7,  4.8,  4.9])
In [26]:
# DataFrameの形式にする
pd.DataFrame({'esa':esa_plot})
Out[26]:
esa
0 0.0
1 0.1
2 0.2
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1.0
11 1.1
12 1.2
13 1.3
14 1.4
15 1.5
16 1.6
17 1.7
18 1.8
19 1.9
20 2.0
21 2.1
22 2.2
23 2.3
24 2.4
25 2.5
26 2.6
27 2.7
28 2.8
29 2.9
30 3.0
31 3.1
32 3.2
33 3.3
34 3.4
35 3.5
36 3.6
37 3.7
38 3.8
39 3.9
40 4.0
41 4.1
42 4.2
43 4.3
44 4.4
45 4.5
46 4.6
47 4.7
48 4.8
49 4.9
In [27]:
# 予測する
pred = result_pois.predict(exog = pd.DataFrame({'esa':esa_plot}))
# 結果の表示
pred
Out[27]:
array([  3.72025716,   3.84018852,   3.96398616,   4.09177471,
         4.22368283,   4.35984332,   4.50039327,   4.64547418,
         4.79523212,   4.94981786,   5.10938705,   5.27410033,
         5.44412353,   5.61962784,   5.80078995,   5.98779225,
         6.18082301,   6.38007658,   6.58575357,   6.79806105,
         7.01721276,   7.24342935,   7.47693857,   7.71797552,
         7.96678287,   8.22361112,   8.48871884,   8.76237294,
         9.04484893,   9.33643121,   9.63741334,   9.94809834,
        10.26879902,  10.59983825,  10.94154932,  11.29427626,
        11.6583742 ,  12.0342097 ,  12.42216115,  12.82261914,
        13.23598686,  13.66268046,  14.10312956,  14.55777758,
        15.02708226,  15.5115161 ,  16.01156682,  16.52773787,
        17.06054892,  17.61053641])
In [28]:
# 予測結果を図示する
sns.lmplot(x="esa", y="neko", data=data,fit_reg=False)
plt.plot(esa_plot, pred)
Out[28]:
[<matplotlib.lines.Line2D at 0x2464405f278>]
In [ ]: