|
| 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