@@ -95,22 +95,21 @@ def plot(self,log=False, sort = False ):
95
95
96
96
class R_pca :
97
97
98
- def __init__ (self , D , mu = None , lmbda = None ):
99
- self .D = D
100
- self .S = np .zeros (self .D .shape )
101
- self .Y = np .zeros (self .D .shape )
98
+ def __init__ (self ,D , mu = None , lmbda = None ):
99
+ self .S = np .zeros (D .shape )
100
+ self .Y = np .zeros (D .shape )
102
101
103
102
if mu :
104
103
self .mu = mu
105
104
else :
106
- self .mu = np .prod (self . D .shape ) / (4 * self .norm_p (self . D , 2 ))
105
+ self .mu = np .prod (D .shape ) / (4 * self .norm_p (D , 2 ))
107
106
108
107
self .mu_inv = 1 / self .mu
109
108
110
109
if lmbda :
111
110
self .lmbda = lmbda
112
111
else :
113
- self .lmbda = 1 / np .sqrt (np .max (self . D .shape ))
112
+ self .lmbda = 1 / np .sqrt (np .max (D .shape ))
114
113
115
114
@staticmethod
116
115
def norm_p (M , p ):
@@ -124,25 +123,25 @@ def svd_threshold(self, M, tau):
124
123
U , S , V = np .linalg .svd (M , full_matrices = False )
125
124
return np .dot (U , np .dot (np .diag (self .shrink (S , tau )), V ))
126
125
127
- def fit (self , tol = None , max_iter = 1000 , iter_print = 100 ):
126
+ def fit (self ,D , tol = None , max_iter = 1000 , iter_print = 100 ):
128
127
iter = 0
129
128
err = np .Inf
130
129
Sk = self .S
131
130
Yk = self .Y
132
- Lk = np .zeros (self . D .shape )
131
+ Lk = np .zeros (D .shape )
133
132
134
133
if tol :
135
134
_tol = tol
136
135
else :
137
- _tol = 1E-7 * self .norm_p (np .abs (self . D ), 2 )
136
+ _tol = 1E-7 * self .norm_p (np .abs (D ), 2 )
138
137
139
138
while (err > _tol ) and iter < max_iter :
140
139
Lk = self .svd_threshold (
141
- self . D - Sk + self .mu_inv * Yk , self .mu_inv )
140
+ D - Sk + self .mu_inv * Yk , self .mu_inv )
142
141
Sk = self .shrink (
143
- self . D - Lk + (self .mu_inv * Yk ), self .mu_inv * self .lmbda )
144
- Yk = Yk + self .mu * (self . D - Lk - Sk )
145
- err = self .norm_p (np .abs (self . D - Lk - Sk ), 2 )
142
+ D - Lk + (self .mu_inv * Yk ), self .mu_inv * self .lmbda )
143
+ Yk = Yk + self .mu * (D - Lk - Sk )
144
+ err = self .norm_p (np .abs (D - Lk - Sk ), 2 )
146
145
iter += 1
147
146
if (iter % iter_print ) == 0 or iter == 1 or iter > max_iter or err <= _tol :
148
147
print ('iteration: {0}, error: {1}' .format (iter , err ))
@@ -163,9 +162,9 @@ def plot_normC(self):
163
162
plt .title ('Norme 2 de $C_{0}$ pour chaque point' ,fontsize = 15 )
164
163
plt .show ()
165
164
166
- def plot_fit (self , size = None , tol = 0.1 , axis_on = True ):
165
+ def plot_fit (self ,D , size = None , tol = 0.1 , axis_on = True ):
167
166
168
- n , d = self . D .shape
167
+ n , d = D .shape
169
168
170
169
if size :
171
170
nrows , ncols = size
@@ -174,8 +173,8 @@ def plot_fit(self, size=None, tol=0.1, axis_on=True):
174
173
nrows = int (sq )
175
174
ncols = int (sq )
176
175
177
- ymin = np .nanmin (self . D )
178
- ymax = np .nanmax (self . D )
176
+ ymin = np .nanmin (D )
177
+ ymax = np .nanmax (D )
179
178
print ('ymin: {0}, ymax: {1}' .format (ymin , ymax ))
180
179
181
180
numplots = np .min ([n , nrows * ncols ])
@@ -189,7 +188,6 @@ def plot_fit(self, size=None, tol=0.1, axis_on=True):
189
188
if not axis_on :
190
189
plt .axis ('off' )
191
190
192
-
193
191
class OutliersKmeans :
194
192
''' v.0.2 OutliersKmeans : Find outliers using Kmeans. Only for semi-supervised outlier detection'''
195
193
def __init__ (self ,normaldata ,kmeans ,parallel = False ):
0 commit comments