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

MC-CCR implementation #102

Merged
merged 22 commits into from
Jun 21, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rename variables
  • Loading branch information
dddddddddtd committed Jun 5, 2023
commit 9283ac1944deeca83e302489f4c2bc7a5a54a3cc
28 changes: 14 additions & 14 deletions multi_imbalance/resampling/ccr.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def _clean_and_generate(self, minority_examples: np.ndarray, majority_examples:
return translated_majority_examples, synthetic_examples

def _calculate_radius_and_translations(self, minority_examples, majority_examples):
r = np.zeros(minority_examples.shape[0])
t = np.zeros(majority_examples.shape)
radius = np.zeros(minority_examples.shape[0])
translations = np.zeros(majority_examples.shape)

majority_count = len(majority_examples)
for i, minority_example in enumerate(minority_examples):
Expand All @@ -89,32 +89,32 @@ def _calculate_radius_and_translations(self, minority_examples, majority_example
number_of_points_in_radius = 1

while current_example < majority_count and energy > 0:
majority_distance_index = sorted_distances_index[current_example]
distance = distances[majority_distance_index]
if distance <= r[i]:
current_example_distance_index = sorted_distances_index[current_example]
current_example_distance = distances[current_example_distance_index]
if current_example_distance <= radius[i]:
number_of_points_in_radius += 1
dr = energy / number_of_points_in_radius

shortest_distance = distances[sorted_distances_index[current_example]]
if r[i] + dr >= shortest_distance:
dr = energy / number_of_points_in_radius # todo: check if this is correct

if radius[i] + dr >= current_example_distance:
number_of_points_in_radius += 1
dr = shortest_distance - r[i]
dr = current_example_distance - radius[i]

r[i] += dr
radius[i] += dr
energy -= dr * (current_example + 1.0)
current_example += 1

if energy > 0:
r[i] += energy / (number_of_points_in_radius - 1)
radius[i] += energy / (number_of_points_in_radius - 1)

examples_in_range_index = np.flatnonzero(distances <= r[i])
examples_in_range_index = np.flatnonzero(distances <= radius[i])
for j in examples_in_range_index:
d = distances[j]
if d == 0:
continue
translation = majority_examples[j] - minority_example
t[j] += (r[i] - d) / d * translation
return r, t
translations[j] += (radius[i] - d) / d * translation
return radius, translations

def _generate_synthetic_examples(self, majority_examples, minority_examples, r, synthetic_examples_total):
generation_order = r.argsort()
Expand Down