XGBoosting Home | About | Contact | Examples

XGBoost get_params() Method

The get_params() method in XGBoost allows you to access all the parameters of a trained model, including both default and user-specified settings.

Retrieving these parameters can be useful for saving and loading model configurations, comparing settings across different models, or updating parameters for fine-tuning.

This example demonstrates how to use get_params() to retrieve and utilize the parameters of a trained XGBoost model.

# XGboosting.com
# XGBoost get_xgb_params() Method
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier

# Load the Iris dataset
iris = load_iris()
X, y = iris.data, iris.target

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train an XGBoost model with specific parameters
model = XGBClassifier(n_estimators=100, learning_rate=0.1, max_depth=3, subsample=0.8, random_state=42)
model.fit(X_train, y_train)

# Access all model parameters using get_params()
params = model.get_params()

# Print the retrieved parameters
print("XGBoost model parameters:")
for param, value in params.items():
    print(f"{param}: {value}")

The get_params() method returns a dictionary containing all the parameters of the trained XGBoost model. These parameters include both the default settings and any user-specified values provided during model initialization.

It’s important to note that get_params() only returns the parameters of a trained model. Ensure that you call this method after the model has been fitted using the fit() method.

Here are some practical tips for using get_params():

  1. Use the retrieved parameters for saving and loading model configurations. You can save the parameters to a file and later load them to recreate the same model.
  2. Compare parameters across different models to understand the differences in their configurations. This can be helpful when experimenting with multiple models and identifying the best-performing settings.
  3. Update specific parameters using the retrieved dictionary for fine-tuning or further experimentation. You can modify the values of certain parameters and create a new model with the updated settings.
  4. Access individual parameters from the dictionary using their respective keys. For example, params['max_depth'] retrieves the value of the max_depth parameter.
  5. Iterate over the parameter dictionary for analysis or logging purposes. You can examine each parameter and its corresponding value to gain insights into the model’s configuration.

By leveraging the get_params() method, you can easily access and utilize all the parameters of a trained XGBoost model, enabling better model management, comparison, and experimentation.

See Also