From 3837d42cdf92a99faba3d30f00d7c3158ac6ee89 Mon Sep 17 00:00:00 2001 From: albertfrancajosuacosta Date: Sat, 9 Sep 2023 23:07:20 -0300 Subject: [PATCH] Create classes random_variable_uncertainty.py and variable_uncertainty.py. --- base.py | 6 +-- fixed_uncertainty.py | 9 +---- random_variable_uncertainty.py | 73 ++++++++++++++++++++++++++++++++++ variable_uncertainty.py | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 random_variable_uncertainty.py create mode 100644 variable_uncertainty.py diff --git a/base.py b/base.py index 3f7d4d7..d28315e 100644 --- a/base.py +++ b/base.py @@ -11,8 +11,4 @@ class ActiveLearningBase(): - """ Base classe for Active Learning Library """ - - @abc.abstractmethod() - def describe(self, describe): - ... + """ Base classe for Active Learning Library """ \ No newline at end of file diff --git a/fixed_uncertainty.py b/fixed_uncertainty.py index 3fea69c..b5d7905 100644 --- a/fixed_uncertainty.py +++ b/fixed_uncertainty.py @@ -25,17 +25,12 @@ class FixedUncertainty(ActiveLearningBase): """ - def __init__(self, theta: float = 0.95, seed=None): + def __init__(self, theta: float = 0.95): super().__init__() self.theta = theta - def describe(self): - print('Strategy of Active Learning to select instances more significative based on uncertainty.'+ - 'The fixed uncertainty sampler selects samples for labeling based on the uncertainty of the prediction.'+ - 'The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty'+ - 'measure is compared with a fixed uncertainty limit.'+ - 'References [^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014.') + def isSignificative(self, x, y_pred) -> bool: """Ask for the label of a current instance. diff --git a/random_variable_uncertainty.py b/random_variable_uncertainty.py new file mode 100644 index 0000000..8c843bf --- /dev/null +++ b/random_variable_uncertainty.py @@ -0,0 +1,73 @@ +""" +Active Learning 18 + +@author: Albert França Josuá Costa +@email: albertfrancajosuacosta@gmail.com +""" + +import numpy as np + +from .base import ActiveLearningBase + + + +class RandomVariableUncertainty (ActiveLearningBase): + + r"""Strategy of Active Learning to select instances more significative based on uncertainty. + + The random variable uncertainty sampler selects samples for labeling based on the uncertainty of the prediction. + The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty + measure is compared with a random variable uncertainty limit. + + + References + ---------- + [^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014. + +""" + + def __init__(self, theta: float = 0.95, s=0.5, delta=1.0): + super().__init__() + + self.theta = theta + self.s = s + self.delta = delta + + + def isSignificative(self, x, y_pred) -> bool: + """Ask for the label of a current instance. + + Based on the uncertainty of the base classifier, it checks whether the current instance should be labeled. + + Parameters + ---------- + x + Instance + + y_pred + + Arrays of predicted labels + + + Returns + ------- + selected + A boolean indicating whether a label is needed. + True for selected instance. + False for not selecte instance. + + + """ + maximum_posteriori = max(y_pred.values()) + selected = False + + thetaRand = self.theta * np.random.normal(1,self.delta) + + if maximum_posteriori < thetaRand: + self.theta = self.theta*(1-self.s) + selected = True + else: + self.theta = self.theta*(1+self.s) + selected = False + + return selected \ No newline at end of file diff --git a/variable_uncertainty.py b/variable_uncertainty.py new file mode 100644 index 0000000..67c8534 --- /dev/null +++ b/variable_uncertainty.py @@ -0,0 +1,70 @@ +""" +Active Learning 18 + +@author: Albert França Josuá Costa +@email: albertfrancajosuacosta@gmail.com +""" + + +from .base import ActiveLearningBase + + + +class VariableUncertainty (ActiveLearningBase): + + r"""Strategy of Active Learning to select instances more significative based on uncertainty. + + The variable uncertainty sampler selects samples for labeling based on the uncertainty of the prediction. + The higher the uncertainty, the more likely the sample will be selected for labeling. The uncertainty + measure is compared with a random variable uncertainty limit. + + + References + ---------- + [^1]: I. Zliobaite, A. Bifet, B.Pfahringer, G. Holmes. “Active Learning with Drifting Streaming Data”, IEEE Transactions on Neural Netowrks and Learning Systems, Vol.25 (1), pp.27-39, 2014. + +""" + + def __init__(self, theta: float = 0.95, s=0.5): + super().__init__() + + self.theta = theta + self.s = s + + + def isSignificative(self, x, y_pred) -> bool: + """Ask for the label of a current instance. + + Based on the uncertainty of the base classifier, it checks whether the current instance should be labeled. + + Parameters + ---------- + x + Instance + + y_pred + + Arrays of predicted labels + + + Returns + ------- + selected + A boolean indicating whether a label is needed. + True for selected instance. + False for not selecte instance. + + + """ + maximum_posteriori = max(y_pred.values()) + selected = False + + + if maximum_posteriori < self.theta: + self.theta = self.theta*(1-self.s) + selected = True + else: + self.theta = self.theta*(1+self.s) + selected = False + + return selected \ No newline at end of file