forked from abhinav4192/sparse-subspace-clustering-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutlierDetection.py
43 lines (36 loc) · 1.24 KB
/
OutlierDetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# This function takes the coefficient matrix resulted from sparse
# representation using \ell_1 minimization. If a point cannot be written as
# a linear combination of other points, it should be an outlier. The
# function detects the indices of outliers and modifies the coefficient
# matrix and the ground-truth accordingly.
# CMat: NxN coefficient matrix
# s: Nx1 ground-truth vector
# CMatC: coefficient matrix after eliminating Nans
# sc: ground-truth after eliminating outliers
# OutlierIndx: indices of outliers in {1,2,...,N}
# Fail: True if number of inliers is less than number of groups, False otherwise
import numpy as np
def OutlierDetection(CMat, s):
n = np.amax(s)
_, N = CMat.shape
OutlierIndx = list()
FailCnt = 0
Fail = False
for i in range(0, N):
c = CMat[:, i]
if np.sum(np.isnan(c)) >= 1:
OutlierIndx.append(i)
FailCnt += 1
sc = s.astype(float)
sc[OutlierIndx] = np.nan
CMatC = CMat.astype(float)
CMatC[OutlierIndx, :] = np.nan
CMatC[:, OutlierIndx] = np.nan
OutlierIndx = OutlierIndx
if FailCnt > (N - n):
CMatC = np.nan
sc = np.nan
Fail = True
return CMatC, sc, OutlierIndx, Fail
if __name__ == "__main__":
pass