-
Notifications
You must be signed in to change notification settings - Fork 2
/
exam_dfo_cfn_regression.py
43 lines (33 loc) · 1.66 KB
/
exam_dfo_cfn_regression.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
#!/usr/bin/env python
# Created by "Thieu" at 21:35, 02/11/2023 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
import numpy as np
from deforce import Data, DfoCfnRegressor
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)
print(data.X_train.shape, data.X_test.shape)
## Scaling dataset
data.X_train, scaler_X = data.scale(data.X_train, scaling_methods=("standard"))
data.X_test = scaler_X.transform(data.X_test)
data.y_train, scaler_y = data.scale(data.y_train, scaling_methods=("minmax", ))
data.y_test = scaler_y.transform(np.reshape(data.y_test, (-1, 1)))
## Create model
opt_paras = {"name": "WOA", "epoch": 250, "pop_size": 30}
model = DfoCfnRegressor(hidden_size=10, act1_name="tanh", act2_name="sigmoid",
obj_name="MSE", optimizer="BaseGA", optimizer_paras=None, verbose=True, seed=42)
## Train the model
model.fit(data.X_train, data.y_train)
## Test the model
y_pred = model.predict(data.X_test)
print(y_pred)
## Calculate some metrics
print(model.score(X=data.X_test, y=data.y_test, method="RMSE"))
print(model.scores(X=data.X_test, y=data.y_test, list_methods=["R2", "NSE", "MAPE"]))
print(model.evaluate(y_true=data.y_test, y_pred=y_pred, list_metrics=["R2", "NSE", "MAPE", "NNSE"]))