[파이썬] catboost 피처 중요도 평가

CatBoost is a popular gradient boosting library that is known for its ability to handle categorical features with ease. It provides built-in feature importance calculation methods, which allow us to evaluate the importance of features in our dataset.

In this blog post, we will explore the different methods provided by CatBoost to assess feature importance and how to use them effectively in Python.

1. Permutation Importance

Permutation importance is a widely used method to measure feature importance in machine learning models. It evaluates the impact of shuffling a feature’s values on the model’s performance. If shuffling a feature impairs the model’s performance, it implies that the feature is important.

Here’s an example code snippet to calculate permutation importance using CatBoost:

from catboost import CatBoostClassifier
from catboost import Pool

# Load data
X, y = ...

# Create a CatBoost classifier
model = CatBoostClassifier(iterations=100, random_seed=42)

# Fit the model
model.fit(X, y)

# Create a Pool object for feature importance calculation
pool = Pool(X, y)

# Calculate feature importance using permutation importance
feature_importance = model.get_feature_importance(pool, type='Permutation')

# Print feature importance scores
for i, score in enumerate(feature_importance):
    print("Feature", i+1, ":", score)

2. Shapley Values

Shapley values is a game-theoretic method that assigns a value to each feature based on its contribution to the prediction of the model. It considers all possible combinations of features and calculates the average change in the prediction when a particular feature is added to or removed from the combination.

Here’s an example code snippet to calculate Shapley values using CatBoost:

from catboost import CatBoostRegressor
from catboost import Pool

# Load data
X, y = ...

# Create a CatBoost regressor
model = CatBoostRegressor(iterations=100, random_seed=42)

# Fit the model
model.fit(X, y)

# Create a Pool object for feature importance calculation
pool = Pool(X, y)

# Calculate feature importance using Shapley values
feature_importance = model.get_feature_importance(pool, type='ShapValues')

# Print feature importance scores
for i, score in enumerate(feature_importance):
    print("Feature", i+1, ":", score)

3. Feature Scores

Feature scores provide an overall measure of feature importance based on their influence on the model’s decision-making process. CatBoost calculates feature scores by considering the frequency of splits (how often a feature is used to split the dataset) and the gain associated with each split.

Here’s an example code snippet to calculate feature scores using CatBoost:

from catboost import CatBoostClassifier
from catboost import Pool

# Load data
X, y = ...

# Create a CatBoost classifier
model = CatBoostClassifier(iterations=100, random_seed=42)

# Fit the model
model.fit(X, y)

# Create a Pool object for feature importance calculation
pool = Pool(X, y)

# Calculate feature scores
feature_importance = model.get_feature_importance(pool, type='FeatureScore')

# Print feature importance scores
for i, score in enumerate(feature_importance):
    print("Feature", i+1, ":", score)

These are just a few methods provided by CatBoost to evaluate feature importance. It is always recommended to try different methods and choose the one that suits your particular use case.