classifier_cv.py
Summary
This code defines a ClassifierCV class for automated classification using gradient boosting backends (XGBoost, CatBoost) with Bayesian optimization and SHAP explanations.
Dependencies
Standard Library
- typing
- multiprocessing
Other
- numpy
- pandas
- pydantic
- sklearn
- xgboost
- catboost
- shap (optional)
Description
The classifier_cv.py file implements an automated classification system using gradient-boosting backends as the base classifiers. The core components include:
ClassifierCVConfig: A Pydantic dataclass that defines configuration options for the classifier, including:monotone_constraints: Dictionary specifying monotonicity direction for variables (0 for none, 1 for increasing, -1 for decreasing)interactions: List of lists containing permitted feature interactionsn_jobs: Number of parallel jobs to use when fitting the modelparameters: Hyperparameter search space definition (OptimizerParams instance)-
return_train_score: Whether to include training scores during optimization -
Pre-configured settings:
default_classifier: A standard configuration using all hyperparameters-
balanced_classifier: A configuration focused on regularization parameters -
ClassifierCV: The main class that inherits fromBaseAutoCV,ClassifierMixin, andExplainerMixIn, providing: - Automated hyperparameter tuning via Bayesian optimization
- Pluggable backends:
backendcan be"xgboost"(default) or"catboost" - Model explanation using SHAP values (when available)
- Feature importance calculation
- Parallel processing support
The implementation gracefully handles cases where the optional SHAP library isn't available, still allowing the core classification functionality to work while disabling the explanation features.
The class supports custom classification metrics through integration with scikit-learn's scoring system and Pydantic's validation mechanism, ensuring that only valid metrics are used. Cross-validation is employed during the optimization process to prevent overfitting.
Overall, this module provides a robust, automated approach to building classification models with XGBoost, combining the power of Bayesian optimization for parameter tuning with the interpretability offered by SHAP explanations.