XGBoost Example

This is a simple example of XGBoost (Extreme Gradient Boosting) using Python and the XGBoost library.

XGBoost Overview

XGBoost is a powerful and efficient gradient boosting algorithm that is widely used for supervised learning tasks, such as classification and regression. It is an ensemble learning method that builds a series of weak learners (usually decision trees) and combines their predictions to create a strong predictive model. XGBoost is known for its speed, performance, and regularization techniques to prevent overfitting.

Key concepts of XGBoost:

XGBoost is widely used in various machine learning competitions and real-world applications.

Python Source Code:

# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split
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 XGBRegressor model
xgb_model = XGBRegressor(n_estimators=100, learning_rate=0.1, max_depth=3, random_state=42)
xgb_model.fit(X_train, y_train.ravel())

# Predict on the test set
y_pred = xgb_model.predict(X_test)

# Plot the model's predictions
plt.figure(figsize=(10, 6))
plt.scatter(X_test, y_test, color='black', label='True Data Points')
plt.plot(X_test, y_pred, color='red', label='XGBRegressor Prediction')
plt.title('XGBoost Regression')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

Explanation: