-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coreset_methods.py
41 lines (34 loc) · 1.57 KB
/
test_coreset_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
test_coreset_methods.py - Tests for coreset computation methods
===============================================================
This module contains the tests for coreset computation methods.
"""
import unittest
import numpy as np
from Infrastructure.utils import Matrix
from ComparedAlgorithms.method_boosters import create_coreset_fast_caratheodory
class TestMatrixCoreset(unittest.TestCase):
""" A class which contains tests for methods for coreset computation."""
def test_outer_products_decomposition(self):
"""
This test verifies that this `einsum` function computes the outer products of all the matrix's rows.
This means the sum of these outer products must be equal to the original matrix.
"""
n: int = 7
d: int = 2
A: Matrix = 500 * np.random.randn(n, d)
rows_outer_products: Matrix = np.einsum("ij,ik->ijk", A, A, optimize=True)
self.assertTrue(np.allclose(A.T.dot(A), rows_outer_products.sum(axis=0), atol=1e-8, rtol=0))
def test_python_fast_matrix_coresets(self):
"""
This test verifies the 'coreset' of the matrix has the same Gram matrix as the original matrix, i.e
:math:`L^{T}L=A^{T}A`.
"""
n: int = 1000
d: int = 7
clusters_count: int = 2 * (d + 1) ** 2 + 2
A: Matrix = 500 * np.random.randn(n, d)
reduced_mat: Matrix = create_coreset_fast_caratheodory(A, clusters_count)
self.assertTrue(np.allclose(reduced_mat.T.dot(reduced_mat), A.T.dot(A), atol=1e-6, rtol=0))
if __name__ == '__main__':
unittest.main()