Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a trained_on_dist to imblearn/random.py #1511 #1513

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading