config¶
Configuration classes for Statistical Context Engineering.
config
¶
@module: sce.config @depends: pandas @exports: AggregationMethod, CleanupConfig, ContextConfig, detect_categorical_columns @paper_ref: Section 3.1 @data_flow: dataset schema + user options -> validated config -> grouping rules
AggregationMethod
¶
Bases: Enum
Statistical aggregation methods for context features.
Paper Section 3.1: "means, medians, dispersion measures, quantiles, counts, and relative deviations"
Source code in sce/config.py
CleanupConfig
dataclass
¶
Configuration for feature cleanup pipeline.
Source code in sce/config.py
ContextConfig
dataclass
¶
Configuration for Statistical Context Engineering.
Supports two modes:
1. Auto-detection mode (recommended): Set categorical_cols=None and the engine
will auto-detect categorical columns from the DataFrame.
2. Manual mode: Specify categorical_cols explicitly.
Attributes:
| Name | Type | Description |
|---|---|---|
target_col |
str
|
Name of the target variable column (REQUIRED) |
categorical_cols |
Optional[List[str]]
|
List of categorical columns for grouping. If None, auto-detected. |
min_categorical_columns |
int
|
Minimum number of categorical columns required to run SCE |
aggregations |
List[AggregationMethod]
|
List of aggregation methods to apply |
min_group_size |
int
|
Minimum samples required per group |
use_cross_fitting |
bool
|
Whether to apply out-of-fold aggregation (prevents leakage) |
cross_fit_strategy |
Literal['random', 'time', 'rolling', 'off']
|
Cross-fit splitter strategy (random/time/rolling/off) |
time_col |
Optional[str]
|
Time column required for temporal cross-fitting |
n_folds |
int
|
Number of folds for cross-fitting |
random_state |
int
|
Seed for randomized splitters |
rolling_max_train_size |
Optional[int]
|
Max rolling train window size (rows), None = expanding |
rolling_test_size |
Optional[int]
|
Rolling validation window size (rows), None = sklearn default |
rolling_gap |
int
|
Gap between rolling train and validation windows |
include_fold_variance |
bool
|
Whether to add fold-variance uncertainty features |
fold_variance_features |
List[str]
|
Which variance features to include (std/lower/upper/cv) |
include_relative_features |
bool
|
Whether to compute z-score/ratio features. WARNING: These features use y_t (the target value) in their formula, causing direct target leakage. Only enable for post-hoc analysis. |
include_global_stats |
bool
|
Whether to include dataset-wide global statistics |
include_interactions |
bool
|
Whether to compute 2-way categorical interactions |
max_interaction_depth |
int
|
Maximum number of columns to combine (2 = pairs only) |
max_cardinality |
int
|
For auto-detection, max unique values to consider categorical |
exclude_cols |
List[str]
|
Columns to exclude from auto-detection |
add_backoff_depth |
bool
|
Whether to add backoff depth features |
cleanup_config |
Optional[CleanupConfig]
|
Optional feature cleanup configuration |
Example (auto-detection): config = ContextConfig( target_col="price", include_interactions=True ) # Categorical columns detected automatically from DataFrame
Example (manual): config = ContextConfig( target_col="price", categorical_cols=["city", "room_type", "is_superhost"], include_interactions=True )
Source code in sce/config.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
__post_init__
¶
Validate configuration parameters.
Source code in sce/config.py
get_categorical_cols
¶
Get categorical columns for grouping.
If categorical_cols is None, auto-detects from DataFrame. Otherwise returns the specified columns (filtered to those that exist).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List of categorical column names |
Source code in sce/config.py
detect_categorical_columns
¶
detect_categorical_columns(df: DataFrame, target_col: str, max_cardinality: int = 100, min_cardinality: int = 2, exclude_cols: Optional[List[str]] = None) -> List[str]
Auto-detect categorical columns suitable for SCE grouping.
Detection rules: 1. Object or category dtype → categorical 2. Boolean dtype → categorical 3. Integer with low cardinality (≤ max_cardinality) → likely categorical 4. Exclude target column and any specified exclusions 5. Exclude columns with only 1 unique value (no variance)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame |
required |
target_col
|
str
|
Target column name (will be excluded) |
required |
max_cardinality
|
int
|
Maximum unique values for a column to be considered categorical |
100
|
min_cardinality
|
int
|
Minimum unique values (must have at least 2 groups) |
2
|
exclude_cols
|
Optional[List[str]]
|
Additional columns to exclude |
None
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of detected categorical column names |
Example
categoricals = detect_categorical_columns(df, target_col="price") print(categoricals) ['city', 'room_type', 'property_type', 'is_superhost']