-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexam_gd_cfn_regression_gridsearch.py
65 lines (51 loc) · 2.37 KB
/
exam_gd_cfn_regression_gridsearch.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
60
61
62
63
64
65
#!/usr/bin/env python
# Created by "Thieu" at 09:18, 17/09/2023 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from deforce import Data, CfnRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_diabetes
## Load data object
# total samples = 442, total features = 10
X, y = load_diabetes(return_X_y=True)
data = Data(X, y)
## Split train and test
data.split_train_test(test_size=0.2, random_state=2, inplace=True)
print(data.X_train.shape, data.X_test.shape)
## Scaling dataset
data.X_train, scaler_X = data.scale(data.X_train, scaling_methods=("standard", "minmax"))
data.X_test = scaler_X.transform(data.X_test)
data.y_train, scaler_y = data.scale(data.y_train, scaling_methods=("standard", "minmax"))
data.y_test = scaler_y.transform(np.reshape(data.y_test, (-1, 1)))
## Set up parameters for CFN
params = {
"hidden_size": [15, 20, 25],
"act1_name": ["elu", "tanh"], # ['relu', 'sigmoid', 'hardsigmoid', 'tanh', 'elu', 'celu', 'selu', 'glu', 'gelu'],
"act2_name": ["elu", "tanh"], # ['relu', 'sigmoid', 'hardsigmoid', 'tanh', 'elu', 'celu', 'selu', 'glu', 'gelu'],
"obj_name": ["MAE", "MSE"],
"max_epochs": [10, 20],
"batch_size": [16],
"optimizer": ["Adam"], # ["Adadelta", "Adam", "Adamax", "RMSprop", "SGD"],
"optimizer_paras": [
{"lr": 0.01,},
{"lr": 0.02,},
]
}
## Define the model
model = CfnRegressor(verbose=False, seed=42)
## Define the gridsearch object
gs = GridSearchCV(model, params, refit=True, cv=3, verbose=2)
## Train the gridsearch
gs.fit(data.X_train, data.y_train)
## Get the best score and best parameter
print(gs.best_score_, gs.best_params_)
# Get the model with the best parameters
best_model = gs.best_estimator_
## Get the prediction of the best model
y_pred = best_model.predict(data.X_test)
## Calculate some metrics
print(best_model.score(X=data.X_test, y=data.y_test, method="RMSE"))
print(best_model.scores(X=data.X_test, y=data.y_test, list_methods=["R2", "NSE", "MAPE"]))
print(best_model.evaluate(y_true=data.y_test, y_pred=y_pred, list_metrics=["R2", "NSE", "MAPE", "NNSE"]))