Skip to content

Commit 81bd710

Browse files
committed
changes rpca
1 parent 158c09c commit 81bd710

File tree

4 files changed

+94
-70
lines changed

4 files changed

+94
-70
lines changed

PCA/.ipynb_checkpoints/robust_pca-checkpoint.ipynb

+26-6
Large diffs are not rendered by default.
-58 Bytes
Binary file not shown.

lib/outlier.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,21 @@ def plot(self,log=False, sort = False ):
9595

9696
class R_pca:
9797

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)
102101

103102
if mu:
104103
self.mu = mu
105104
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))
107106

108107
self.mu_inv = 1 / self.mu
109108

110109
if lmbda:
111110
self.lmbda = lmbda
112111
else:
113-
self.lmbda = 1 / np.sqrt(np.max(self.D.shape))
112+
self.lmbda = 1 / np.sqrt(np.max(D.shape))
114113

115114
@staticmethod
116115
def norm_p(M, p):
@@ -124,25 +123,25 @@ def svd_threshold(self, M, tau):
124123
U, S, V = np.linalg.svd(M, full_matrices=False)
125124
return np.dot(U, np.dot(np.diag(self.shrink(S, tau)), V))
126125

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):
128127
iter = 0
129128
err = np.Inf
130129
Sk = self.S
131130
Yk = self.Y
132-
Lk = np.zeros(self.D.shape)
131+
Lk = np.zeros(D.shape)
133132

134133
if tol:
135134
_tol = tol
136135
else:
137-
_tol = 1E-7 * self.norm_p(np.abs(self.D), 2)
136+
_tol = 1E-7 * self.norm_p(np.abs(D), 2)
138137

139138
while (err > _tol) and iter < max_iter:
140139
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)
142141
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)
146145
iter += 1
147146
if (iter % iter_print) == 0 or iter == 1 or iter > max_iter or err <= _tol:
148147
print('iteration: {0}, error: {1}'.format(iter, err))
@@ -163,9 +162,9 @@ def plot_normC(self):
163162
plt.title('Norme 2 de $C_{0}$ pour chaque point',fontsize=15)
164163
plt.show()
165164

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):
167166

168-
n, d = self.D.shape
167+
n, d = D.shape
169168

170169
if size:
171170
nrows, ncols = size
@@ -174,8 +173,8 @@ def plot_fit(self, size=None, tol=0.1, axis_on=True):
174173
nrows = int(sq)
175174
ncols = int(sq)
176175

177-
ymin = np.nanmin(self.D)
178-
ymax = np.nanmax(self.D)
176+
ymin = np.nanmin(D)
177+
ymax = np.nanmax(D)
179178
print('ymin: {0}, ymax: {1}'.format(ymin, ymax))
180179

181180
numplots = np.min([n, nrows * ncols])
@@ -189,7 +188,6 @@ def plot_fit(self, size=None, tol=0.1, axis_on=True):
189188
if not axis_on:
190189
plt.axis('off')
191190

192-
193191
class OutliersKmeans:
194192
''' v.0.2 OutliersKmeans : Find outliers using Kmeans. Only for semi-supervised outlier detection'''
195193
def __init__(self,normaldata,kmeans,parallel=False):

0 commit comments

Comments
 (0)