Cập nhật mã nguồn và file Colab Cache

This commit is contained in:
2026-07-16 19:32:43 +07:00
parent 25969cb0f5
commit a258db54cd
69 changed files with 3377 additions and 645 deletions
+31 -10
View File
@@ -13,6 +13,7 @@ from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
import joblib
from datetime import datetime
import json
@@ -961,18 +962,38 @@ def train_model(
verbosity=0
)
elif model_type == 'random_forest':
model = RandomForestClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
random_state=42,
n_jobs=-1, # Use all cores
verbose=0
)
if use_gpu:
model = XGBClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
tree_method='hist',
device='cuda:0',
random_state=42,
n_jobs=-1,
verbosity=0
)
else:
model = RandomForestClassifier(
n_estimators=n_estimators,
max_depth=max_depth,
random_state=42,
n_jobs=-1, # Use all cores
verbose=0
)
elif model_type == 'decision_tree':
model = DecisionTreeClassifier(
max_depth=max_depth,
random_state=42
)
elif model_type == 'lightgbm':
model = LGBMClassifier(
n_estimators=n_estimators if n_estimators else 300,
max_depth=max_depth if max_depth else -1,
learning_rate=learning_rate,
class_weight='balanced',
random_state=42,
device='gpu' if use_gpu else 'cpu'
)
elif model_type == 'svm':
model = SVC(
kernel='rbf',
@@ -1281,7 +1302,7 @@ def train_model(
class_names = label_encoder.classes_.tolist()
# Classification report as dict
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
cls_report = classification_report(y_test, y_pred, target_names=class_names, output_dict=True, zero_division=0)
# Confusion matrix
@@ -1313,9 +1334,9 @@ def train_model(
"test_accuracy": float(test_score),
"model_type": model_type,
"device": device if model_type == 'xgboost' else 'cpu',
"n_estimators": n_estimators if model_type in ['xgboost', 'random_forest', 'cnn', 'swin-unet', 'mobilenet-lraspp'] else None,
"n_estimators": n_estimators if model_type in ['xgboost', 'random_forest', 'lightgbm', 'cnn', 'swin-unet', 'mobilenet-lraspp'] else None,
"max_depth": max_depth if model_type not in ['cnn', 'swin-unet', 'mobilenet-lraspp'] else None,
"learning_rate": learning_rate if model_type in ['xgboost', 'swin-unet', 'mobilenet-lraspp'] else None,
"learning_rate": learning_rate if model_type in ['xgboost', 'lightgbm', 'swin-unet', 'mobilenet-lraspp'] else None,
"epochs": min(50, n_estimators // 2) if model_type == 'cnn' else (min(60, n_estimators // 2) if model_type in ['swin-unet', 'mobilenet-lraspp'] else None),
"n_features": X_train.shape[1],
"n_classes": len(np.unique(y_train)),