mlquantify 0.1.2__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mlquantify-0.1.3/MANIFEST.in +1 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/PKG-INFO +1 -1
- mlquantify-0.1.3/VERSION.txt +1 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/aggregative.py +130 -1
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/threshold_optimization.py +0 -151
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify.egg-info/PKG-INFO +1 -1
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify.egg-info/SOURCES.txt +2 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/setup.py +6 -6
- {mlquantify-0.1.2 → mlquantify-0.1.3}/README.md +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/__init__.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/base.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/classification/__init__.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/classification/methods.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/evaluation/__init__.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/evaluation/measures.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/evaluation/protocol.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/__init__.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/meta.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/mixture_models.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/methods/non_aggregative.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/model_selection.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/plots.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/utils/__init__.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/utils/general.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify/utils/method.py +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify.egg-info/dependency_links.txt +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify.egg-info/requires.txt +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/mlquantify.egg-info/top_level.txt +0 -0
- {mlquantify-0.1.2 → mlquantify-0.1.3}/setup.cfg +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include VERSION.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.3
|
|
@@ -907,10 +907,140 @@ class PCC(AggregativeQuantifier):
|
|
|
907
907
|
|
|
908
908
|
|
|
909
909
|
|
|
910
|
+
class PACC(AggregativeQuantifier):
|
|
911
|
+
"""
|
|
912
|
+
Probabilistic Adjusted Classify and Count (PACC).
|
|
913
|
+
This method extends the Adjusted Classify and Count (AC) approach
|
|
914
|
+
by leveraging the average class-conditional confidences obtained
|
|
915
|
+
from a probabilistic classifier instead of relying solely on true
|
|
916
|
+
positive and false positive rates.
|
|
917
|
+
|
|
918
|
+
Parameters
|
|
919
|
+
----------
|
|
920
|
+
learner : BaseEstimator
|
|
921
|
+
A scikit-learn compatible classifier to be used for quantification.
|
|
922
|
+
threshold : float, optional
|
|
923
|
+
The decision threshold for classification. Default is 0.5.
|
|
924
|
+
|
|
925
|
+
Attributes
|
|
926
|
+
----------
|
|
927
|
+
learner : BaseEstimator
|
|
928
|
+
A scikit-learn compatible classifier.
|
|
929
|
+
threshold : float
|
|
930
|
+
Decision threshold for classification. Default is 0.5.
|
|
931
|
+
tpr : float
|
|
932
|
+
True positive rate computed during the fitting process.
|
|
933
|
+
fpr : float
|
|
934
|
+
False positive rate computed during the fitting process.
|
|
935
|
+
|
|
936
|
+
See Also
|
|
937
|
+
--------
|
|
938
|
+
ThresholdOptimization : Base class for threshold-based quantification methods.
|
|
939
|
+
ACC : Adjusted Classify and Count quantification method.
|
|
940
|
+
CC : Classify and Count quantification method.
|
|
941
|
+
|
|
942
|
+
References
|
|
943
|
+
----------
|
|
944
|
+
A. Bella, C. Ferri, J. Hernández-Orallo and M. J. Ramírez-Quintana, "Quantification via Probability Estimators," 2010 IEEE International Conference on Data Mining, Sydney, NSW, Australia, 2010, pp. 737-742, doi: 10.1109/ICDM.2010.75. Available at: https://ieeexplore.ieee.org/abstract/document/5694031
|
|
910
945
|
|
|
946
|
+
Examples
|
|
947
|
+
--------
|
|
948
|
+
>>> from mlquantify.methods.aggregative import PACC
|
|
949
|
+
>>> from mlquantify.utils.general import get_real_prev
|
|
950
|
+
>>> from sklearn.datasets import load_breast_cancer
|
|
951
|
+
>>> from sklearn.svm import SVC
|
|
952
|
+
>>> from sklearn.model_selection import train_test_split
|
|
953
|
+
>>>
|
|
954
|
+
>>> features, target = load_breast_cancer(return_X_y=True)
|
|
955
|
+
>>>
|
|
956
|
+
>>> X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
|
|
957
|
+
>>>
|
|
958
|
+
>>> pacc = PACC(learner=SVC(probability=True))
|
|
959
|
+
>>> pacc.fit(X_train, y_train)
|
|
960
|
+
>>> y_pred = pacc.predict(X_test)
|
|
961
|
+
>>> y_pred
|
|
962
|
+
{0: 0.4664886119311328, 1: 0.5335113880688672}
|
|
963
|
+
>>> get_real_prev(y_test)
|
|
964
|
+
{0: 0.3991228070175439, 1: 0.6008771929824561}
|
|
965
|
+
"""
|
|
911
966
|
|
|
967
|
+
def __init__(self, learner: BaseEstimator=None, threshold: float = 0.5):
|
|
968
|
+
self.learner = learner
|
|
969
|
+
self.threshold = threshold
|
|
970
|
+
self.mean_pos = None
|
|
971
|
+
self.mean_neg = None
|
|
972
|
+
|
|
973
|
+
@property
|
|
974
|
+
def is_probabilistic(self) -> bool:
|
|
975
|
+
return True
|
|
976
|
+
|
|
977
|
+
@property
|
|
978
|
+
def is_multiclass(self) -> bool:
|
|
979
|
+
return False
|
|
912
980
|
|
|
981
|
+
def _fit_method(self, X, y):
|
|
982
|
+
# Get predicted labels and probabilities
|
|
983
|
+
if mq.arguments["y_labels"] is not None and mq.arguments["posteriors_train"] is not None:
|
|
984
|
+
y_labels = mq.arguments["y_labels"]
|
|
985
|
+
probabilities = mq.arguments["posteriors_train"]
|
|
986
|
+
else:
|
|
987
|
+
y_labels, probabilities = get_scores(X, y, self.learner, self.cv_folds, self.learner_fitted)
|
|
988
|
+
|
|
989
|
+
# Adjust thresholds and compute true and false positive rates
|
|
990
|
+
|
|
991
|
+
self.mean_pos = np.mean(probabilities[y_labels == self.classes[1], 1])
|
|
992
|
+
self.mean_neg = np.mean(probabilities[y_labels != self.classes[1], 1])
|
|
993
|
+
|
|
994
|
+
return self
|
|
995
|
+
|
|
996
|
+
|
|
997
|
+
def _predict_method(self, X):
|
|
998
|
+
"""
|
|
999
|
+
Predicts the class prevalence using the mean class-conditional
|
|
1000
|
+
probabilities from a probabilistic classifier.
|
|
913
1001
|
|
|
1002
|
+
Parameters
|
|
1003
|
+
----------
|
|
1004
|
+
X : array-like or sparse matrix of shape (n_samples, n_features)
|
|
1005
|
+
The input data for prediction.
|
|
1006
|
+
|
|
1007
|
+
Returns
|
|
1008
|
+
-------
|
|
1009
|
+
dict
|
|
1010
|
+
A dictionary with class labels as keys and their respective
|
|
1011
|
+
prevalence estimates as values.
|
|
1012
|
+
|
|
1013
|
+
Notes
|
|
1014
|
+
-----
|
|
1015
|
+
The prevalence is adjusted using the formula:
|
|
1016
|
+
prevalence = |mean_score - FPR| / (TPR - FPR),
|
|
1017
|
+
where mean_score is the average probability for the positive class.
|
|
1018
|
+
|
|
1019
|
+
Raises
|
|
1020
|
+
------
|
|
1021
|
+
ZeroDivisionError
|
|
1022
|
+
If `TPR - FPR` equals zero, indicating that the classifier's
|
|
1023
|
+
performance does not vary across the threshold range.
|
|
1024
|
+
"""
|
|
1025
|
+
prevalences = {}
|
|
1026
|
+
|
|
1027
|
+
# Calculate probabilities for the positive class
|
|
1028
|
+
probabilities = self.predict_learner(X)[:, 1]
|
|
1029
|
+
|
|
1030
|
+
# Compute the mean score for the positive class
|
|
1031
|
+
mean_scores = np.mean(probabilities)
|
|
1032
|
+
|
|
1033
|
+
# Adjust prevalence based on TPR and FPR
|
|
1034
|
+
if self.mean_pos - self.mean_neg == 0:
|
|
1035
|
+
prevalence = mean_scores
|
|
1036
|
+
else:
|
|
1037
|
+
prevalence = np.clip(abs(mean_scores - self.mean_neg) / (self.mean_pos - self.mean_neg), 0, 1)
|
|
1038
|
+
|
|
1039
|
+
# Map the computed prevalence to the class labels
|
|
1040
|
+
prevalences[self.classes[0]] = 1 - prevalence
|
|
1041
|
+
prevalences[self.classes[1]] = prevalence
|
|
1042
|
+
|
|
1043
|
+
return prevalences
|
|
914
1044
|
|
|
915
1045
|
|
|
916
1046
|
class PWK(AggregativeQuantifier):
|
|
@@ -1012,7 +1142,6 @@ class PWK(AggregativeQuantifier):
|
|
|
1012
1142
|
from . import threshold_optimization
|
|
1013
1143
|
|
|
1014
1144
|
ACC = threshold_optimization.ACC
|
|
1015
|
-
PACC = threshold_optimization.PACC
|
|
1016
1145
|
T50 = threshold_optimization.T50
|
|
1017
1146
|
MAX = threshold_optimization.MAX
|
|
1018
1147
|
X_method = threshold_optimization.X_method
|
|
@@ -659,157 +659,6 @@ class MS2(ThresholdOptimization):
|
|
|
659
659
|
|
|
660
660
|
return np.asarray(prevalences)
|
|
661
661
|
|
|
662
|
-
class PACC(ThresholdOptimization):
|
|
663
|
-
"""
|
|
664
|
-
Probabilistic Adjusted Classify and Count (PACC).
|
|
665
|
-
This method extends the Adjusted Classify and Count (AC) approach
|
|
666
|
-
by leveraging the average class-conditional confidences obtained
|
|
667
|
-
from a probabilistic classifier instead of relying solely on true
|
|
668
|
-
positive and false positive rates.
|
|
669
|
-
|
|
670
|
-
Parameters
|
|
671
|
-
----------
|
|
672
|
-
learner : BaseEstimator
|
|
673
|
-
A scikit-learn compatible classifier to be used for quantification.
|
|
674
|
-
threshold : float, optional
|
|
675
|
-
The decision threshold for classification. Default is 0.5.
|
|
676
|
-
|
|
677
|
-
Attributes
|
|
678
|
-
----------
|
|
679
|
-
learner : BaseEstimator
|
|
680
|
-
A scikit-learn compatible classifier.
|
|
681
|
-
threshold : float
|
|
682
|
-
Decision threshold for classification. Default is 0.5.
|
|
683
|
-
tpr : float
|
|
684
|
-
True positive rate computed during the fitting process.
|
|
685
|
-
fpr : float
|
|
686
|
-
False positive rate computed during the fitting process.
|
|
687
|
-
|
|
688
|
-
See Also
|
|
689
|
-
--------
|
|
690
|
-
ThresholdOptimization : Base class for threshold-based quantification methods.
|
|
691
|
-
ACC : Adjusted Classify and Count quantification method.
|
|
692
|
-
CC : Classify and Count quantification method.
|
|
693
|
-
|
|
694
|
-
References
|
|
695
|
-
----------
|
|
696
|
-
A. Bella, C. Ferri, J. Hernández-Orallo and M. J. Ramírez-Quintana, "Quantification via Probability Estimators," 2010 IEEE International Conference on Data Mining, Sydney, NSW, Australia, 2010, pp. 737-742, doi: 10.1109/ICDM.2010.75. Available at: https://ieeexplore.ieee.org/abstract/document/5694031
|
|
697
|
-
|
|
698
|
-
Examples
|
|
699
|
-
--------
|
|
700
|
-
>>> from mlquantify.methods.aggregative import PACC
|
|
701
|
-
>>> from mlquantify.utils.general import get_real_prev
|
|
702
|
-
>>> from sklearn.datasets import load_breast_cancer
|
|
703
|
-
>>> from sklearn.svm import SVC
|
|
704
|
-
>>> from sklearn.model_selection import train_test_split
|
|
705
|
-
>>>
|
|
706
|
-
>>> features, target = load_breast_cancer(return_X_y=True)
|
|
707
|
-
>>>
|
|
708
|
-
>>> X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
|
|
709
|
-
>>>
|
|
710
|
-
>>> pacc = PACC(learner=SVC(probability=True))
|
|
711
|
-
>>> pacc.fit(X_train, y_train)
|
|
712
|
-
>>> y_pred = pacc.predict(X_test)
|
|
713
|
-
>>> y_pred
|
|
714
|
-
{0: 0.4664886119311328, 1: 0.5335113880688672}
|
|
715
|
-
>>> get_real_prev(y_test)
|
|
716
|
-
{0: 0.3991228070175439, 1: 0.6008771929824561}
|
|
717
|
-
"""
|
|
718
|
-
|
|
719
|
-
def __init__(self, learner: BaseEstimator=None, threshold: float = 0.5):
|
|
720
|
-
super().__init__(learner)
|
|
721
|
-
self.threshold = threshold
|
|
722
|
-
|
|
723
|
-
def _predict_method(self, X):
|
|
724
|
-
"""
|
|
725
|
-
Predicts the class prevalence using the mean class-conditional
|
|
726
|
-
probabilities from a probabilistic classifier.
|
|
727
|
-
|
|
728
|
-
Parameters
|
|
729
|
-
----------
|
|
730
|
-
X : array-like or sparse matrix of shape (n_samples, n_features)
|
|
731
|
-
The input data for prediction.
|
|
732
|
-
|
|
733
|
-
Returns
|
|
734
|
-
-------
|
|
735
|
-
dict
|
|
736
|
-
A dictionary with class labels as keys and their respective
|
|
737
|
-
prevalence estimates as values.
|
|
738
|
-
|
|
739
|
-
Notes
|
|
740
|
-
-----
|
|
741
|
-
The prevalence is adjusted using the formula:
|
|
742
|
-
prevalence = |mean_score - FPR| / (TPR - FPR),
|
|
743
|
-
where mean_score is the average probability for the positive class.
|
|
744
|
-
|
|
745
|
-
Raises
|
|
746
|
-
------
|
|
747
|
-
ZeroDivisionError
|
|
748
|
-
If `TPR - FPR` equals zero, indicating that the classifier's
|
|
749
|
-
performance does not vary across the threshold range.
|
|
750
|
-
"""
|
|
751
|
-
prevalences = {}
|
|
752
|
-
|
|
753
|
-
# Calculate probabilities for the positive class
|
|
754
|
-
probabilities = self.predict_learner(X)[:, 1]
|
|
755
|
-
|
|
756
|
-
# Compute the mean score for the positive class
|
|
757
|
-
mean_scores = np.mean(probabilities)
|
|
758
|
-
|
|
759
|
-
# Adjust prevalence based on TPR and FPR
|
|
760
|
-
if self.tpr - self.fpr == 0:
|
|
761
|
-
prevalence = mean_scores
|
|
762
|
-
else:
|
|
763
|
-
prevalence = np.clip(abs(mean_scores - self.fpr) / (self.tpr - self.fpr), 0, 1)
|
|
764
|
-
|
|
765
|
-
# Map the computed prevalence to the class labels
|
|
766
|
-
prevalences[self.classes[0]] = 1 - prevalence
|
|
767
|
-
prevalences[self.classes[1]] = prevalence
|
|
768
|
-
|
|
769
|
-
return prevalences
|
|
770
|
-
|
|
771
|
-
def best_tprfpr(self, thresholds: np.ndarray, tprs: np.ndarray, fprs: np.ndarray) -> tuple:
|
|
772
|
-
"""
|
|
773
|
-
Finds the true positive rate (TPR) and false positive rate (FPR)
|
|
774
|
-
corresponding to the specified decision threshold.
|
|
775
|
-
|
|
776
|
-
Parameters
|
|
777
|
-
----------
|
|
778
|
-
thresholds : np.ndarray
|
|
779
|
-
An array of threshold values.
|
|
780
|
-
tprs : np.ndarray
|
|
781
|
-
An array of true positive rates corresponding to the thresholds.
|
|
782
|
-
fprs : np.ndarray
|
|
783
|
-
An array of false positive rates corresponding to the thresholds.
|
|
784
|
-
|
|
785
|
-
Returns
|
|
786
|
-
-------
|
|
787
|
-
tuple
|
|
788
|
-
A tuple containing the specified threshold, TPR, and FPR.
|
|
789
|
-
|
|
790
|
-
Raises
|
|
791
|
-
------
|
|
792
|
-
IndexError
|
|
793
|
-
If the specified threshold is not found in the `thresholds` array.
|
|
794
|
-
"""
|
|
795
|
-
# Locate TPR and FPR for the specified threshold
|
|
796
|
-
tpr = tprs[thresholds == self.threshold][0]
|
|
797
|
-
fpr = fprs[thresholds == self.threshold][0]
|
|
798
|
-
return (self.threshold, tpr, fpr)
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
def best_tprfpr(self, thresholds:np.ndarray, tprs: np.ndarray, fprs: np.ndarray) -> tuple:
|
|
804
|
-
tpr = tprs[thresholds == self.threshold][0]
|
|
805
|
-
fpr = fprs[thresholds == self.threshold][0]
|
|
806
|
-
return (self.threshold, tpr, fpr)
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
662
|
|
|
814
663
|
|
|
815
664
|
class T50(ThresholdOptimization):
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
from setuptools import setup, find_packages
|
|
2
|
-
|
|
3
2
|
import pathlib
|
|
4
3
|
|
|
5
4
|
here = pathlib.Path(__file__).parent.resolve()
|
|
6
|
-
|
|
7
5
|
long_description = (here / 'README.md').read_text(encoding='utf-8')
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
# Lê a versão do arquivo VERSION.txt
|
|
8
|
+
version_file = here / 'VERSION.txt'
|
|
9
|
+
VERSION = version_file.read_text(encoding='utf-8').strip()
|
|
10
|
+
|
|
10
11
|
DESCRIPTION = 'Quantification Library'
|
|
11
12
|
|
|
12
|
-
# Setting up
|
|
13
13
|
setup(
|
|
14
14
|
name="mlquantify",
|
|
15
15
|
version=VERSION,
|
|
16
16
|
url="https://github.com/luizfernandolj/QuantifyML/tree/master",
|
|
17
|
-
maintainer="Luiz Fernando Luth Junior",
|
|
17
|
+
maintainer="Luiz Fernando Luth Junior",
|
|
18
18
|
description=DESCRIPTION,
|
|
19
19
|
long_description=long_description,
|
|
20
20
|
long_description_content_type="text/markdown",
|
|
@@ -30,4 +30,4 @@ setup(
|
|
|
30
30
|
"Operating System :: MacOS :: MacOS X",
|
|
31
31
|
"Operating System :: Microsoft :: Windows",
|
|
32
32
|
]
|
|
33
|
-
)
|
|
33
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|