XGBoost in Python
Here’s a basic example of how to use XGBoost in Python for a classification task.
This example uses the popular Iris dataset.
Install XGBoost
If you don’t have XGBoost installed, you can install it using pip:
1 | pip install xgboost |
Sample Code
1 | import xgboost as xgb |
Explanation
Loading the Dataset: We use the Iris dataset, which is a common dataset for classification tasks.
It contains three classes of flowers.Splitting the Data: The dataset is split into training and testing sets using
train_test_split
.DMatrix: XGBoost uses its own data structure called
DMatrix
for training.
It is more efficient and optimized for XGBoost operations.Setting Parameters:
objective
: Defines the learning task and the corresponding objective function.
In this case,multi:softmax
is used for multiclass classification.num_class
: Specifies the number of classes.max_depth
: The maximum depth of the trees.eta
: The learning rate.
Training: The model is trained using the
train
function with the specified parameters and number of boosting rounds.Prediction: The trained model makes predictions on the test set, and the accuracy is calculated using
accuracy_score
.
This is a basic example, but XGBoost offers a wide range of parameters and options that can be fine-tuned for different types of data and tasks.
Output
1 | Accuracy: 100.00% |