Gradient Boosting (Basico)

Basic

Gradient Boosting (Basico) | Intuicion, formulas y practica

Creado: Última actualización: Tiempo de lectura: 2 min
import numpy as np
import matplotlib.pyplot as plt
import japanize_matplotlib
from sklearn.ensemble import GradientBoostingRegressor

Intuicion (formulas) #

Minimizar una perdida \(L(y,F(x))\) con un modelo aditivo por etapas:

  • Inicializar \(F_0(x)\).
  • Para \(m=1,\dots,M\): calcular los gradientes negativos (pseudo-residuos) \(r_{im} = -\left[\partial L(y_i, F(x_i))/\partial F\right]{F=F{m-1}}\), ajustar un arbol \(h_m\) a \(r_{im}\), y actualizar \(F_m(x) = F_{m-1}(x) + \nu, \rho_m, h_m(x)\) (\(\nu\): learning rate).

Ajuste en datos sinteticos #

X = np.linspace(-10, 10, 500)[:, np.newaxis]
noise = np.random.rand(X.shape[0]) * 10
y = (
    (np.sin(X).ravel() + np.cos(4 * X).ravel()) * 10
    + 10
    + np.linspace(-10, 10, 500)
    + noise
)

reg = GradientBoostingRegressor(n_estimators=50, learning_rate=0.5)
reg.fit(X, y)
y_pred = reg.predict(X)

plt.figure(figsize=(10, 5))
plt.scatter(X, y, c="k", marker="x", label="train")
plt.plot(X, y_pred, c="r", label="prediction", linewidth=1)
plt.xlabel("x")
plt.ylabel("y")
plt.title("Ajuste en datos de entrenamiento")
plt.legend()
plt.show()

Ajuste en datos sinteticos


Efecto de la funcion de perdida (outliers) #

Comparar loss en { "squared_error", "absolute_error", "huber", "quantile" }.

X = np.linspace(-10, 10, 500)[:, np.newaxis]
noise = np.random.rand(X.shape[0]) * 10
for i in range(0, X.shape[0], 80):
    noise[i] = 70 + np.random.randint(-10, 10)
y = (
    (np.sin(X).ravel() + np.cos(4 * X).ravel()) * 10
    + 10
    + np.linspace(-10, 10, 500)
    + noise
)

for loss in ["squared_error", "absolute_error", "huber", "quantile"]:
    reg = GradientBoostingRegressor(n_estimators=50, learning_rate=0.5, loss=loss)
    reg.fit(X, y)
    y_pred = reg.predict(X)

    plt.figure(figsize=(10, 5))
    plt.scatter(X, y, c="k", marker="x", label="train")
    plt.plot(X, y_pred, c="r", label="prediction", linewidth=1)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.title(f"loss={loss}")
    plt.legend()
    plt.show()

Comparacion de loss Comparacion de loss