33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
import numpy as np
|
||
import matplotlib.pyplot as plt
|
||
from lightgbm import LGBMRegressor
|
||
|
||
# === Données non linéaires ===
|
||
np.random.seed(0)
|
||
X = np.linspace(0, 10, 200).reshape(-1, 1)
|
||
y = np.sin(X).ravel() + np.random.normal(0, 0.1, X.shape[0]) # sinusoïde + bruit
|
||
|
||
# === Entraînement du modèle ===
|
||
model = LGBMRegressor(
|
||
n_estimators=300, # nombre d’arbres
|
||
learning_rate=0.05, # taux d’apprentissage (plus petit = plus lisse)
|
||
max_depth=5 # profondeur des arbres (plus grand = plus complexe)
|
||
)
|
||
model.fit(X, y)
|
||
|
||
# === Prédiction ===
|
||
X_test = np.linspace(0, 10, 500).reshape(-1, 1)
|
||
y_pred = model.predict(X_test)
|
||
|
||
# === Visualisation ===
|
||
plt.figure(figsize=(10, 5))
|
||
plt.scatter(X, y, color="lightgray", label="Données réelles (sin + bruit)", s=20)
|
||
plt.plot(X_test, np.sin(X_test), color="green", linestyle="--", label="sin(x) réel")
|
||
plt.plot(X_test, y_pred, color="red", label="Prédiction LGBM")
|
||
plt.title("Approximation non linéaire avec LGBMRegressor")
|
||
plt.xlabel("x")
|
||
plt.ylabel("y")
|
||
plt.legend()
|
||
plt.grid(True)
|
||
plt.show()
|