XGBoosting Home | About | Contact | Examples

Configure XGBoost "reg:squarederror" Objective

The "reg:squarederror" objective in XGBoost is used for regression tasks when the target variable is continuous. It minimizes the squared error between the predicted and actual values, making it a common choice for regression problems due to its simplicity and interpretability.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error

# Generate a synthetic dataset for regression
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, random_state=42)

# 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)

# Initialize an XGBRegressor with the "reg:squarederror" objective
model = XGBRegressor(objective="reg:squarederror", n_estimators=100, learning_rate=0.1)

# Fit the model on the training data
model.fit(X_train, y_train)

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

# Calculate the mean squared error of the predictions
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")

The "reg:squarederror" objective minimizes the squared difference between the predicted and actual values, which is equivalent to the mean squared error (MSE) loss function.

This objective is suitable when the target variable is continuous, and the goal is to minimize the average squared difference between predictions and actual values.

When using the "reg:squarederror" objective, consider the following tips:

  1. Ensure that the target variable is continuous and not categorical or binary.
  2. Scale the input features to a similar range to improve convergence and model performance.
  3. Use appropriate evaluation metrics for regression, such as MSE, RMSE, or MAE, to assess the model’s performance.
  4. Tune hyperparameters like learning_rate, max_depth, and n_estimators to optimize performance.


See Also