13
13
14
14
class IdentityBilinearMixin (BilinearMixin ):
15
15
"""A simple Identity bilinear mixin that returns an identity matrix
16
- M as learned. Can change M for a random matrix calling random_M.
17
- Class for testing purposes.
16
+ M as learned. Can change M for a random matrix specifying random=True
17
+ at fit(). Class for testing purposes.
18
18
"""
19
19
def __init__ (self , preprocessor = None ):
20
20
super ().__init__ (preprocessor = preprocessor )
21
21
22
22
def fit (self , X , y , random = False ):
23
23
"""
24
- Checks input's format. Sets M matrix to identity of shape (d,d)
25
- where d is the dimension of the input.
24
+ Checks input's format. If random=False, sets M matrix to
25
+ identity of shape (d,d) where d is the dimension of the input.
26
+ Otherwise, a random (d,d) matrix is set.
26
27
"""
27
28
X , y = self ._prepare_inputs (X , y , ensure_min_samples = 2 )
28
29
self .d = np .shape (X [0 ])[- 1 ]
@@ -32,19 +33,14 @@ def fit(self, X, y, random=False):
32
33
self .components_ = np .identity (self .d )
33
34
return self
34
35
35
- def random_M (self ):
36
- """
37
- Changes the matrix M for a random one of shape (d,d)
38
- """
39
- self .components_ = np .random .rand (self .d , self .d )
40
-
41
36
42
37
def identity_fit (d = 100 , n = 100 , n_pairs = None , random = False ):
43
38
"""
44
39
Creates 'n' d-dimentional arrays. Also generates 'n_pairs'
45
40
sampled from the 'n' arrays. Fits an IdentityBilinearMixin()
46
41
and then returns the arrays, the pairs and the mixin. Only
47
- generates the pairs if n_pairs is not None
42
+ generates the pairs if n_pairs is not None. If random=True,
43
+ the matrix M fitted will be random.
48
44
"""
49
45
X = np .array ([np .random .rand (d ) for _ in range (n )])
50
46
mixin = IdentityBilinearMixin ()
@@ -107,7 +103,7 @@ def test_check_handmade_symmetric_example():
107
103
"""
108
104
When the Bilinear matrix is the identity. The similarity
109
105
between two arrays must be equal: S(u,v) = S(v,u). Also
110
- checks the random case: when the matrix is pd and symetric.
106
+ checks the random case: when the matrix is spd and symetric.
111
107
"""
112
108
# Random pairs for M = Identity
113
109
d , n , n_pairs = 100 , 100 , 1000
@@ -128,8 +124,8 @@ def test_check_handmade_symmetric_example():
128
124
def test_score_pairs_finite ():
129
125
"""
130
126
Checks for 'n' score_pairs() of 'd' dimentions, that all
131
- similarities are finite numbers, not NaN, +inf or -inf.
132
- Considering a random M for bilinear similarity.
127
+ similarities are finite numbers: not NaN, +inf or -inf.
128
+ Considers a random M for bilinear similarity.
133
129
"""
134
130
d , n , n_pairs = 100 , 100 , 1000
135
131
_ , random_pairs , mixin = identity_fit (d = d , n = n , n_pairs = n_pairs , random = True )
0 commit comments