cursus.steps.scripts.xgboost_model_inference

class RiskTableMappingProcessor(column_name, label_name, smooth_factor=0.0, count_threshold=0, risk_tables=None)[source]

Bases: object

A 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.

get_name()[source]
set_risk_tables(risk_tables)[source]
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.

transform(data)[source]

Transform data using the computed risk tables. - If data is a DataFrame, transforms the ‘column_name’ Series within it. - If data is a Series, transforms the Series (assumed to be the target column). - If data is a single value, uses the ‘process’ method.

get_risk_tables()[source]
class NumericalVariableImputationProcessor(variables=None, imputation_dict=None, strategy='mean')[source]

Bases: object

A processor that performs imputation on numerical variables using predefined or computed values. Supports mean, median, and mode imputation strategies.

get_name()[source]
process(input_data)[source]
transform(X)[source]

Transform input data by imputing missing values.

Parameters:

X (DataFrame | Series) – Input DataFrame or Series

Returns:

Transformed DataFrame or Series with imputed values

Return type:

DataFrame | Series

get_params()[source]
load_dataframe_with_format(file_path)[source]

Load DataFrame and detect its format.

Parameters:

file_path (Path) – Path to the file

Returns:

Tuple of (DataFrame, format_string)

Return type:

Tuple[DataFrame, str]

save_dataframe_with_format(df, output_path, format_str)[source]

Save DataFrame in specified format.

Parameters:
  • df (DataFrame) – DataFrame to save

  • output_path (Path) – Base output path (without extension)

  • format_str (str) – Format to save in (‘csv’, ‘tsv’, or ‘parquet’)

Returns:

Path to saved file

Return type:

Path

load_model_artifacts(model_dir)[source]

Load the trained XGBoost model and all preprocessing artifacts from the specified directory. Handles both extracted artifacts and model.tar.gz archives. Returns model, risk_tables, impute_dict, feature_columns, and hyperparameters.

preprocess_inference_data(df, feature_columns, risk_tables, impute_dict)[source]

Apply risk table mapping and numerical imputation to inference data. Preserves all original columns while ensuring features are model-ready.

generate_predictions(model, df, feature_columns, hyperparams)[source]

Generate predictions using the XGBoost model. Handles both binary and multiclass scenarios.

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(df, predictions, output_dir, input_format='csv', id_col='id', label_col='label', json_orient='records')[source]

Save predictions with original data preserving input format. Supports CSV, TSV, Parquet, and JSON formats.

create_health_check_file(output_path)[source]

Create a health check file to signal script completion.

main(input_paths, output_paths, environ_vars, job_args)[source]

Main entry point for XGBoost model inference script. Loads model and data, runs inference, and saves predictions.

Parameters:
  • input_paths (Dict[str, str]) – Dictionary of input paths

  • output_paths (Dict[str, str]) – Dictionary of output paths

  • environ_vars (Dict[str, str]) – Dictionary of environment variables

  • job_args (argparse.Namespace) – Command line arguments