-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_svr.py
59 lines (43 loc) · 1.51 KB
/
best_svr.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
47
48
49
50
51
52
53
54
55
56
57
58
59
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import r2_score
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def plot_results(X,y, gs):
plt.scatter(y, gs.predict(X), color = 'blue')
plt.plot(y,y, color = 'red')
plt.title('SVR')
plt.xlabel('True Y')
plt.ylabel('Predicted Y')
plt.show()
return(0)
def best_model(X_train, X_test, y_train, y_test, plot_ind, eval_parm):
#return_parm=[]
if eval_parm == 'deep':
parameters = {'C': (0.5, 1., 2.), \
'epsilon': (0.05, 0.1, 0.2, 0.3), \
'kernel': ('linear', 'rbf', 'sigmoid', 'poly'), \
'degree': (2, 3, 4), \
'coef0': (0.05, 0.1), \
'gamma': (0.1, 1, 10) \
}
elif eval_parm == 'test':
parameters = {'C': (1., 2.), \
'epsilon': (0.05, 0.1), \
'kernel': ('linear', 'rbf', 'poly'), \
'degree': (2, 3), \
}
regressor = SVR()
gs = GridSearchCV(regressor, parameters, cv=5)
gs.fit(X_train, \
y_train)
# Predicting the Test set results
y_pred = gs.predict(X_test)
score = r2_score(y_test, y_pred)
return_parm= {'trained_model': gs.best_estimator_, 'score': score}
if plot_ind:
X = np.concatenate((X_train, X_test), axis=0)
y = np.concatenate((y_train, y_test), axis=0)
plot_results(X,y, gs)
return (return_parm)