XGBoosting Home | About | Contact | Examples

Configure XGBoost Objective "multi:softmax" vs "multi:softprob"

This example will differentiate between the XGBoost objectives “multi:softmax” and “multi:softprob,” which are both used for multi-class classification tasks.

It will explain when to use each objective, providing a full code example to highlight their implementation and key differences.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, log_loss

# Generate a synthetic multi-class classification dataset
X, y = make_classification(n_samples=1000, n_clusters_per_class=1, n_classes=3, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define the model using the multi:softmax objective
model_softmax = XGBClassifier(objective="multi:softmax", num_class=3, n_estimators=100, learning_rate=0.1)
model_softmax.fit(X_train, y_train)
y_pred_softmax = model_softmax.predict(X_test)
accuracy_softmax = accuracy_score(y_test, y_pred_softmax)

# Define the model using the multi:softprob objective
model_softprob = XGBClassifier(objective="multi:softprob", num_class=3, n_estimators=100, learning_rate=0.1)
model_softprob.fit(X_train, y_train)
y_pred_softprob = model_softprob.predict_proba(X_test)
logloss_softprob = log_loss(y_test, y_pred_softprob)

print(f"Accuracy for 'multi:softmax': {accuracy_softmax:.4f}")
print(f"Log Loss for 'multi:softprob': {logloss_softprob:.4f}")

Analysis of Outcomes:

Best Practices and Tips:



See Also