Skip to content

Commit

Permalink
add a trained_on_dist to imblearn/random.py #1511
Browse files Browse the repository at this point in the history
  • Loading branch information
niccolopetti committed Mar 12, 2024
1 parent f603161 commit 95da1f9
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions river/imblearn/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def __init__(self, classifier: base.Classifier, desired_dist: dict, seed: int |
super().__init__(classifier=classifier, seed=seed)
self.desired_dist = desired_dist
self._actual_dist: typing.Counter = collections.Counter()
self._trained_on_dist: typing.Counter = collections.Counter()
self._pivot = None

def learn_one(self, x, y, **kwargs):
Expand All @@ -90,6 +91,7 @@ def learn_one(self, x, y, **kwargs):
if y != self._pivot:
self._pivot = max(g.keys(), key=lambda y: f[y] / g[y])
else:
self._trained_on_dist[y] += 1
self.classifier.learn_one(x, y, **kwargs)
return

Expand All @@ -98,6 +100,7 @@ def learn_one(self, x, y, **kwargs):
ratio = f[y] / (M * g[y])

if ratio < 1 and self._rng.random() < ratio:
self._trained_on_dist[y] += 1
self.classifier.learn_one(x, y, **kwargs)


Expand Down Expand Up @@ -151,6 +154,7 @@ class percentages. The values must sum up to 1.
def __init__(self, classifier: base.Classifier, desired_dist: dict, seed: int | None = None):
super().__init__(classifier=classifier, seed=seed)
self.desired_dist = desired_dist
self._trained_on_dist: typing.Counter = collections.Counter()
self._actual_dist: typing.Counter = collections.Counter()
self._pivot = None

Expand All @@ -163,13 +167,15 @@ def learn_one(self, x, y, **kwargs):
if y != self._pivot:
self._pivot = max(g.keys(), key=lambda y: g[y] / f[y])
else:
self._trained_on_dist[y] += 1
self.classifier.learn_one(x, y, **kwargs)
return

M = g[self._pivot] / f[self._pivot]
rate = M * f[y] / g[y]

for _ in range(utils.random.poisson(rate, rng=self._rng)):
self._trained_on_dist[y] += 1
self.classifier.learn_one(x, y, **kwargs)


Expand Down Expand Up @@ -233,6 +239,7 @@ def __init__(
):
super().__init__(classifier=classifier, seed=seed)
self.sampling_rate = sampling_rate
self._trained_on_dist: typing.Counter = collections.Counter()
self._actual_dist: typing.Counter = collections.Counter()
if desired_dist is None:
desired_dist = self._actual_dist
Expand All @@ -248,4 +255,5 @@ def learn_one(self, x, y, **kwargs):
rate = self.sampling_rate * f[y] / (g[y] / self._n)

for _ in range(utils.random.poisson(rate, rng=self._rng)):
self._trained_on_dist[y] += 1
self.classifier.learn_one(x, y, **kwargs)

0 comments on commit 95da1f9

Please sign in to comment.