Skip to content

Commit 66afc78

Browse files
committed
Add examples for decision tree models
1 parent 8baf52b commit 66afc78

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
# Created by "Thieu" at 09:17, 10/05/2025 ----------%
3+
# Email: nguyenthieu2102@gmail.com %
4+
# Github: https://github.com/thieu1995 %
5+
# --------------------------------------------------%
6+
7+
from sklearn.tree import DecisionTreeClassifier
8+
from sklearn.datasets import load_iris
9+
from sklearn.model_selection import train_test_split
10+
from metasklearn import MetaSearchCV, IntegerVar, StringVar, FloatVar
11+
12+
# Load dataset
13+
X, y = load_iris(return_X_y=True)
14+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
15+
16+
# Define param bounds
17+
18+
# param_grid = { ==> This is for GridSearchCV, show you how to convert to our MetaSearchCV
19+
# 'criterion': ['gini', 'entropy', 'log_loss'], # Hàm đo mức độ phân tách (classification)
20+
# 'splitter': ['best', 'random'], # Cách chọn cách chia tại mỗi node
21+
# 'max_depth': [None, 5, 10, 20, 30], # Độ sâu tối đa của cây
22+
# 'min_samples_split': [2, 5, 10], # Số lượng mẫu tối thiểu để một node được chia
23+
# 'min_samples_leaf': [1, 2, 4], # Số lượng mẫu tối thiểu tại một node lá
24+
# 'max_features': [None, 'sqrt', 'log2'], # Số đặc trưng tối đa khi tìm split tốt nhất
25+
# 'max_leaf_nodes': [None, 10, 20, 50], # Giới hạn số lượng node lá
26+
# 'ccp_alpha': [0.0, 0.01, 0.05, 0.1], # Complexity parameter để cắt tỉa cây (post-pruning)
27+
# }
28+
29+
param_bounds = [
30+
StringVar(valid_sets=("gini", "entropy", "log_loss"), name="criterion"), # Hàm đo mức độ phân tách (classification)
31+
StringVar(valid_sets=("best", "random"), name="splitter"), # Cách chọn cách chia tại mỗi node
32+
IntegerVar(lb=2, ub=15, name="max_depth"), # Độ sâu tối đa của cây
33+
IntegerVar(lb=2, ub=10, name="min_samples_split"), # Số lượng mẫu tối thiểu để một node được chia
34+
IntegerVar(lb=1, ub=5, name="min_samples_leaf"), # Số lượng mẫu tối thiểu tại một node lá
35+
StringVar(valid_sets=("sqrt", "log2"), name="max_features"), # Số đặc trưng tối đa khi tìm split tốt nhất
36+
IntegerVar(lb=2, ub=30, name="max_leaf_nodes"), # Giới hạn số lượng node lá
37+
FloatVar(lb=0.0, ub=0.1, name="ccp_alpha"), # Complexity parameter để cắt tỉa cây (post-pruning)
38+
]
39+
40+
# Initialize and fit MetaSearchCV
41+
searcher = MetaSearchCV(
42+
estimator=DecisionTreeClassifier(random_state=42),
43+
param_bounds=param_bounds,
44+
task_type="classification",
45+
optim="BaseGA",
46+
optim_params={"epoch": 20, "pop_size": 30, "name": "GA"},
47+
cv=3,
48+
scoring="AS", # or any custom scoring like "F1_macro"
49+
seed=42,
50+
n_jobs=2,
51+
verbose=True
52+
)
53+
54+
searcher.fit(X_train, y_train)
55+
print("Best parameters:", searcher.best_params)
56+
print("Best model: ", searcher.best_estimator)
57+
print("Best score during searching: ", searcher.best_score)
58+
59+
# Make prediction after re-fit
60+
y_pred = searcher.predict(X_test)
61+
print("Test Accuracy:", searcher.score(X_test, y_test))
62+
print("Test Score: ", searcher.scores(X_test, y_test, list_metrics=("AS", "RS", "PS", "F1S")))
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# Created by "Thieu" at 09:12, 10/05/2025 ----------%
3+
# Email: nguyenthieu2102@gmail.com %
4+
# Github: https://github.com/thieu1995 %
5+
# --------------------------------------------------%
6+
7+
from sklearn.tree import DecisionTreeRegressor
8+
from sklearn.datasets import load_diabetes
9+
from metasklearn import MetaSearchCV, IntegerVar, StringVar, FloatVar, Data
10+
11+
## Load data object
12+
X, y = load_diabetes(return_X_y=True)
13+
data = Data(X, y)
14+
15+
## Split train and test
16+
data.split_train_test(test_size=0.2, random_state=42, inplace=True)
17+
print(data.X_train.shape, data.X_test.shape)
18+
19+
## Scaling dataset
20+
data.X_train, scaler_X = data.scale(data.X_train, scaling_methods=("standard", "minmax"))
21+
data.X_test = scaler_X.transform(data.X_test)
22+
23+
data.y_train, scaler_y = data.scale(data.y_train, scaling_methods=("standard", "minmax"))
24+
data.y_train = data.y_train.ravel()
25+
data.y_test = scaler_y.transform(data.y_test.reshape(-1, 1)).ravel()
26+
27+
# Define param bounds
28+
29+
# param_grid = { ==> This is for GridSearchCV, show you how to convert to our MetaSearchCV
30+
# 'criterion': ['squared_error', 'friedman_mse', 'absolute_error', 'poisson'], # (regression)
31+
# 'splitter': ['best', 'random'], # Cách chọn cách chia tại mỗi node
32+
# 'max_depth': [None, 5, 10, 20, 30], # Độ sâu tối đa của cây
33+
# 'min_samples_split': [2, 5, 10], # Số lượng mẫu tối thiểu để một node được chia
34+
# 'min_samples_leaf': [1, 2, 4], # Số lượng mẫu tối thiểu tại một node lá
35+
# 'max_features': [None, 'sqrt', 'log2'], # Số đặc trưng tối đa khi tìm split tốt nhất
36+
# 'max_leaf_nodes': [None, 10, 20, 50], # Giới hạn số lượng node lá
37+
# 'ccp_alpha': [0.0, 0.01, 0.05, 0.1], # Complexity parameter để cắt tỉa cây (post-pruning)
38+
# }
39+
40+
param_bounds = [
41+
StringVar(valid_sets=("squared_error", "friedman_mse", "absolute_error", "poisson"), name="criterion"), # (regression)
42+
StringVar(valid_sets=("best", "random"), name="splitter"), # Cách chọn cách chia tại mỗi node
43+
IntegerVar(lb=2, ub=15, name="max_depth"), # Độ sâu tối đa của cây
44+
IntegerVar(lb=2, ub=10, name="min_samples_split"), # Số lượng mẫu tối thiểu để một node được chia
45+
IntegerVar(lb=1, ub=5, name="min_samples_leaf"), # Số lượng mẫu tối thiểu tại một node lá
46+
StringVar(valid_sets=("sqrt", "log2"), name="max_features"), # Số đặc trưng tối đa khi tìm split tốt nhất
47+
IntegerVar(lb=2, ub=30, name="max_leaf_nodes"), # Giới hạn số lượng node lá
48+
FloatVar(lb=0.0, ub=0.1, name="ccp_alpha"), # Complexity parameter để cắt tỉa cây (post-pruning)
49+
]
50+
51+
# Initialize and fit MetaSearchCV
52+
searcher = MetaSearchCV(
53+
estimator=DecisionTreeRegressor(random_state=42),
54+
param_bounds=param_bounds,
55+
task_type="regression",
56+
optim="BaseGA",
57+
optim_params={"epoch": 20, "pop_size": 30, "name": "GA"},
58+
cv=3,
59+
scoring="MSE", # or any custom scoring like "F1_macro"
60+
seed=42,
61+
n_jobs=2,
62+
verbose=True
63+
)
64+
65+
searcher.fit(data.X_train, data.y_train)
66+
print("Best parameters:", searcher.best_params)
67+
print("Best model: ", searcher.best_estimator)
68+
print("Best score during searching: ", searcher.best_score)
69+
70+
# Make prediction after re-fit
71+
y_pred = searcher.predict(data.X_test)
72+
print("Test R2:", searcher.score(data.X_test, data.y_test))
73+
print("Test Score: ", searcher.scores(data.X_test, data.y_test, list_metrics=("RMSE", "R", "KGE", "NNSE")))

0 commit comments

Comments
 (0)