Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

PRML実装 第3章 線形基底関数モデル

More than 5 years have passed since last update.

PRML実装 第3章 線形基底関数モデル

最尤推定解を求めるスクリプトを書いてみました。

script.py
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt

M = 10 # モデルの複雑さ
N = 20 # データ数
L = 10 # データセット数

ary_mu = np.linspace(0,1,M)

def phi(ary_mu, x):
    ret = np.array([np.exp(-(x-mu)**2/(2*0.1**2)) for mu in ary_mu])
    ret[0] = 1
    return ret

for x in range(L):
    x_train = np.linspace(0,1,N)
    y_train = np.sin(2*np.pi*x_train) + np.random.normal(0,0.3,N)

    # 計画行列(3.16式)の計算
    PHI = np.array([phi(ary_mu,x) for x in x_train]).reshape(N,M)

    # 最尤推定解(3.15式)の計算
    w_ml = inv(PHI.transpose().dot(PHI)).dot(PHI.transpose()).dot(y_train)
    # 正則化する場合は下を使う
    # l = 0.3
    # w_ml = inv(l*np.identity(M)+PHI.transpose().dot(PHI)).dot(PHI.transpose()).dot(y_train)

    xx = np.linspace(0,1,100)
    y = [phi(ary_mu,x).dot(w_ml) for x in xx]

    plt.plot(xx,y)

plt.show()
 sin(2*\pi*x) + N(0,0.3)

でデータ生成を行い、データセット毎の結果を表示してみました。

正則化を行わない場合
normal.png

正則化を行った場合
正則化.png

です。なんとなく正則化の効果が出ています。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away