10
10
11
11
RNG = check_random_state (0 )
12
12
13
+
13
14
class IdentityBilinearMixin (BilinearMixin ):
14
15
"""A simple Identity bilinear mixin that returns an identity matrix
15
16
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):
50
51
mixin .fit (X , [0 for _ in range (n )], random = random )
51
52
if n_pairs is not None :
52
53
random_pairs = [[X [RNG .randint (0 , n )], X [RNG .randint (0 , n )]]
53
- for _ in range (n_pairs )]
54
+ for _ in range (n_pairs )]
54
55
else :
55
56
random_pairs = None
56
57
return X , random_pairs , mixin
@@ -62,7 +63,7 @@ def test_same_similarity_with_two_methods():
62
63
In both cases, the results must match for the same input.
63
64
Tests it for 'n_pairs' sampled from 'n' d-dimentional arrays.
64
65
"""
65
- d , n , n_pairs = 100 , 100 , 1000
66
+ d , n , n_pairs = 100 , 100 , 1000
66
67
_ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
67
68
dist1 = mixin .score_pairs (random_pairs )
68
69
dist2 = [mixin .get_metric ()(p [0 ], p [1 ]) for p in random_pairs ]
@@ -76,11 +77,12 @@ def test_check_correctness_similarity():
76
77
get_metric(). Results are compared with the real bilinear similarity
77
78
calculated in-place.
78
79
"""
79
- d , n , n_pairs = 100 , 100 , 1000
80
+ d , n , n_pairs = 100 , 100 , 1000
80
81
_ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
81
82
dist1 = mixin .score_pairs (random_pairs )
82
83
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 ]
84
86
85
87
assert_array_almost_equal (dist1 , desired ) # score_pairs
86
88
assert_array_almost_equal (dist2 , desired ) # get_metric
@@ -108,7 +110,7 @@ def test_check_handmade_symmetric_example():
108
110
checks the random case: when the matrix is pd and symetric.
109
111
"""
110
112
# Random pairs for M = Identity
111
- d , n , n_pairs = 100 , 100 , 1000
113
+ d , n , n_pairs = 100 , 100 , 1000
112
114
_ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs )
113
115
pairs_reverse = [[p [1 ], p [0 ]] for p in random_pairs ]
114
116
dist1 = mixin .score_pairs (random_pairs )
@@ -122,13 +124,14 @@ def test_check_handmade_symmetric_example():
122
124
dist2 = mixin .score_pairs (pairs_reverse )
123
125
assert_array_almost_equal (dist1 , dist2 )
124
126
127
+
125
128
def test_score_pairs_finite ():
126
129
"""
127
130
Checks for 'n' score_pairs() of 'd' dimentions, that all
128
131
similarities are finite numbers, not NaN, +inf or -inf.
129
132
Considering a random M for bilinear similarity.
130
133
"""
131
- d , n , n_pairs = 100 , 100 , 1000
134
+ d , n , n_pairs = 100 , 100 , 1000
132
135
_ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
133
136
dist1 = mixin .score_pairs (random_pairs )
134
137
assert np .isfinite (dist1 ).all ()
@@ -140,7 +143,7 @@ def test_score_pairs_dim():
140
143
and scoring of 2D arrays (one tuple) should return an error (like
141
144
scikit-learn's error when scoring 1D arrays)
142
145
"""
143
- d , n , n_pairs = 100 , 100 , 1000
146
+ d , n = 100 , 100
144
147
X , _ , mixin = identity_fit (d = d , n = n , n_pairs = None , random = True )
145
148
tuples = np .array (list (product (X , X )))
146
149
assert mixin .score_pairs (tuples ).shape == (tuples .shape [0 ],)
@@ -156,7 +159,7 @@ def test_score_pairs_dim():
156
159
def test_check_scikitlearn_compatibility ():
157
160
"""Check that the similarity returned by get_metric() is compatible with
158
161
scikit-learn's algorithms using a custom metric, DBSCAN for instance"""
159
- d , n = 100 , 100
162
+ d , n = 100 , 100
160
163
X , _ , mixin = identity_fit (d = d , n = n , n_pairs = None , random = True )
161
164
clustering = DBSCAN (metric = mixin .get_metric ())
162
165
clustering .fit (X )
0 commit comments