Optunaを使ったSVMの設定方法

##Optunaを使ったSVR（サポートベクター回帰）の設定方法

`SVM`の回帰`SVR`について設定してみる。
設定はメインの、`gamma`,`C`,`epsilon`を設定した。`kernel`も設定を忘れないようにコメントとして、置いておいた。`kernel`込みの計算も可能だが、`poly`多項式が入るととたんに計算が遅くなったので、とりあえず`rbf`（放射基底関数）のみで、計算しておいた方が良いと思います。

パラメータの設定

```
def objective(trial):
    #kernel = trial.suggest_categorical('kernel', ['linear','rbf','poly','sigmoid','precomputed'])
    gamma = trial.suggest_loguniform('gamma',1e-5,1e5)
    C = trial.suggest_loguniform('C',1e-5,1e5)
    epsilon = trial.suggest_loguniform('epsilon',1e-5,1e5)

    #カーネルを選択した場合は、上を使う
    #regr = SVR(kernel = kernel, gamma = gamma, C = C ,epsilon = epsilon)
    regr = SVR(kernel = 'rbf', gamma = gamma, C = C ,epsilon = epsilon)
    score = cross_val_score(regr, X_train_std, y_train, cv=3, scoring="r2")

    r2_mean = score.mean()
    print(r2_mean)

    return r2_mean
```

Optunaでの学習

```
# optunaのstudy
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100)
 
# チューニングしたハイパーパラメーターをフィット

optimised_regr = SVR(kernel = 'rbf' , gamma = study.best_params['gamma'],
                   C = study.best_params['C'],epsilon = study.best_params['epsilon'])
'''
#kernelを選択した場合は、こちらを使う
optimised_regr = SVR(kernel = study.best_params['kernel'] , gamma = study.best_params['gamma'],
                   C = study.best_params['C'],epsilon = study.best_params['epsilon'])
'''
optimised_regr.fit(X_train_std ,y_train)

```
結果はこんな感じで良い感じでｆｉｔできました。

![svm_Figure_1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/336017/95a9aedf-eea1-a52a-2efb-478814db0fb5.png)




プログラムの全文です

```
# -*- coding: utf-8 -*-

 
from sklearn import datasets
from sklearn.svm import SVR
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
import pandas as pd
import optuna
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.preprocessing import StandardScaler
 
 
boston = datasets.load_boston()
 
print(boston['feature_names'])
 
X = boston['data']
y = boston['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8)

scaler=StandardScaler()
scaler.fit(X_train)
X_train_std=scaler.transform(X_train)
X_test_std=scaler.transform(X_test)

# 目的関数
def objective(trial):
    #kernel = trial.suggest_categorical('kernel', ['linear','rbf','poly','sigmoid','precomputed'])
    gamma = trial.suggest_loguniform('gamma',1e-5,1e5)
    C = trial.suggest_loguniform('C',1e-5,1e5)
    epsilon = trial.suggest_loguniform('epsilon',1e-5,1e5)
 
    #regr = SVR(kernel = kernel, gamma = gamma, C = C ,epsilon = epsilon)
 
    regr = SVR(kernel = 'rbf', gamma = gamma, C = C ,epsilon = epsilon)
 
    score = cross_val_score(regr, X_train_std, y_train, cv=3, scoring="r2")


    r2_mean = score.mean()
    print(r2_mean)
 
    return r2_mean

'''
 kernel='rbf',
 degree=3, gamma='scale', coef0=0.0, tol=0.001, C=1.0, epsilon=0.1, shrinking=True, cache_size=200, verbose=False, max_iter=-1



'''

 
# optuna
study = optuna.create_study(direction='maximize')
study.optimize(objective, n_trials=100)
 
# チューニングしたハイパーパラメーターをフィット

optimised_regr = SVR(kernel = 'rbf' , gamma = study.best_params['gamma'],
                   C = study.best_params['C'],epsilon = study.best_params['epsilon'])
'''
optimised_regr = SVR(kernel = study.best_params['kernel'] , gamma = study.best_params['gamma'],
                   C = study.best_params['C'],epsilon = study.best_params['epsilon'])
'''
optimised_regr.fit(X_train_std ,y_train)


#結果の表示
print("訓練データにフィット")
print("訓練データの精度 =", optimised_regr.score(X_train_std, y_train))
pre_train = optimised_regr.predict(X_train_std)
print("テストデータにフィット")
print("テストデータの精度 =", optimised_regr.score(X_test_std, y_test))
pre_test = optimised_regr.predict(X_test_std)
 
#グラフの表示
 
plt.scatter(y_train, pre_train, marker='o', cmap = "Blue", label="train")
plt.scatter(y_test ,pre_test, marker='o', cmap= "Red", label="test")
plt.title('boston')
plt.xlabel('measurment')
plt.ylabel('predict')
#ここでテキストは微調整する
x = 30  
y1 = 12
y2 = 10
s1 =  "train_r2 =" + str(optimised_regr.score(X_train_std, y_train))
s2 =  "test_r2 =" + str(optimised_regr.score(X_test_std, y_test))
plt.text(x, y1, s1)
plt.text(x, y2, s2)
 
plt.legend(loc="upper left", fontsize=14)
plt.show()




```