svm

# preparation of SVR fitting

# x= x.reshape(x.size, 1)

# xs = xs.reshape(xs.size, 1)

# Do SVR fitting.

model = svm.SVR(kernel='rbf', C=1e3, gamma=0.6)

fitted = model.fit(x,ytrue)

ypredic=fitted.predict(x)

# グラフ化

plt.plot(ytrue, ypredic,'o')

plt.xlabel('ytrue')

plt.ylabel('ypredic')

plt.legend()

plt.show()

# xs= np.linspace(0, 1, 500)

# ytrue = np.sin(2*np.pi * xs) # input target shape must be (number of time, )

support vector machine (SVM, サポート・ベクトル・マシン)は機械学習の一方法.分類と回帰(多変数入力の一変数出力が可能)に使われれる.回帰はsupport vector regression (SVR)と呼ばれ,sklean.svm.svrで利用可能.SVRは風力発電予測などで使われている.

# 2入力変数でのSV回帰のスクリプト例.

import numpy as np

import matplotlib.pyplot as plt

from sklearn import svm

np.random.seed(0)

x1 = np.linspace(0, 1, 10)

x2 = np.linspace(0, 1, 10)

ytrue = np.sin(2 * np.pi * x1) + np.cos(4 * np.pi*x2)

ysampl = ytrue + np.random.normal(0, 0.2, x1.size)

x=np.array([[x1],[x2]])

x=x.reshape(2,10)

x=np.transpose(x) # input shape must be (number of time, number of variables)

support vector machine (SVM) is a method of marine learning. SVM is used for classification and regression (multiple inputs and one output are possible, no multiple outputs (2017/08/29)). Regression is called as support vector regression (SVR), and can be used in klean.svm.svr.SVR is used for wind-power generation prediction.

# An example of SVR with two variable inputs.

import numpy as np

import matplotlib.pyplot as plt

from sklearn import svm

np.random.seed(0)

x1 = np.linspace(0, 1, 10)

x2 = np.linspace(0, 1, 10)

ytrue = np.sin(2 * np.pi * x1) + np.cos(4 * np.pi*x2)

ysampl = ytrue + np.random.normal(0, 0.2, x1.size)

x=np.array([[x1],[x2]])

x=x.reshape(2,10)

x=np.transpose(x) # input shape must be (number of time, number of variables)

# xs= np.linspace(0, 1, 500)

# ytrue = np.sin(2*np.pi * xs) # input target shape must be (number of time, )

# preparation of SVR fitting

# x= x.reshape(x.size, 1)

# xs = xs.reshape(xs.size, 1)

# Do SVR fitting.

model = svm.SVR(kernel='rbf', C=1e3, gamma=0.6)

fitted = model.fit(x,ytrue)

ypredic=fitted.predict(x)

# グラフ化

plt.plot(ytrue, ypredic,'o')

plt.xlabel('ytrue')

plt.ylabel('ypredic')

plt.legend()

plt.show()