Classify & Count (CC)
quack.quantifiers._baselines.CC
Bases: BaseQuantifier
Classify and Count (CC) quantifier.
The CC method is the simplest baseline in quantification. It works by classifying all unlabeled instances in the test bag using a hard (crisp) classifier, and then computing the relative frequency (prevalence) of each class based on those predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification.
If None, an instance of |
= None
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during training, sorted ascending
(as returned by |
n_classes_ |
int
|
The number of distinct classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The prevalence of each class in the training dataset. |
y_prevs_ |
ndarray of shape (n_classes,)
|
Alias for train_prevalence_ kept for backward compatibility. |
classifier_ |
estimator object
|
The fitted base classifier trained on the entire dataset. |
Notes
The Classify and Count method does not adjust for misclassifications made by the base classifier (false positives and false negatives). Therefore, its performance is highly dependent on the classification accuracy.
The estimated prevalence for class 'c' is given by the formula:
p_hat(c) = (1 / |X|) * sum( I( f(x) == c ) )
where f(x) is the crisp prediction of the classifier for instance x, and I() is the indicator function.
References
George Forman. Counting positives accurately despite inaccurate classification. In Proceedings of the 16th European Conference on Machine Learning, pages 564-575, Porto, Portugal, 2005.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = CC()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_baselines.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 | |
fit(X, y)
Adjusts the CC quantifier by fitting the base classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. |
array-like
|
y
|
array-like of shape (n_samples,)
|
Labels for the corresponding classes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the fitted estimator instance itself. |
Source code in quack/quantifiers/_baselines.py
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 | |
predict(X)
Estimate the class prevalences for the test bag X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
The test bag with unlabelled instances. |
array-like
|
Returns:
| Name | Type | Description |
|---|---|---|
p_adjusted |
ndarray of shape (n_classes,)
|
An array with the estimated prevalences for each class, normalized to sum up to 1.0. |
Source code in quack/quantifiers/_baselines.py
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 | |
Adjusted Classify & Count (ACC)
quack.quantifiers._baselines.ACC
Bases: BaseCalibratedQuantifier
Adjusted Classify and Count (ACC) quantifier for binary problems.
ACC is a calibrated quantification method that adjusts the simple Classify and Count (CC) estimates. It uses Out-of-Fold (OOF) predictions to calculate the True Positive Rate (TPR) and False Positive Rate (FPR) of the base classifier. Then, it mathematically corrects the final prevalence using the classical binary adjustment formula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification.
If None, an instance of |
None
|
cv
|
int, cross-validation generator or an iterable
|
Determines the cross-validation splitting strategy to generate the Out-of-Fold predictions used for calibration. |
= 10
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (2,)
|
The distinct class labels found during training. |
n_classes_ |
int
|
The number of distinct classes (expected to be 2). |
train_prevalence_ |
ndarray of shape (2,)
|
The prevalence of each class in the training dataset. |
tpr_ |
float
|
The True Positive Rate (Sensitivity) estimated out-of-fold. |
fpr_ |
float
|
The False Positive Rate (1 - Specificity) estimated out-of-fold. |
classifier_ |
estimator object
|
The fitted base classifier trained on the entire dataset. |
Notes
The ACC method adjusts the observed crisp prevalence (p_pred) using the following analytical equation for the positive class:
p_adjusted = (p_pred - fpr) / (tpr - fpr)
The prevalence for the negative class is then derived as 1 - p_corrected. If TPR equals FPR, the denominator becomes zero, and the method falls back to the unadjusted p_pred.
References
George Forman. Counting positives accurately despite inaccurate classification. In Proceedings of the 16th European Conference on Machine Learning, pages 564-575, Porto, Portugal, 2005.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = ACC()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_baselines.py
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
fit(X, y)
Fits the ACC quantifier, enforcing the binary-only constraint.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in quack/quantifiers/_baselines.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
Probabilistic Classify & Count (PCC)
quack.quantifiers._baselines.PCC
Bases: BaseQuantifier
Probabilistic Classify and Count (PCC) quantifier.
The PCC method extends the classical Classify and Count (CC) by using the posterior probabilities generated by a classifier instead of its crisp (hard) predictions. The estimated prevalence for each class is computed as the expected value (average) of the predicted probabilities across all instances in the test bag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification.
Must implement the |
= None
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during training. |
n_classes_ |
int
|
The number of distinct classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The prevalence of each class in the training dataset. |
y_prevs_ |
ndarray of shape (n_classes,)
|
Alias for train_prevalence_ kept for backward compatibility. |
classifier_ |
estimator object
|
The fitted base classifier trained on the entire dataset. |
Notes
Unlike CC, PCC accounts for the classifier's confidence in its predictions, which often leads to better quantification performance, especially when the classifier is well-calibrated. However, like CC, it is an "unadjusted" method, meaning it does not explicitly correct for systematic classification errors via a confusion matrix.
The estimated prevalence for class 'c' is given by the formula:
p_hat(c) = (1 / |X|) * sum( P(c | x) )
where P(c | x) is the probability that instance x belongs to class c,
estimated by the base classifier's predict_proba method.
References
Antonio Bella, Cesar Ferri, José Hernández-Orallo, and María José Ramírez-Quintana. Quantification via probability estimators. In 2010 IEEE International Conference on Data Mining, pages 737-742, Sydney, Australia, 2010.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = PCC()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_baselines.py
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 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 | |
fit(X, y)
Adjusts the PCC quantifier by fitting the probabilistic base classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
Training data. |
array-like
|
y
|
array-like of shape (n_samples,)
|
Labels for the corresponding classes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the fitted estimator instance itself. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided classifier does not support probability estimation
(i.e., lacks a |
Source code in quack/quantifiers/_baselines.py
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 | |
predict(X)
Estimate the class prevalences for the test bag X using soft probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
array-like, sparse matrix
|
The test bag with unlabelled instances. |
array-like
|
Returns:
| Name | Type | Description |
|---|---|---|
p_adjusted |
ndarray of shape (n_classes,)
|
An array with the estimated prevalences for each class, normalized to sum up to 1.0. |
Source code in quack/quantifiers/_baselines.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
Probabilistic Adjusted Classify & Count (PACC)
quack.quantifiers._baselines.PACC
Bases: BaseCalibratedQuantifier
Probabilistic Adjusted Classify and Count (PACC) quantifier for binary problems.
PACC is a calibrated quantification method that refines the Probabilistic Classify and Count (PCC) by correcting for the base classifier's systematic probabilistic bias. Using Out-of-Fold (OOF) predictions, it computes the expected predicted probability for the positive class given the true class labels. It then applies an analytical correction formula similar to ACC but entirely based on continuous probability scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification.
Must implement the |
= None
|
cv
|
int, cross-validation generator or an iterable
|
Determines the cross-validation splitting strategy to generate the Out-of-Fold predictions used for calibration. |
= 10
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (2,)
|
The distinct class labels found during training. |
n_classes_ |
int
|
The number of distinct classes (expected to be 2). |
train_prevalence_ |
ndarray of shape (2,)
|
The prevalence of each class in the training dataset. |
y_prevs_ |
ndarray of shape (2,)
|
Alias for train_prevalence_ kept for backward compatibility. |
mu_pos_pos_ |
float
|
The mean probability assigned to the positive class for instances that are truly positive, estimated out-of-fold. Analogue to TPR. |
mu_neg_pos_ |
float
|
The mean probability assigned to the positive class for instances that are truly negative, estimated out-of-fold. Analogue to FPR. |
classifier_ |
estimator object
|
The fitted base classifier trained on the entire dataset. |
Notes
The PACC method adjusts the observed probabilistic prevalence (p_pcc) using the following analytical equation for the positive class:
p_corrected = (p_pcc - mu_neg_pos) / (mu_pos_pos - mu_neg_pos)
where 'p_pcc' is the average predicted probability of the positive class in the test bag. The prevalence for the negative class is then derived as 1 - p_corrected. If the denominator evaluates to zero, the method falls back to the unadjusted p_pcc.
References
Antonio Bella, Cesar Ferri, José Hernández-Orallo, and María José Ramírez-Quintana. Quantification via probability estimators. In 2010 IEEE International Conference on Data Mining, pages 737-742, Sydney, Australia, 2010.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = PACC()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_baselines.py
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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | |
fit(X, y)
Fits the PACC quantifier, validating classifier and class-count constraints.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided classifier does not support probability estimation. |
ValueError
|
If |
Source code in quack/quantifiers/_baselines.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
Generalized Adjusted Classify & Count (GAC)
quack.quantifiers._dmm.GAC
Bases: BaseCalibratedQuantifier, BaseMixtureQuantifier
Generalized Adjusting Confusion Matrix (GAC) Quantifier.
A distance-minimizing multi-class generalization of the Adjusting Count (AC) algorithm that optimizes target distributions across the discrete labels confusion matrix profile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier. Defaults to |
= LogisticRegression
|
distance_metric
|
str
|
The distance metric minimized. |
= 'L2'
|
cv
|
int
|
The number of cross-validation folds. |
= 10
|
use_convex_solver
|
bool
|
If True, optimizes via CVXPY. |
= True
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Aykut Firat. Unified framework for quantification. arXiv preprint arXiv:1606.00868, 2016.
Source code in quack/quantifiers/_dmm.py
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 | |
Generalized Probabilistic Adjusted Classify & Count (GPAC)
quack.quantifiers._dmm.GPAC
Bases: BaseCalibratedQuantifier, BaseMixtureQuantifier
Generalized Probabilistic Adjusting Confusion Matrix (GPAC) Quantifier.
A distance-minimizing multi-class generalization of the Probabilistic Adjusting Count (PAC) algorithm that optimizes target distributions using soft-probability profiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier. Defaults to |
= LogisticRegression
|
distance_metric
|
str
|
The distance metric minimized. |
= 'L2'
|
cv
|
int
|
The number of cross-validation folds. |
= 10
|
use_convex_solver
|
bool
|
If True, optimizes via CVXPY. |
= True
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Aykut Firat. Unified framework for quantification. arXiv preprint arXiv:1606.00868, 2016.
Source code in quack/quantifiers/_dmm.py
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 | |
Friedman's Method (FM)
quack.quantifiers._dmm.FM
Bases: BaseCalibratedQuantifier, BaseMixtureQuantifier
Friedman's Method (FM) Quantifier.
An adjusting prediction mixture model that maps soft classifier probabilities into binary indicator matrices by comparing them against baseline training priors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier. Defaults to |
= LogisticRegression
|
distance_metric
|
str
|
The distance metric minimized. |
= 'L2'
|
cv
|
int
|
The number of cross-validation folds. |
= 10
|
use_convex_solver
|
bool
|
If True, optimizes via CVXPY. |
= True
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Jerome H. Friedman. Class counts in future unlabeled samples, 2014. Presentation at MIT CSAIL Big Data Event.
Source code in quack/quantifiers/_dmm.py
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 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 | |
Threshold Selector X (X)
quack.quantifiers._threshold.X
Bases: BaseThresholdQuantifier
Forman's X threshold selection quantifier matching QFY's TSX strategy.
This method selects the optimal threshold from the dynamic grid that minimizes the absolute distance |TPR - (1 - FPR)|, searching for the intersection point in the ROC curve where TPR + FPR ~= 1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification. |
= None
|
cv
|
int
|
Determines the cross-validation splitting strategy. |
= 10
|
precision
|
int
|
The decimal precision used to round the Out-of-Fold probabilities. |
= 3
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Forman, G. (2008). Quantifying counts and costs via classification. Data Mining and Knowledge Discovery, 17(2):164-206.
Examples:
>>> from sklearn.datasets import make_classification
>>> X_, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = X()
>>> quantifier.fit(X_, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_threshold.py
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 | |
Threshold Selector MAX (Max)
quack.quantifiers._threshold.Max
Bases: BaseThresholdQuantifier
Forman's Max threshold selection quantifier matching QFY's TSMax strategy.
This method selects the threshold that maximizes the separation split between classes, maximizing the absolute difference |TPR - FPR| to achieve the highest denominator stability during shift adjustment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification. |
= None
|
cv
|
int
|
Determines the cross-validation splitting strategy. |
= 10
|
precision
|
int
|
The decimal precision used to round the Out-of-Fold probabilities. |
= 3
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Forman, G. (2008). Quantifying counts and costs via classification. Data Mining and Knowledge Discovery, 17(2):164-206.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = Max()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_threshold.py
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 | |
Threshold Selector 50 (T50)
quack.quantifiers._threshold.T50
Bases: BaseThresholdQuantifier
Forman's Threshold 50 quantifier matching QFY's TS50 strategy.
This method selects the threshold that is closest to achieving a True Positive Rate (TPR) of exactly 0.5, minimizing the cost function |TPR - 0.5| across the candidate grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification. |
= None
|
cv
|
int
|
Determines the cross-validation splitting strategy. |
= 10
|
precision
|
int
|
The decimal precision used to round the Out-of-Fold probabilities. |
= 3
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Forman, G. (2008). Quantifying counts and costs via classification. Data Mining and Knowledge Discovery, 17(2):164-206.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = T50()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_threshold.py
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 | |
MedianSweep
quack.quantifiers._threshold.MedianSweep
Bases: BaseThresholdQuantifier
Forman's Median Sweep (MS) quantifier faithful to QFY's implementation.
Median Sweep evaluates individual ACC adjustments across all candidates in the
dynamic grid. It enforces a strict filter where thresholds with a denominator
(TPR - FPR) smaller than delta_min are ignored.
If no thresholds satisfy the filter, it falls back to the estimate with the
largest available separation. Otherwise, it returns the robust median of all
valid predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The classifier to be used as the base for quantification. |
None
|
cv
|
int
|
Determines the cross-validation splitting strategy. |
10
|
precision
|
int
|
The decimal precision used to round the Out-of-Fold probabilities. |
3
|
delta_min
|
float
|
The minimum required threshold separation (TPR - FPR) to accept an individual ACC adaptation, avoiding denominator instability. |
0.25
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Notes
The fallback path (used only when no threshold satisfies
delta > delta_min) picks the threshold with the largest delta among
all candidates: as long as no threshold is ever accepted as valid,
every candidate is a fallback contender, so the largest-delta
candidate found while sweeping the whole grid is exactly the one a
sequential scan would have kept until the end.
References
Forman, G. (2008). Quantifying counts and costs via classification. Data Mining and Knowledge Discovery, 17(2):164-206.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
>>> quantifier = MedianSweep()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=200, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
>>> print(prevalences)
Source code in quack/quantifiers/_threshold.py
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 | |
HDx
quack.quantifiers._features.HDx
Bases: BaseMixtureQuantifier
Hellinger Distance x (HDx) quantifier.
HDx is a non-parametric feature-space mixture model that operates directly on categorical or discretized continuous features without training an underlying classifier. It projects each feature column independently, constructs a global marginal conditional probability matrix during training, and minimizes the Hellinger Distance to estimate the test class prevalences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_convex_solver
|
bool
|
If True, attempts to solve the statistical mixture distribution using |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The baseline prevalence proportion of each class observed in the training data. |
feature_spaces_ |
list of ndarray
|
A list of length |
conditional_matrix_ |
ndarray of shape (n_total_unique_bins, n_classes)
|
The stacked conditional probability matrix built during the |
References
Víctor González-Castro, Rocío Alaiz-Rodríguez, and Enrique Alegre. Class distribution estimation based on the Hellinger distance. Information Sciences, 218(1):146-164, 2013
Source code in quack/quantifiers/_features.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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
fit(X, y)
Fits the HDx mixture model by building the marginal conditional matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray of shape (n_samples, n_features)
|
The training feature matrix. |
required |
y
|
ndarray of shape (n_samples,)
|
The target class labels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the instance itself. |
Source code in quack/quantifiers/_features.py
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 | |
ReadMe
quack.quantifiers._features.ReadMe
Bases: BaseQuantifier
ReadMe Ensemble Quantifier.
ReadMe is an ensemble mixture model specifically designed for high-dimensional categorical data or short text analysis (e.g., Bag-of-Words). It circumvents the curse of dimensionality by training multiple independent sub-space mixture models over randomized feature subsets, obtaining final test prevalences by averaging individual predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distance_metric
|
str
|
The distance metric minimized by internal sub-quantifiers ('L1', 'L2', 'HD', 'TS'). |
'L2'
|
use_convex_solver
|
bool
|
If True, internal sub-quantifiers utilize |
True
|
n_features
|
int
|
Number of random features selected per subset. If None, it automatically
defaults to |
None
|
n_subsets
|
int
|
The total number of random subspace sub-quantifiers to ensemble. |
100
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting/predicting the
|
= None
|
parallel_backend
|
str
|
|
= "loky"
|
random_state
|
int, RandomState instance or None
|
Controls the randomness of the per-subset feature selection. Pass an
int for reproducible subspaces across repeated |
= None
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The baseline prevalence proportion of each class observed in the training data. |
feature_subsets_ |
list of ndarray
|
A list containing the chosen feature column indices for each random subset. |
sub_quantifiers_ |
list of _RawSubspaceMixture
|
The collection of fitted internal mixture models making up the ensemble. |
References
Hopkins, D., & King, G. (2010). A method of automated nonparametric content analysis for social science. American Journal of Political Science, 54(1), 229-247.
Source code in quack/quantifiers/_features.py
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 386 387 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 | |
fit(X, y)
Fits the ReadMe ensemble by training multiple subspace mixture models.
The n_subsets sub-quantifiers are mutually independent, so they are
dispatched as independent joblib jobs (see n_jobs/parallel_backend)
instead of a sequential Python loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray of shape (n_samples, n_features)
|
The training feature matrix. |
required |
y
|
ndarray of shape (n_samples,)
|
The target class labels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the instance itself. |
Source code in quack/quantifiers/_features.py
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 407 408 | |
predict(X)
Estimates class prevalences by averaging sub-quantifier predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray of shape (n_samples, n_features)
|
The testing feature matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
ensemble_prevalences |
ndarray of shape (n_classes,)
|
The final aggregated and normalized prevalence estimation vector. |
Source code in quack/quantifiers/_features.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
Energy Distance (ED)
quack.quantifiers._probabilities.ED
Bases: BaseQuantifier
Energy Distance Minimization (ED) Quantifier.
A non-parametric, feature-space mixture model that estimates target class prevalences by minimizing the Energy Distance divergence between the joint training distributions and the unlabelled test batch. It uses an exact analytical solution for binary settings and a quadratic programming solver (via CVXPY) for multiclass problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_jobs
|
int
|
Number of jobs to run in parallel while computing the pairwise-distance
sums that make up |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes. |
train_class_samples_ |
list of ndarray
|
A list of length |
class_distances_matrix_ |
ndarray of shape (n_classes, n_classes)
|
Matrix 'A' representing the expected cross-class average pairwise distances calculated across the training data subsets. |
quadratic_matrix_ |
ndarray of shape (n_classes - 1, n_classes - 1)
|
Matrix 'B' storing the transformed quadratic form coefficients used to
solve multiclass optimization steps. Only populated if |
References
Hideko Kawakubo, Marthinus Christoffel du Plessis, and Masashi Sugiyama. Computationally efficient class-prior estimation under class balance change using energy distance. IEICE Transactions on Information and Systems, 99(1):176-186, 2016.
Examples:
>>> from sklearn.datasets import make_classification
>>> X, y = make_classification(n_samples=500, n_classes=2, random_state=0)
>>> quantifier = ED()
>>> quantifier.fit(X, y)
>>> X_test, _ = make_classification(n_samples=100, n_classes=2, random_state=7)
>>> prevalences = quantifier.predict(X_test)
Source code in quack/quantifiers/_probabilities.py
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 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 | |
fit(X, y)
Fits the ED quantifier by computing expected intra-class pairwise distances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray of shape (n_samples, n_features)
|
The training feature matrix. |
required |
y
|
ndarray of shape (n_samples,)
|
The target class labels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
self |
object
|
Returns the instance itself. |
Source code in quack/quantifiers/_probabilities.py
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 | |
predict(X)
Estimates class prevalences for the given unlabelled test data batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray of shape (n_samples, n_features)
|
The testing data matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
final_prevalences |
ndarray of shape (n_classes,)
|
A normalized probability vector indicating the estimated prevalence proportions for each class. |
Source code in quack/quantifiers/_probabilities.py
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 | |
HDy
quack.quantifiers._dmm.HDy
Bases: DyS
Hellinger Distance y (HDy) Quantifier.
A specialized instance of the DyS framework that minimizes the Hellinger Distance over binned score histograms using a Logistic Regression classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier. Defaults to |
None
|
n_bins
|
int
|
The total number of histogram bins. |
10
|
cv
|
int
|
The number of cross-validation folds. |
10
|
use_convex_solver
|
bool
|
If True, optimizes via CVXPY. |
True
|
predict_proba
|
bool
|
If True, forces the model to use probabilistic |
False
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
References
Víctor González-Castro, Rocío Alaiz-Rodríguez, and Enrique Alegre. Class distribution estimation based on the Hellinger distance. Information Sciences, 218(1):146-164, 2013.
Source code in quack/quantifiers/_dmm.py
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 | |
Distribution y Similarity (DyS)
quack.quantifiers._dmm.DyS
Bases: BaseScoreMixtureQuantifier
Distribution y-Similarity (DyS) Quantifier.
An adjusting prediction mixture model built strictly for binary quantification. It partitions out-of-fold continuous classification scores into a specified number of histograms bins to match training and testing distributions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier. If None, defaults to |
None
|
distance_metric
|
str
|
The distance metric minimized ('L1', 'L2', 'HD', 'TS'). |
'TS'
|
n_bins
|
int
|
The total number of histogram bins used to slice the distribution profiles. |
10
|
cv
|
int
|
The number of cross-validation folds for out-of-fold scoring. |
10
|
use_convex_solver
|
bool
|
If True, optimizes via CVXPY. |
True
|
predict_proba
|
bool
|
If True, forces the model to use probabilistic |
False
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The baseline prevalence proportion of each class observed in the training data. |
conditional_matrix_ |
ndarray of shape (n_bins, n_classes)
|
The binned score conditional matrix built using out-of-fold calibration data. |
score_range_ |
tuple of float (min, max)
|
The minimum and maximum boundaries used to define histogram bins. |
References
André Maletzke, Denis dos Reis, Everton Cherman, and Gustavo Batista. DyS: A framework for mixture models in quantification. In Proceedings of the AAAI Conference on Artificial Intelligence, pages 4552-4560, Honolulu, Hawaii, 2019.
Source code in quack/quantifiers/_dmm.py
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 | |
Expectation Maximization Quantifier (EMQ)
quack.quantifiers._iterators.EM
Bases: BaseCalibratedQuantifier
Expectation Maximization (EM) Quantifier.
An iterative quantification algorithm that adapts a classifier's output probabilities to a target test set by maximizing the likelihood of the test data. It recursively updates sample posteriors and prior estimations until convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier implementing |
= None
|
cv
|
int, cross-validation generator or an iterable
|
Determines the cross-validation splitting strategy for the calibration phase. |
= 10
|
epsilon
|
float
|
The convergence tolerance threshold. The iterative loop terminates when the Euclidean norm between consecutive steps is smaller than this value. |
= 1e-06
|
max_iter
|
int
|
The maximum allowable optimization iterations. |
= 1000
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (n_classes,)
|
The distinct class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes. |
train_prevalence_ |
ndarray of shape (n_classes,)
|
The baseline prevalence proportion of each class observed in the training data. |
classifier_ |
estimator object
|
The final trained base classifier adjusted on the entire training dataset. |
References
Marco Saerens, Patrice Latinne, and Christine Decaestecker. Adjusting the outputs of a classifier to new a priori probabilities: A simple procedure. Neural Computation, 14(1): 21-41, 2002.
Source code in quack/quantifiers/_iterators.py
8 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 | |
Class Distribution Estimation (CDE)
quack.quantifiers._iterators.CDE
Bases: BaseCalibratedQuantifier
Class Conditional Density Estimation (CDE) Quantifier.
An iterative binary threshold-adjusting quantifier that modulates prediction cutoffs dynamically by evaluating relative target distribution shifts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
estimator object
|
The underlying base classifier implementing |
= None
|
cv
|
int, cross-validation generator or an iterable
|
Determines the cross-validation splitting strategy for the calibration phase. |
= 10
|
epsilon
|
float
|
The convergence tolerance threshold. |
= 1e-06
|
max_iter
|
int
|
The maximum allowable optimization iterations. |
= 1000
|
n_jobs
|
int
|
Number of jobs to run in parallel while fitting the |
= None
|
parallel_backend
|
str
|
|
= "loky"
|
Attributes:
| Name | Type | Description |
|---|---|---|
classes_ |
ndarray of shape (2,)
|
The binary class labels found during the training phase. |
n_classes_ |
int
|
The total number of unique classes (strictly equals 2). |
train_prevalence_ |
ndarray of shape (2,)
|
The baseline prevalence proportion of each class observed in the training data. |
classifier_ |
estimator object
|
The final trained base classifier adjusted on the entire training dataset. |
References
Dirk Tasche. Fisher consistency for prior probability shift. Journal of Machine Learning Research, 18(95):1-32, 2017.
Source code in quack/quantifiers/_iterators.py
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 | |