LGBMClassifier ajout des corrélations / prévisions dérivées

This commit is contained in:
Jérôme Delacotte
2025-11-11 11:43:49 +01:00
parent acaf1c9a7b
commit 3b3cf5976a

View File

@@ -63,6 +63,7 @@ from sklearn.model_selection import GridSearchCV
from sklearn.feature_selection import VarianceThreshold from sklearn.feature_selection import VarianceThreshold
import seaborn as sns import seaborn as sns
from lightgbm import LGBMRegressor from lightgbm import LGBMRegressor
import lightgbm as lgb
# Couleurs ANSI de base # Couleurs ANSI de base
RED = "\033[31m" RED = "\033[31m"
@@ -100,7 +101,8 @@ class Zeus_LGBMRegressor(IStrategy):
# 'rsi_1h', 'rsi_deriv1_1h', 'rsi_deriv2_1h', "max_rsi_12_1h", # 'rsi_1h', 'rsi_deriv1_1h', 'rsi_deriv2_1h', "max_rsi_12_1h",
# ] # ]
model_indicators = ["ms-10", "ms-5", "ms-2", "ms-1", "ms-0"] model = None
model_indicators = ["ms-10", "ms-5", "ms-4", "ms-3", "ms-2", "ms-1", "ms-0"]
# model_indicators = ['open', 'high', 'close', 'haclose', 'percent', 'sma5', 'sma12', 'sma24', 'sma24_deriv1', 'sma24_deriv2', 'sma48', 'sma48_deriv1', 'sma48_deriv2', 'sma60', 'sma60_dist', 'sma60_deriv1', # model_indicators = ['open', 'high', 'close', 'haclose', 'percent', 'sma5', 'sma12', 'sma24', 'sma24_deriv1', 'sma24_deriv2', 'sma48', 'sma48_deriv1', 'sma48_deriv2', 'sma60', 'sma60_dist', 'sma60_deriv1',
# 'sma60_deriv2', 'mid_smooth_3_deriv2', 'mid_smooth_12_deriv1', 'mid_smooth_12_deriv2', 'mid_smooth_24', 'mid_smooth_24_deriv1', 'mid_smooth_24_deriv2', 'max_rsi_12', 'max_rsi_24', 'max12', # 'sma60_deriv2', 'mid_smooth_3_deriv2', 'mid_smooth_12_deriv1', 'mid_smooth_12_deriv2', 'mid_smooth_24', 'mid_smooth_24_deriv1', 'mid_smooth_24_deriv2', 'max_rsi_12', 'max_rsi_24', 'max12',
# 'max60', 'min60', 'min_max_60', 'bb_lowerband', 'bb_upperband', 'bb_width', 'macd', 'macdsignal', 'macdhist', 'sma_20', 'sma_100', 'atr', 'atr_norm', 'adx', 'obv', 'vol_24', 'adx_change', # 'max60', 'min60', 'min_max_60', 'bb_lowerband', 'bb_upperband', 'bb_width', 'macd', 'macdsignal', 'macdhist', 'sma_20', 'sma_100', 'atr', 'atr_norm', 'adx', 'obv', 'vol_24', 'adx_change',
@@ -993,12 +995,6 @@ class Zeus_LGBMRegressor(IStrategy):
dataframe['mid_smooth_5h'] dataframe['mid_smooth_5h']
dataframe["mid_smooth_5h_deriv2"] = 100 * dataframe["mid_smooth_5h_deriv1"].diff().rolling(window=60).mean() dataframe["mid_smooth_5h_deriv2"] = 100 * dataframe["mid_smooth_5h_deriv1"].diff().rolling(window=60).mean()
dataframe["ms-10"] = dataframe["mid_smooth_24_deriv1"].shift(10)
dataframe["ms-5"] = dataframe["mid_smooth_24_deriv1"].shift(5)
dataframe["ms-2"] = dataframe["mid_smooth_24_deriv1"].shift(2)
dataframe["ms-1"] = dataframe["mid_smooth_24_deriv1"].shift(1)
dataframe["ms-0"] = dataframe["mid_smooth_24_deriv1"]
# dataframe["ms+10"] = dataframe["mid_smooth_24"].shift(-11)
# =============================== # ===============================
# Lissage des valeurs Journalières # Lissage des valeurs Journalières
horizon_d = 12 * 5 * 24 horizon_d = 12 * 5 * 24
@@ -1114,6 +1110,14 @@ class Zeus_LGBMRegressor(IStrategy):
# #
# self.model_indicators = usable_cols # self.model_indicators = usable_cols
dataframe["ms-10"] = dataframe["mid_smooth_24_deriv1"].shift(10)
dataframe["ms-5"] = dataframe["mid_smooth_24_deriv1"].shift(5)
dataframe["ms-4"] = dataframe["mid_smooth_24_deriv1"].shift(4)
dataframe["ms-3"] = dataframe["mid_smooth_24_deriv1"].shift(3)
dataframe["ms-2"] = dataframe["mid_smooth_24_deriv1"].shift(2)
dataframe["ms-1"] = dataframe["mid_smooth_24_deriv1"].shift(1)
dataframe["ms-0"] = dataframe["mid_smooth_24_deriv1"]
# dataframe["ms+10"] = dataframe["mid_smooth_24"].shift(-11)
df = dataframe[self.model_indicators].copy() df = dataframe[self.model_indicators].copy()
# Corrélations des colonnes # Corrélations des colonnes
@@ -1572,7 +1576,7 @@ class Zeus_LGBMRegressor(IStrategy):
# print(f"✅ Meilleur F1 : {f1s[best_idx]:.3f} au seuil {seuils[best_idx]:.2f}") # print(f"✅ Meilleur F1 : {f1s[best_idx]:.3f} au seuil {seuils[best_idx]:.2f}")
# --- Après l'entraînement du modèle --- # --- Après l'entraînement du modèle ---
preds = self.model.predict(X_test) preds = model.predict(X_test)
# --- Évaluation --- # --- Évaluation ---
mse = mean_squared_error(y_test, preds) mse = mean_squared_error(y_test, preds)
@@ -1607,6 +1611,53 @@ class Zeus_LGBMRegressor(IStrategy):
print(f"✅ Graphique sauvegardé : {plot_path}") print(f"✅ Graphique sauvegardé : {plot_path}")
# save_dir = "/home/souti/freqtrade/user_data/plots/"
# os.makedirs(save_dir, exist_ok=True)
ax = lgb.plot_tree(model, tree_index=0, figsize=(30, 20),
show_info=["split_gain", "internal_value", "internal_count"])
plt.title("Arbre de décision n°0")
plt.savefig(os.path.join(plot_dir, "lgbm_tree_0.png"), bbox_inches="tight")
plt.close()
ax = lgb.plot_tree(model, figsize=(40, 20))
plt.title("Vue globale du modèle LGBM")
plt.savefig(os.path.join(plot_dir, "lgbm_all_trees.png"), bbox_inches="tight")
plt.close()
# X_test = np.linspace(0, 10, 1000).reshape(-1, 1)
y_pred = model.predict(X_test)
plt.figure(figsize=(10, 5))
plt.plot(X_test, y_pred, color="red", label="modèle LGBM")
plt.title("Fonction apprise par LGBMRegressor")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.savefig(os.path.join(plot_dir, "lgbm_function_curve.png"), bbox_inches="tight")
plt.close()
ax = lgb.plot_importance(model, max_num_features=30, figsize=(12, 6))
plt.title("Importance des features - LGBM")
plt.savefig(os.path.join(plot_dir, "lgbm_feature_importance.png"), bbox_inches="tight")
plt.close()
corr = X_train.corr() * 100 # en pourcentage
plt.figure(figsize=(20, 16))
sns.heatmap(corr, cmap="coolwarm", center=0, annot=False, fmt=".1f", cbar_kws={'label': 'Corrélation (%)'})
plt.title("Matrice de corrélation (%)")
plt.savefig(os.path.join(plot_dir, "correlation_matrix.png"), bbox_inches="tight")
plt.close()
plt.figure(figsize=(10, 6))
plt.scatter(y_test, model.predict(X_test), alpha=0.5)
plt.xlabel("Valeurs réelles")
plt.ylabel("Prédictions du modèle")
plt.title("Comparaison y_test vs y_pred")
plt.savefig(os.path.join(plot_dir, "ytest_vs_ypred.png"), bbox_inches="tight")
plt.close()
print("\n===== ✅ FIN DE LANALYSE =====") print("\n===== ✅ FIN DE LANALYSE =====")
def plot_threshold_analysis(self, y_true, y_proba, step=0.05, save_path=None): def plot_threshold_analysis(self, y_true, y_proba, step=0.05, save_path=None):