XGBoosting Home | About | Contact | Examples

Predict Numeric Values with XGBoost Regression

XGBoost is not only powerful for classification tasks but also excels at regression problems where the goal is to predict continuous numeric values.

Whether you’re estimating house prices, stock values, or any other real-valued target, XGBoost’s regression capabilities can help you build highly accurate models.

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor

# Load the California housing dataset
X, y = fetch_california_housing(return_X_y=True)

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

# Create an XGBRegressor
model = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)

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

# Predict numeric values on the test data
predictions = model.predict(X_test)

print("Predicted values:\n", predictions[:5])  # Print the first 5 predictions

The model.predict() method returns an array of numeric predictions, one for each input sample in the test set. These predictions can be directly used for tasks like estimating house prices, stock values, or any other continuous target variable.

See Also