Install and use XGBoost Python package for gradient boosting machine learning models. Train, evaluate, and deploy high-performance ML models for classification and regression tasks.
A skill for installing and using the XGBoost Python package to build gradient boosting machine learning models.
This skill helps you install XGBoost from PyPI and use it to train gradient boosting models for classification and regression tasks. XGBoost is a high-performance implementation of gradient boosted decision trees designed for speed and performance.
Install the stable version of XGBoost from PyPI:
```bash
pip install xgboost
```
For specific version requirements, use:
```bash
pip install xgboost==3.1.3
```
Import the package in your Python code:
```python
import xgboost as xgb
```
Load and prepare your dataset. XGBoost works with NumPy arrays, pandas DataFrames, or its native DMatrix format:
```python
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv('your_data.csv')
X = df.drop('target', axis=1)
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
```
For classification:
```python
params = {
'objective': 'binary:logistic',
'max_depth': 6,
'learning_rate': 0.1,
'n_estimators': 100
}
model = xgb.XGBClassifier(**params)
model.fit(X_train, y_train)
```
For regression:
```python
params = {
'objective': 'reg:squarederror',
'max_depth': 6,
'learning_rate': 0.1,
'n_estimators': 100
}
model = xgb.XGBRegressor(**params)
model.fit(X_train, y_train)
```
```python
predictions = model.predict(X_test)
```
For classification:
```python
from sklearn.metrics import accuracy_score, classification_report
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy}")
print(classification_report(y_test, predictions))
```
For regression:
```python
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print(f"MSE: {mse}")
print(f"R²: {r2}")
```
Analyze which features contribute most to predictions:
```python
import matplotlib.pyplot as plt
xgb.plot_importance(model)
plt.show()
```
Save the trained model:
```python
model.save_model('xgboost_model.json')
```
Load a saved model:
```python
loaded_model = xgb.XGBClassifier()
loaded_model.load_model('xgboost_model.json')
```
```python
import xgboost as xgb
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = xgb.XGBClassifier(max_depth=5, learning_rate=0.1, n_estimators=100)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, predictions)}")
```
```python
import xgboost as xgb
from sklearn.datasets import make_regression
from sklearn.model_selection import cross_val_score
X, y = make_regression(n_samples=1000, n_features=20, random_state=42)
model = xgb.XGBRegressor(max_depth=5, learning_rate=0.1, n_estimators=100)
scores = cross_val_score(model, X, y, cv=5, scoring='r2')
print(f"Cross-validated R² scores: {scores}")
print(f"Mean R²: {scores.mean()}")
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/xgboost-model-training/raw