-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernelSelection.py
46 lines (41 loc) · 1.58 KB
/
kernelSelection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold, cross_val_score
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ExpSineSquared, RationalQuadratic, DotProduct, RBF
## Load data
var = 6 # selects either max frequency or middle frequency as the output
df = pd.read_csv('./data/optimization.csv')
X = df.iloc[:, 0:6].to_numpy()
y = df.iloc[:, var].to_numpy()
n = len(df.index)
print(n)
## Prepare models
models = []
#models.append(( 'expsinesquared', GaussianProcessRegressor(kernel=ExpSineSquared()) ))
models.append(( 'rationalquadratic', GaussianProcessRegressor(kernel=RationalQuadratic()) ))
#models.append(( 'dotproduct', GaussianProcessRegressor(kernel=DotProduct()) ))
models.append(( 'rbf', GaussianProcessRegressor(kernel=RBF(), n_restarts_optimizer=10) ))
## Evaluate each model
results = []
names = []
for name, model in models:
kfold = KFold(n_splits=10)
cv_results = cross_val_score(model, X, y, cv=kfold)
results.append(cv_results)
names.append(name)
output = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(output)
# Boxplot model comparison
fig = plt.figure()
fig.suptitle('Kernel comparison')
ax = fig.add_subplot(111)
colors = {'patch_artist': True,
'boxprops': dict(color='blue', facecolor='white'),
'capprops': dict(color='blue'),
'medianprops': dict(color='red'),
'whiskerprops': dict(color='blue')}
plt.boxplot(results, **colors)
ax.set_xticklabels(names)
plt.show()