-
Notifications
You must be signed in to change notification settings - Fork 3
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
Coverage alignment heuristic #76
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e339ee1
cov alignment heuristic
SkBlaz fadc249
cov heu test
SkBlaz 695e7f9
more tests
SkBlaz 72c6135
tests
SkBlaz f0204a4
Setup.py
SkBlaz bf5701b
tests
SkBlaz 7498e80
overflow
SkBlaz 02d0d80
proper types?
SkBlaz 321e384
int32
SkBlaz 7afc7a9
moved test
SkBlaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
outrank/algorithms/feature_ranking/ranking_cov_alignment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
np.random.seed(123) | ||
max_size = 10**6 | ||
|
||
|
||
def max_pair_coverage(array1: npt.NDArray[np.int32], array2: npt.NDArray[np.int32]) -> float: | ||
def hash_pair(el1: np.int32, el2: np.int32): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing output type hint :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add with improved pre-commit hooks, as that should have picked it up |
||
return (el1 * 1471343 - el2) % max_size | ||
|
||
counts = np.zeros(max_size, dtype=np.int32) | ||
tot_len = len(array1) | ||
for i in range(tot_len): | ||
identifier = hash_pair(array1[i], array2[i]) | ||
counts[identifier] += 1 | ||
|
||
return np.max(counts) / tot_len | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
array1 = np.array([1,1,2,3,1,1,1,5] * 100000) | ||
array2 = np.array([0,0,5,5,3,0,0,0] * 100000) | ||
coverage = max_pair_coverage(array1, array2) | ||
assert coverage == 0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
import sys | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from outrank.algorithms.feature_ranking.ranking_cov_alignment import \ | ||
max_pair_coverage | ||
|
||
np.random.seed(123) | ||
sys.path.append('./outrank') | ||
|
||
|
||
class TestMaxPairCoverage(unittest.TestCase): | ||
def test_basic_functionality(self): | ||
array1 = np.array([1, 2, 3, 1, 2]) | ||
array2 = np.array([4, 5, 6, 4, 5]) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertAlmostEqual(result, 2/5, places=5) | ||
|
||
def test_identical_elements(self): | ||
array1 = np.array([1, 1, 1, 1]) | ||
array2 = np.array([1, 1, 1, 1]) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertEqual(result, 1.0) | ||
|
||
def test_large_arrays(self): | ||
array1 = np.random.randint(0, 100, size=10000) | ||
array2 = np.random.randint(0, 100, size=10000) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertTrue(0 <= result <= 1) | ||
|
||
def test_all_unique_pairs(self): | ||
array1 = np.array([1, 2, 3, 4, 5]) | ||
array2 = np.array([6, 7, 8, 9, 10]) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertEqual(result, 1/5) | ||
|
||
def test_all_same_pairs(self): | ||
array1 = np.array([1, 1, 1, 1, 1]) | ||
array2 = np.array([2, 2, 2, 2, 2]) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertEqual(result, 1.0) | ||
|
||
def test_high_collision_potential(self): | ||
array1 = np.array([1] * 1000) | ||
array2 = np.array([2] * 1000) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertEqual(result, 1.0) | ||
|
||
def test_very_large_arrays(self): | ||
array1 = np.random.randint(0, 1000, size=1000000) | ||
array2 = np.random.randint(0, 1000, size=1000000) | ||
result = max_pair_coverage(array1, array2) | ||
self.assertTrue(0 <= result <= 1) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qq, can max_size be estimated depending on input vector?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can, but this is directly related to batch sizes, where 1mil is a very very safe bound (many things go wrong before this is reached)