0
2

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.

素人がガウス過程回帰を勉強する(1)

Last updated at Posted at 2019-07-13

素人がガウス過程回帰を勉強する(1)

次のページを見てもよくわからないので、少しずつ勉強してます。
でも余りにも素人なので、gpyの使い方の前にpandas、numpyを勉強した方が良いと感じた。

スクリプトは、このHPを参考にわからないところをちょっと追加しています。

# モジュールのインポート
import GPy
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Gpy の設定 RBFカーネルでinput_dim では次元を必ず設定する。
# variance は分散。
# lengthscaleはまだよくわからない.
# この数値を大きくすると線が結果のy_simの変化が小さくなる。
# 小さいとy-simの変化が大きい(苦笑)

kernel = GPy.kern.RBF(input_dim=1, variance=1, lengthscale=0.2)

# ここで、x_simに-1から1までの乱数を100個作っている

np.random.seed(seed=123)
N_sim = 100
x_sim = np.linspace(-1, 1, N_sim)

# これはnumpyをリスト(ベクトル)に直している?
# 元のHPの文章:GPyの関数の多くは、引数のshapeが(データ点の数, 1)である必要があります。そこで[:, None]を加えてその形にしています。
# x_sim.T(転置)でも同じ結果になると思うが、違うのか?

x_sim = x_sim[:, None]

# muに100個の0を作成。numpyで。
mu = np.zeros(N_sim)

# Gpyで共分散行列を作成
# 元のHPの文章:kernelオブジェクトに対しK関数を使うと分散共分散行列を作成できます。

cov = kernel.K(x_sim, x_sim)

# np.random.multivariate_normal()関数は,
# 指定された平均(mu)と共分散行列(cov)から、ランダム(sizeの個数)に作成する関数

y_sim = np.random.multivariate_normal(mu, cov, size=20)

# グラフ作成
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
for i in range(20):
    ax.plot(x_sim[:], y_sim[i,:])
fig.savefig('output/fig1.png')

とりあえず最初のスクリプトは、理解できました。
わからないのは、lengthscaleとガウス過程回帰の関係。

lengthscale = 0.2
image.png

lengthscale = 0.5
image.png

lengthscale = 1.0
image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?