1010
1111RNG = check_random_state (0 )
1212
13+
1314class IdentityBilinearMixin (BilinearMixin ):
1415 """A simple Identity bilinear mixin that returns an identity matrix
1516 M as learned. Can change M for a random matrix calling random_M.
@@ -50,7 +51,7 @@ def identity_fit(d=100, n=100, n_pairs=None, random=False):
5051 mixin .fit (X , [0 for _ in range (n )], random = random )
5152 if n_pairs is not None :
5253 random_pairs = [[X [RNG .randint (0 , n )], X [RNG .randint (0 , n )]]
53- for _ in range (n_pairs )]
54+ for _ in range (n_pairs )]
5455 else :
5556 random_pairs = None
5657 return X , random_pairs , mixin
@@ -62,7 +63,7 @@ def test_same_similarity_with_two_methods():
6263 In both cases, the results must match for the same input.
6364 Tests it for 'n_pairs' sampled from 'n' d-dimentional arrays.
6465 """
65- d , n , n_pairs = 100 , 100 , 1000
66+ d , n , n_pairs = 100 , 100 , 1000
6667 _ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
6768 dist1 = mixin .score_pairs (random_pairs )
6869 dist2 = [mixin .get_metric ()(p [0 ], p [1 ]) for p in random_pairs ]
@@ -76,11 +77,12 @@ def test_check_correctness_similarity():
7677 get_metric(). Results are compared with the real bilinear similarity
7778 calculated in-place.
7879 """
79- d , n , n_pairs = 100 , 100 , 1000
80+ d , n , n_pairs = 100 , 100 , 1000
8081 _ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
8182 dist1 = mixin .score_pairs (random_pairs )
8283 dist2 = [mixin .get_metric ()(p [0 ], p [1 ]) for p in random_pairs ]
83- desired = [np .dot (np .dot (p [0 ].T , mixin .components_ ), p [1 ]) for p in random_pairs ]
84+ desired = [np .dot (np .dot (p [0 ].T , mixin .components_ ), p [1 ])
85+ for p in random_pairs ]
8486
8587 assert_array_almost_equal (dist1 , desired ) # score_pairs
8688 assert_array_almost_equal (dist2 , desired ) # get_metric
@@ -108,7 +110,7 @@ def test_check_handmade_symmetric_example():
108110 checks the random case: when the matrix is pd and symetric.
109111 """
110112 # Random pairs for M = Identity
111- d , n , n_pairs = 100 , 100 , 1000
113+ d , n , n_pairs = 100 , 100 , 1000
112114 _ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs )
113115 pairs_reverse = [[p [1 ], p [0 ]] for p in random_pairs ]
114116 dist1 = mixin .score_pairs (random_pairs )
@@ -122,13 +124,14 @@ def test_check_handmade_symmetric_example():
122124 dist2 = mixin .score_pairs (pairs_reverse )
123125 assert_array_almost_equal (dist1 , dist2 )
124126
127+
125128def test_score_pairs_finite ():
126129 """
127130 Checks for 'n' score_pairs() of 'd' dimentions, that all
128131 similarities are finite numbers, not NaN, +inf or -inf.
129132 Considering a random M for bilinear similarity.
130133 """
131- d , n , n_pairs = 100 , 100 , 1000
134+ d , n , n_pairs = 100 , 100 , 1000
132135 _ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
133136 dist1 = mixin .score_pairs (random_pairs )
134137 assert np .isfinite (dist1 ).all ()
@@ -140,7 +143,7 @@ def test_score_pairs_dim():
140143 and scoring of 2D arrays (one tuple) should return an error (like
141144 scikit-learn's error when scoring 1D arrays)
142145 """
143- d , n , n_pairs = 100 , 100 , 1000
146+ d , n = 100 , 100
144147 X , _ , mixin = identity_fit (d = d , n = n , n_pairs = None , random = True )
145148 tuples = np .array (list (product (X , X )))
146149 assert mixin .score_pairs (tuples ).shape == (tuples .shape [0 ],)
@@ -156,7 +159,7 @@ def test_score_pairs_dim():
156159def test_check_scikitlearn_compatibility ():
157160 """Check that the similarity returned by get_metric() is compatible with
158161 scikit-learn's algorithms using a custom metric, DBSCAN for instance"""
159- d , n = 100 , 100
162+ d , n = 100 , 100
160163 X , _ , mixin = identity_fit (d = d , n = n , n_pairs = None , random = True )
161164 clustering = DBSCAN (metric = mixin .get_metric ())
162165 clustering .fit (X )
0 commit comments