XGBoosting Home | About | Contact | Examples

XGBoost for the Ionosphere Dataset

The Ionosphere dataset is a binary classification dataset used for detecting structures in the ionosphere. It contains 34 continuous predictor variables and a binary target variable indicating the presence of structures.

In this example, we’ll load the Ionosphere dataset using fetch_openml from scikit-learn, perform hyperparameter tuning using GridSearchCV with common XGBoost parameters, save the best model, load it, and use it to make predictions.

from sklearn.datasets import fetch_openml
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.preprocessing import LabelEncoder
from xgboost import XGBClassifier
import numpy as np
from collections import Counter

# Load the Ionosphere dataset
ionosphere = fetch_openml('ionosphere', as_frame=True)
X, y = ionosphere.data, ionosphere.target

# Print key information about the dataset
print(f"Dataset shape: {X.shape}")
print(f"Features: {ionosphere.feature_names}")
print(f"Target variable: {ionosphere.target_names}")
print(f"Class distributions: {Counter(y)}")

# Encode target variable
y = LabelEncoder().fit_transform(y)

# 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, stratify=y)

# Define parameter grid
param_grid = {
    'max_depth': [3, 4, 5],
    'learning_rate': [0.1, 0.01, 0.05],
    'n_estimators': [50, 100, 200],
    'subsample': [0.8, 1.0],
    'colsample_bytree': [0.8, 1.0]

# Create XGBClassifier
model = XGBClassifier(objective='binary:logistic', random_state=42, n_jobs=1)

# Perform grid search
grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=3, n_jobs=-1)
grid_search.fit(X_train, y_train)

# Print best score and parameters
print(f"Best score: {grid_search.best_score_:.3f}")
print(f"Best parameters: {grid_search.best_params_}")

# Access best model
best_model = grid_search.best_estimator_

# Save best model

# Load saved model
loaded_model = XGBClassifier()

# Use loaded model for predictions
predictions = loaded_model.predict(X_test)

# Print accuracy score
accuracy = loaded_model.score(X_test, y_test)
print(f"Accuracy: {accuracy:.3f}")

Running the example, you will see results similar to the following:

Dataset shape: (351, 34)
Features: ['a01', 'a02', 'a03', 'a04', 'a05', 'a06', 'a07', 'a08', 'a09', 'a10', 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a17', 'a18', 'a19', 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a29', 'a30', 'a31', 'a32', 'a33', 'a34']
Target variable: ['class']
Class distributions: Counter({'g': 225, 'b': 126})
Best score: 0.911
Best parameters: {'colsample_bytree': 0.8, 'learning_rate': 0.1, 'max_depth': 4, 'n_estimators': 200, 'subsample': 0.8}
Accuracy: 0.958

In this example, we load the Ionosphere dataset using fetch_openml from scikit-learn. We print key information about the dataset, including its shape, feature names, and target variable.

Next, we split the data into train and test sets using train_test_split, define a parameter grid for hyperparameter tuning, create an instance of XGBClassifier, and perform a grid search using GridSearchCV with 3-fold cross-validation. We print the best score and corresponding best parameters.

We access the best model using best_estimator_, save it to a file named ‘best_model_ionosphere.ubj’, and demonstrate loading the saved model using load_model().

Finally, we use the loaded model to make predictions on the test set and print the accuracy score.

By following this approach, you can easily perform hyperparameter tuning on the Ionosphere dataset using XGBoost, save the best model, and use it for making predictions.

See Also