-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cov alignment heuristic * cov heu test * more tests * tests * Setup.py * tests * overflow * proper types? * int32 * moved test
- Loading branch information
Showing
6 changed files
with
95 additions
and
6 deletions.
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): | ||
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) |