Gradient Boosting Example

This is a simple example of Gradient Boosting using Python and scikit-learn.

Gradient Boosting Overview

Gradient Boosting is an ensemble learning technique that builds a predictive model by combining the strengths of multiple weak models, typically decision trees. The model is built sequentially, with each new tree attempting to correct the errors of the combined ensemble so far. Gradient Boosting is powerful and robust, often achieving high performance across various tasks.

Key concepts of Gradient Boosting:

Gradient Boosting is widely used for regression and classification tasks and is implemented in libraries like scikit-learn, XGBoost, and LightGBM.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error

# Generate synthetic data
np.random.seed(42)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the Gradient Boosting model
model = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
model.fit(X_train, y_train.ravel())

# Make predictions on the test set
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')

# Plot the results
plt.scatter(X_test, y_test, color='black')
plt.scatter(X_test, y_pred, color='red', marker='x')
plt.title('Gradient Boosting Example')
plt.xlabel('X')
plt.ylabel('y')
plt.show()

Explanation: