classifierpromax.ClassifierOptimizer
Functions
|
|
|
|
|
|
|
Optimizes a dictionary of scikit-learn Pipeline classifiers using RandomizedSearchCV |
Module Contents
- classifierpromax.ClassifierOptimizer.validate_inputs(model_dict, param_dist, X_train, y_train, scoring, n_iter, cv, random_state, n_jobs)[source]
- classifierpromax.ClassifierOptimizer.optimize_model(name, model, param_dist, X_train, y_train, scoring, n_iter, cv, random_state, n_jobs)[source]
- classifierpromax.ClassifierOptimizer.ClassifierOptimizer(model_dict, X_train, y_train, scoring='f1', n_iter=100, cv=5, random_state=42, n_jobs=-1)[source]
Optimizes a dictionary of scikit-learn Pipeline classifiers using RandomizedSearchCV and evaluates their performance.
Parameters:
- model_dictdict
A dictionary where keys are model names (str) and values are scikit-learn Pipeline objects. Each pipeline must contain a classifier whose hyperparameters are defined in param_dist.
- X_trainpandas.DataFrame or numpy.ndarray
The feature matrix for training the classifiers. Must have the same number of samples as y_train.
- y_trainpandas.Series or numpy.ndarray
The target labels for training the classifiers. Must have the same number of samples as X_train.
- scoringstr, optional, default=’f1’
The scoring metric to use for hyperparameter optimization and model evaluation. Must be one of the following: - “accuracy” - “precision” - “recall” - “f1”
- n_iterint, optional, default=100
The number of parameter settings sampled for RandomizedSearchCV.
- cvint, optional, default=5
The number of cross-validation folds for both RandomizedSearchCV and cross_validate.
- random_stateint, optional, default=42
Random seed for reproducibility of RandomizedSearchCV.
- n_jobsint, optional, default=-1
The number of jobs to run in parallel for RandomizedSearchCV (-1 uses all available processors).
Returns:
- optimized_model_dictdict
A dictionary containing the best estimators for each classifier after hyperparameter optimization.
- scoring_dictdict
A dictionary containing cross-validation results for each optimized model, with metrics aggregated by mean and standard deviation.
Raises:
- ValueError
If the input parameters are invalid (e.g., empty model dictionary, mismatched data shapes, unsupported scoring metric).
Examples:
>>> from sklearn.pipeline import Pipeline >>> from sklearn.linear_model import LogisticRegression >>> from sklearn.svm import SVC >>> from sklearn.ensemble import RandomForestClassifier >>> from sklearn.preprocessing import StandardScaler >>> model_dict = { ... 'logreg': Pipeline([ ... ('scaler', StandardScaler()), ... ('logisticregression', LogisticRegression()) ... ]), ... 'svc': Pipeline([ ... ('scaler', StandardScaler()), ... ('svc', SVC()) ... ]), ... 'random_forest': Pipeline([ ... ('randomforestclassifier', RandomForestClassifier()) ... ]) ... } >>> optimized_models, scoring_results = ClassifierOptimizer(model_dict, X_train, y_train)