0
0
ML Pythonml~20 mins

XGBoost in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
XGBoost Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding XGBoost Objective Functions

Which of the following objective functions is not commonly used in XGBoost for classification tasks?

Areg:squarederror
Bmulti:softmax
Cbinary:logistic
Dcount:poisson
Attempts:
2 left
💡 Hint

Think about which objective functions are for regression versus classification.

Predict Output
intermediate
2:00remaining
XGBoost Model Training Output

What will be the output of the following Python code snippet using XGBoost?

ML Python
import xgboost as xgb
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
params = {'objective': 'multi:softprob', 'num_class': 3, 'eval_metric': 'mlogloss'}
model = xgb.train(params, dtrain, num_boost_round=10, evals=[(dtest, 'eval')], verbose_eval=False)
preds = model.predict(dtest)
print(len(preds), len(preds[0]))
A30
B30 1
C120 3
D30 3
Attempts:
2 left
💡 Hint

Check the number of test samples and the shape of prediction output for multi-class softprob.

Model Choice
advanced
2:00remaining
Choosing XGBoost Parameters for Imbalanced Data

You have a highly imbalanced binary classification dataset. Which XGBoost parameter setting is best to help the model focus on the minority class?

ASet <code>max_depth</code> to a very high value like 20
BSet <code>scale_pos_weight</code> to the ratio of negative to positive samples
CUse <code>objective</code> as <code>reg:squarederror</code>
DSet <code>learning_rate</code> to 1.0
Attempts:
2 left
💡 Hint

Think about how to handle class imbalance in XGBoost.

Metrics
advanced
2:00remaining
Evaluating XGBoost Model with Multi-class Log Loss

After training an XGBoost model with objective='multi:softprob', you get a multi-class log loss (mlogloss) of 0.8 on the test set. What does this value indicate?

AThe model predictions have moderate confidence and some errors
BThe model predictions are random and uninformative
CThe model predictions are very confident and accurate
DThe model predictions perfectly match the true labels
Attempts:
2 left
💡 Hint

Lower mlogloss is better; think about what 0.8 means.

🔧 Debug
expert
2:00remaining
Debugging XGBoost Training Error

Consider this code snippet that raises an error during training:

import xgboost as xgb
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 1])
dtrain = xgb.DMatrix(X, label=y)
params = {'objective': 'binary:logistic'}
model = xgb.train(params, dtrain, num_boost_round=5)

What is the cause of the error?

AMissing eval_metric parameter in params
BIncorrect objective function for binary classification
CMismatch between number of samples in X and y labels
DDMatrix requires labels to be float, not int
Attempts:
2 left
💡 Hint

Check the shapes of X and y carefully.