cursus.steps.scripts.xgboost_model_eval¶
- class RiskTableMappingProcessor(column_name, label_name, smooth_factor=0.0, count_threshold=0, risk_tables=None)[source]¶
Bases:
objectA processor that performs risk-table-based mapping on a specified categorical variable. The ‘process’ method (called via __call__) handles single values. The ‘transform’ method handles pandas Series or DataFrames.
- process(input_value)[source]¶
Process a single input value (for the configured ‘column_name’), mapping it to its binned risk value. This method is called when the processor instance is called as a function.
- class NumericalVariableImputationProcessor(variables=None, imputation_dict=None, strategy='mean')[source]¶
Bases:
objectA processor that performs imputation on numerical variables using predefined or computed values. Supports mean, median, and mode imputation strategies.
- save_dataframe_with_format(df, output_path, format_str)[source]¶
Save DataFrame in specified format.
- decompress_model_artifacts(model_dir)[source]¶
Checks for a model.tar.gz file in the model directory and extracts it.
- load_model_artifacts(model_dir)[source]¶
Load the trained XGBoost model and all preprocessing artifacts from the specified directory. Returns model, risk_tables, impute_dict, feature_columns, and hyperparameters.
- preprocess_eval_data(df, feature_columns, risk_tables, impute_dict)[source]¶
Apply risk table mapping and numerical imputation to the evaluation DataFrame. Ensures all features are numeric and columns are ordered as required by the model. Preserves any non-feature columns like id and label.
- log_metrics_summary(metrics, is_binary=True)[source]¶
Log a nicely formatted summary of metrics for easy visibility in logs.
- compute_metrics_binary(y_true, y_prob)[source]¶
Compute binary classification metrics: AUC-ROC, average precision, and F1 score.
- compute_metrics_multiclass(y_true, y_prob, n_classes)[source]¶
Compute multiclass metrics: one-vs-rest AUC-ROC, average precision, F1 for each class, and micro/macro averages for all metrics.
- compute_comparison_metrics(y_true, y_new_score, y_prev_score, is_binary=True)[source]¶
Compute comparison metrics between new model and previous model scores.
- perform_statistical_tests(y_true, y_new_score, y_prev_score, is_binary=True)[source]¶
Perform statistical significance tests comparing model performances.
- load_eval_data(eval_data_dir)[source]¶
Load the first data file found in the evaluation data directory. Returns a pandas DataFrame and the detected format.
- get_id_label_columns(df, id_field, label_field)[source]¶
Determine the ID and label columns in the DataFrame. Falls back to the first and second columns if not found.
- save_predictions(ids, y_true, y_prob, id_col, label_col, output_eval_dir, input_format='csv')[source]¶
Save predictions preserving input format, including id, true label, and class probabilities.
- plot_and_save_roc_curve(y_true, y_score, output_dir, prefix='')[source]¶
Plot ROC curve and save as JPG.
- plot_and_save_pr_curve(y_true, y_score, output_dir, prefix='')[source]¶
Plot Precision-Recall curve and save as JPG.
- plot_comparison_roc_curves(y_true, y_new_score, y_prev_score, output_dir)[source]¶
Plot side-by-side ROC curves comparing new and previous models.
- plot_comparison_pr_curves(y_true, y_new_score, y_prev_score, output_dir)[source]¶
Plot side-by-side Precision-Recall curves comparing new and previous models.
- plot_score_scatter(y_new_score, y_prev_score, y_true, output_dir)[source]¶
Plot scatter plot of new vs previous model scores, colored by true labels.
- plot_score_distributions(y_new_score, y_prev_score, y_true, output_dir)[source]¶
Plot score distributions for both models, separated by true labels.
- evaluate_model(model, df, feature_columns, id_col, label_col, hyperparams, output_eval_dir, output_metrics_dir, input_format='csv')[source]¶
Run model prediction and evaluation, then save predictions and metrics preserving format. Also generate and save ROC and PR curves as JPG.
- evaluate_model_with_comparison(model, df, feature_columns, id_col, label_col, previous_scores, hyperparams, output_eval_dir, output_metrics_dir, comparison_metrics, statistical_tests, comparison_plots, input_format='csv')[source]¶
Run model prediction and evaluation with comparison to previous model scores preserving format. Generates comprehensive comparison metrics, statistical tests, and visualizations.
- save_predictions_with_comparison(ids, y_true, y_prob, previous_scores, id_col, label_col, output_eval_dir, input_format='csv')[source]¶
Save predictions preserving input format, including id, true label, new model probabilities, and previous model scores.
- create_comparison_report(metrics, output_metrics_dir, is_binary)[source]¶
Create a comprehensive comparison report summarizing model performance differences.
- create_health_check_file(output_path)[source]¶
Create a health check file to signal script completion.