XGBoosting Home | About | Contact | Examples

XGBoost for Multi-Class Classification

Multi-class classification involves predicting a single label from more than two classes for each instance. XGBoost’s objective="multi:softmax" parameter enables efficient and effective multi-class classification.

# XGBoosting.com
# Fit an XGBoost Model for Multi-Class Classification using scikit-learn API
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier

# Generate a synthetic multi-class dataset
X, y = make_classification(n_samples=1000, n_classes=5, n_informative=8, n_redundant=1, random_state=42)

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

# Initialize XGBClassifier
model = XGBClassifier(
    n_estimators=10,
    objective='multi:softmax',
    num_class=5,
    random_state=42
)

# Fit the model
model.fit(X_train, y_train)

# Generate predictions
predictions = model.predict(X_test)

print(predictions[:5])

By setting objective="multi:softmax" and specifying the num_class parameter to match the number of classes in your dataset, you can easily adapt XGBoost for multi-class classification tasks.

The multi:softmax objective uses a softmax function to calculate the probability of each class and selects the class with the highest probability as the prediction.



See Also