Modeling_Tool.Sample¶
样本管理层 —— 切分、分层、均衡、拒绝推断、分布适配。
样本切分与采样 — Sample_Split¶
Sample_Split
¶
Sampling classes for credit modeling.
This module provides classes for splitting samples, stratified sampling, and sample balancing for credit model development.
类:
| 名称 | 描述 |
|---|---|
SampleSplitter : Split data into train/test samples. |
|
StratifiedSampler : Stratified sampling with target balance control. |
|
SampleBalancer : Balance samples using various techniques. |
|
示例:
>>> from Modeling_Tool_refactored.sample import SampleSplitter
>>> splitter = SampleSplitter()
>>> train, test = splitter.split(df, 'target', test_size=0.3)
SampleSplitter
¶
Split data into training and testing samples.
This class provides flexible sample splitting with support for stratification, random sampling, and custom split ratios.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
test_size
|
float
|
Proportion of data for testing (0 to 1). |
0.3
|
random_state
|
int
|
Random seed for reproducibility. |
None
|
stratify
|
bool
|
Whether to stratify by target variable. |
True
|
属性:
| 名称 | 类型 | 描述 |
|---|---|---|
train_index_ |
ndarray
|
Indices for training data. |
test_index_ |
ndarray
|
Indices for testing data. |
方法:
| 名称 | 描述 |
|---|---|
split |
Split data into train and test sets. |
split_df |
Split DataFrame while excluding certain columns. |
示例:
>>> splitter = SampleSplitter(test_size=0.2, random_state=42)
>>> train, test = splitter.split(X, y)
>>> print(f"Train size: {len(train)}, Test size: {len(test)}")
源代码位于: Modeling_Tool/Sample/Sample_Split.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 | |
split
¶
split(X: Union[DataFrame, ndarray], y: Union[Series, ndarray], test_size: Optional[float] = None, stratify: Optional[bool] = None) -> Tuple
Split data into train and test sets.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
X
|
DataFrame or ndarray
|
Features. |
必需 |
y
|
Series or ndarray
|
Target variable. |
必需 |
test_size
|
float
|
Override default test size. |
None
|
stratify
|
bool
|
Override default stratify setting. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple
|
(X_train, X_test, y_train, y_test) |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
split_df
¶
split_df(df: DataFrame, target: str, exclude_cols: Optional[List[str]] = None, test_size: Optional[float] = None) -> Tuple[DataFrame, DataFrame]
Split DataFrame while excluding certain columns from split.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame. |
必需 |
target
|
str
|
Target column name. |
必需 |
exclude_cols
|
list of str
|
Columns to exclude from split. |
None
|
test_size
|
float
|
Override default test size. |
None
|
返回:
| 类型 | 描述 |
|---|---|
tuple
|
(train_df, test_df) |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
StratifiedSampler
¶
Stratified sampling with target balance control.
This class provides stratified sampling that maintains the target distribution while allowing controlled sampling.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target_rate
|
float
|
Target bad rate in the sample. |
None
|
random_state
|
int
|
Random seed. |
None
|
方法:
| 名称 | 描述 |
|---|---|
sample |
Perform stratified sampling. |
balance |
Balance sample by adjusting target distribution. |
示例:
>>> sampler = StratifiedSampler(target_rate=0.15)
>>> balanced = sampler.balance(df, 'target', method='undersample')
源代码位于: Modeling_Tool/Sample/Sample_Split.py
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
sample
¶
sample(df: DataFrame, target: str, n_samples: Optional[int] = None, sample_frac: Optional[float] = None) -> DataFrame
Perform stratified sampling.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df
|
DataFrame
|
Input data. |
必需 |
target
|
str
|
Target column name. |
必需 |
n_samples
|
int
|
Number of samples to draw. |
None
|
sample_frac
|
float
|
Fraction of data to sample. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Sampled DataFrame. |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
balance
¶
Balance sample by adjusting target distribution.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df
|
DataFrame
|
Input data. |
必需 |
target
|
str
|
Target column name. |
必需 |
method
|
str
|
Balancing method: 'undersample', 'oversample', 'smote'. |
'undersample'
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Balanced DataFrame. |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
SampleBalancer
¶
Advanced sample balancing with multiple methods.
This class provides various sampling techniques to handle class imbalance in credit modeling.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
method
|
str
|
Balancing method. |
'random'
|
target_ratio
|
float
|
Desired minority/majority ratio. |
None
|
random_state
|
int
|
Random seed. |
None
|
方法:
| 名称 | 描述 |
|---|---|
fit_resample |
Resample features and target. |
get_balanced_indices |
Get indices for balanced sampling. |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
fit_resample
¶
Resample data to balance classes.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
X
|
DataFrame or ndarray
|
Features. |
必需 |
y
|
Series or ndarray
|
Target. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
tuple
|
(X_resampled, y_resampled) |
示例:
源代码位于: Modeling_Tool/Sample/Sample_Split.py
select_sample_seed
¶
select_sample_seed(master_df, oot_split_col, model, tgt_name, seed_range=(3000, 3050), ins_prop=0.7)
Select Best Seed for Sample Splitting.
源代码位于: Modeling_Tool/Sample/Sample_Split.py
拒绝推断 — Reject_Infer¶
Reject_Infer
¶
Reject inference classes for credit modeling.
This module provides classes for applying reject inference techniques to handle the selection bias in credit modeling when using approved loan data only.
类:
| 名称 | 描述 |
|---|---|
RejectInferrer : Base class for reject inference. |
|
RejectInferenceFactory : Factory for creating reject inference methods. |
|
ParcelingInferrer : Parceling method for reject inference. |
|
FuzzyAugmentInferrer : Fuzzy augmentation method. |
|
HardCutoffInferrer : Hard cutoff method. |
|
SimpleAugmentInferrer : Simple augmentation method. |
|
示例:
>>> from Modeling_Tool_refactored.sample import RejectInferrer
>>> inferrer = RejectInferenceFactory.create('parceling')
>>> df_inferred = inferrer.infer(df_approved, df_rejected, 'score')
RejectInferrer
¶
Bases: ABC
Abstract base class for reject inference methods.
Reject inference is used to address selection bias when building credit models on approved loans only.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
target_col
|
str
|
Name of the target column. |
'target'
|
score_col
|
str
|
Name of the score/probability column. |
'score'
|
方法:
| 名称 | 描述 |
|---|---|
infer |
Apply reject inference. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
infer
abstractmethod
¶
Apply reject inference.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df_approved
|
DataFrame
|
DataFrame with approved applications (has target). |
必需 |
df_rejected
|
DataFrame
|
DataFrame with rejected applications (no target). |
必需 |
score_col
|
str
|
Score column name. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Combined DataFrame with inferred targets for rejected applications. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
SimpleAugmentInferrer
¶
Bases: RejectInferrer
Simple augmentation reject inference method.
Assigns the average bad rate from approved applications to all rejected applications.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
bad_rate
|
float
|
Override bad rate to use. |
None
|
示例:
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
infer
¶
Apply simple augmentation.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df_approved
|
DataFrame
|
Approved applications. |
必需 |
df_rejected
|
DataFrame
|
Rejected applications. |
必需 |
score_col
|
str
|
Score column. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Combined data with inferred targets. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
HardCutoffInferrer
¶
Bases: RejectInferrer
Hard cutoff reject inference method.
Assigns all rejected applications below a score threshold as bad (target=1), and all above as good (target=0).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
cutoff
|
float
|
Score cutoff threshold. |
0.5
|
示例:
>>> inferrer = HardCutoffInferrer(cutoff=0.3)
>>> df_combined = inferrer.infer(df_approved, df_rejected, 'probability')
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
infer
¶
Apply hard cutoff inference.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df_approved
|
DataFrame
|
Approved applications. |
必需 |
df_rejected
|
DataFrame
|
Rejected applications. |
必需 |
score_col
|
str
|
Score column. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Combined data with inferred targets. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
FuzzyAugmentInferrer
¶
Bases: RejectInferrer
Fuzzy augmentation reject inference method.
Weights approved applications based on their predicted probability and creates pseudo-target values for rejected applications.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
weight_factor
|
float
|
Factor to adjust weights. |
1.0
|
示例:
>>> inferrer = FuzzyAugmentInferrer(weight_factor=0.9)
>>> df_combined = inferrer.infer(df_approved, df_rejected, 'probability')
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
infer
¶
Apply fuzzy augmentation.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df_approved
|
DataFrame
|
Approved applications. |
必需 |
df_rejected
|
DataFrame
|
Rejected applications. |
必需 |
score_col
|
str
|
Score column. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Combined data with inferred targets. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
ParcelingInferrer
¶
Bases: RejectInferrer
Parceling reject inference method.
Splits rejected applications into parcels based on score bands and assigns average bad rate from approved applications in each parcel.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
n_parcels
|
int
|
Number of score parcels. |
10
|
示例:
>>> inferrer = ParcelingInferrer(n_parcels=5)
>>> df_combined = inferrer.infer(df_approved, df_rejected, 'score')
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
infer
¶
Apply parceling inference.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
df_approved
|
DataFrame
|
Approved applications. |
必需 |
df_rejected
|
DataFrame
|
Rejected applications. |
必需 |
score_col
|
str
|
Score column. |
None
|
返回:
| 类型 | 描述 |
|---|---|
DataFrame
|
Combined data with inferred targets. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
RejectInferenceFactory
¶
Factory class for creating reject inference methods.
示例:
>>> inferrer = RejectInferenceFactory.create('parceling', n_parcels=5)
>>> inferrer = RejectInferenceFactory.create('fuzzy', weight_factor=0.9)
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
create
classmethod
¶
create(method: str = 'parceling', **kwargs) -> RejectInferrer
Create a reject inference method.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
method
|
str
|
Method name. |
'parceling'
|
**kwargs
|
Additional parameters for the method. |
{}
|
返回:
| 类型 | 描述 |
|---|---|
RejectInferrer
|
Instantiated reject inferrer. |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
If method name is not recognized. |
源代码位于: Modeling_Tool/Sample/Reject_Infer.py
available_methods
classmethod
¶
Get list of available methods.
返回:
| 类型 | 描述 |
|---|---|
list of str
|
Available method names. |
分布适配 — Distribution_Adaptation¶
Distribution_Adaptation
¶
DistributionAdaptation
¶
源代码位于: Modeling_Tool/Sample/Distribution_Adaptation.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
estimate_density_ratio
¶
使用密度比估计方法计算样本权重 KLIEP/KMM等方法的简化实现
源代码位于: Modeling_Tool/Sample/Distribution_Adaptation.py
covariate_shift_weighting
¶
使用领域分类器估计样本重要性权重
源代码位于: Modeling_Tool/Sample/Distribution_Adaptation.py
fit
¶
计算适应OOT分布的样本权重
源代码位于: Modeling_Tool/Sample/Distribution_Adaptation.py
get_weights
¶
visualize_distribution_comparison
¶
可视化训练集和OOT集的分布差异