-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexam_gradient_mlp_multi_class_classification.py
More file actions
42 lines (32 loc) · 1.63 KB
/
Copy pathexam_gradient_mlp_multi_class_classification.py
File metadata and controls
42 lines (32 loc) · 1.63 KB
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
#!/usr/bin/env python
# Created by "Thieu" at 16:38, 25/10/2023 ----------%
# Email: nguyenthieu2102@gmail.com %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%
from sklearn.datasets import load_iris
from metaperceptron import Data, MlpClassifier
## Load data object
X, y = load_iris(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, shuffle=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.encode_label(data.y_train)
data.y_test = scaler_y.transform(data.y_test)
print(type(data.X_train), type(data.y_train))
## Create model
model = MlpClassifier(hidden_layers=(30,), act_names="Tanh", dropout_rates=None, act_output=None,
epochs=10, batch_size=16, optim="Adam", optim_params=None,
early_stopping=True, n_patience=10, epsilon=0.001, valid_rate=0.1,
seed=42, verbose=True, device="cpu")
## Train the model
model.fit(X=data.X_train, y=data.y_train)
## Test the model
y_pred = model.predict(data.X_test)
print(y_pred)
print(model.predict_proba(data.X_test))
## Calculate some metrics
print(model.evaluate(y_true=data.y_test, y_pred=y_pred, list_metrics=["F2S", "CKS", "FBS", "PS", "RS", "NPV", "F1S"]))