cursus.processing.categorical¶
Categorical Processing Module
This module provides atomic processors for categorical data processing, including encoding, imputation, validation, and numerical categorization.
- class CategoricalLabelProcessor(initial_categories=None, update_on_new=True, unknown_label=-1)[source]¶
Bases:
ProcessorMap category strings to integer labels.
Warning
With
update_on_new=True(the default),process()MUTATES the category->label mapping on first sight of a new category. This is not safe across DataLoader workers: undernum_workers > 0the processor is forked per worker, so each worker assigns its own (divergent) ids to the same category and labels become inconsistent. For multi-worker / distributed use, pass a completeinitial_categorieslist and setupdate_on_new=Falseso the mapping is fixed and read-only during processing.
- class MultiClassLabelProcessor(label_list=None, one_hot=False, strict=False)[source]¶
Bases:
ProcessorProcesses multi-class labels into a format suitable for machine learning models.
This processor handles encoding categorical labels into numerical arrays and optionally provides one-hot encoding using numpy.
- Parameters:
label_list (Optional[List[str]]) – A list of unique label strings. If provided, the processor will learn this mapping; otherwise, it will learn the mapping from the data it processes.
one_hot (bool) – If True, output one-hot encoded labels.
strict (bool) – If True, raise error for unknown labels when label_list is provided.
- class DictionaryEncodingProcessor(categorical_map=None, unknown_strategy='default', default_value=0, columns=None)[source]¶
Bases:
ProcessorPure dictionary-based categorical encoding.
Extracted from TSA CategoricalTransformer functionality.
- Parameters:
- class CategoricalImputationProcessor(default_values=None, missing_indicators=None, strategy='default', constant_value='UNKNOWN')[source]¶
Bases:
ProcessorHandles missing categorical values with configurable defaults.
Extracted from TSA default value handling logic.
- Parameters:
- class NumericalCategoricalProcessor(binning_strategy='equal_width', n_bins=5, bin_edges=None, thresholds=None, labels=None, columns=None)[source]¶
Bases:
ProcessorConverts numerical values to categorical labels using binning or thresholds.
Extracted from TSA str(int(float(cur_var))) conversion patterns.
- Parameters:
- fit(data)[source]¶
Learn binning parameters from data.
For
equal_width/equal_frequencythe bin edges are LEARNED here. Forcustom/thresholdthe bins come from the constructor params (bin_edges/thresholds) and there is nothing to learn — but we still validate the required param is present sois_fittedis never set True for a strategy that has no usable bins (which previously caused a silent mis-binning at transform time).
- class CategoricalValidationProcessor(allowed_values=None, validation_rules=None, validation_strategy='warn', report_violations=True, max_violations=None)[source]¶
Bases:
ProcessorValidates categorical data quality and consistency.
Extracted from TSA data validation requirements.
- Parameters:
allowed_values (Dict[str, Set[Any]] | None) – Dictionary of field -> allowed values mappings
validation_rules (Dict[str, callable] | None) – Custom validation rules
validation_strategy (str) – ‘strict’, ‘warn’, ‘filter’
report_violations (bool) – Whether to report validation violations
max_violations (int | None) – Maximum allowed violations before error
- class RiskTableMappingProcessor(column_name, label_name, smooth_factor=0.0, count_threshold=0, risk_tables=None)[source]¶
Bases:
ProcessorA 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.
- 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.
Performance optimized: Uses fast path for single-value Series.
- class StreamingRiskTableProcessor(column_name, label_name, smooth_factor=0.0, count_threshold=0, risk_tables=None)[source]¶
Bases:
RiskTableMappingProcessorStreaming-aware risk table processor.
Extends RiskTableMappingProcessor to support fitting from PipelineIterableDataset by accumulating cross-tabulation counts incrementally.
Uses online cross-tabulation for exact computation: - Maintains category counts for each label value (0, 1) - Computes risk ratios with smoothing - Memory proportional to number of unique categories (not dataset size)
Examples
>>> # Create streaming processor >>> proc = StreamingRiskTableProcessor( ... column_name='customer_type', ... label_name='label', ... smooth_factor=0.1, ... count_threshold=5 ... ) >>> >>> # Fit from streaming dataset >>> proc.fit_streaming(train_iterable_dataset) >>> >>> # Use in pipeline (same API as base processor) >>> dataset.add_pipeline('customer_type', proc)
- fit(data)[source]¶
Fit risk tables from data.
Automatically detects input type and delegates to appropriate method: - IterableDataset: uses fit_streaming() - DataFrame: uses parent class fit()
- Parameters:
data (DataFrame | torch.utils.data.IterableDataset) – DataFrame or IterableDataset
- Returns:
self (for method chaining)
- Return type:
- fit_streaming(dataset, field_names=None, label_name=None, smooth_factor=None, count_threshold=None, max_samples=None, show_progress=False)[source]¶
Fit risk tables from streaming dataset.
Supports both single-field and multi-field (batch) fitting: - Single-field: Uses self.column_name, returns self for chaining - Multi-field: Processes all fields in ONE pass, returns Dict[field_name -> risk_tables]
- Parameters:
dataset (torch.utils.data.IterableDataset) – PipelineIterableDataset to stream from
field_names (List[str] | None) – Optional list of fields for batch fitting. If None, uses self.column_name
label_name (str | None) – Optional label name override. If None, uses self.label_name
smooth_factor (float | None) – Optional smoothing factor override. If None, uses self.smooth_factor
count_threshold (int | None) – Optional count threshold override. If None, uses self.count_threshold
max_samples (int | None) – Optional limit on samples processed
show_progress (bool) – Whether to show progress bar (for batch mode)
- Returns:
self (for method chaining) - Multi-field mode: Dict[field_name -> risk_tables_dict]
- Return type:
Single-field mode
- Raises:
RuntimeError – If no valid samples found
Modules
Categorical Imputation Processor for Missing Values |
|
Categorical Validation Processor for Data Quality Checks |
|
Dictionary Encoding Processor for Categorical Features |
|
Numerical Categorical Processor for Converting Numbers to Categories |
|
Streaming Risk Table Processor - IterableDataset Support |