-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTGA_Testing.py
More file actions
81 lines (39 loc) · 1.27 KB
/
TGA_Testing.py
File metadata and controls
81 lines (39 loc) · 1.27 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# coding: utf-8
# # Robust PCA Example
# Robust PCA is an awesome relatively new method for factoring a matrix into a low rank component and a sparse component. This enables really neat applications for outlier detection, or models that are robust to outliers.
# ### Make Some Toy Data
# In[49]:
import numpy as np
# In[50]:
def mk_rot_mat(rad=np.pi / 4):
rot = np.array([[np.cos(rad),-np.sin(rad)], [np.sin(rad), np.cos(rad)]])
return rot
# In[51]:
rot_mat = mk_rot_mat( np.pi / 4)
x = np.random.randn(100) * 5
y = np.random.randn(100)
points = np.vstack([y,x])
# In[52]:
rotated = np.dot(points.T, rot_mat).T
# ### Add Some Outliers to Make Life Difficult
# In[53]:
outliers = np.tile([15,-10], 10).reshape((-1,2))
# In[54]:
pts = np.vstack([rotated.T, outliers]).T
# ### Compute SVD on both the clean data and the outliery data
# In[55]:
U,s,Vt = np.linalg.svd(rotated)
U_n,s_n,Vt_n = np.linalg.svd(pts)
# ### Just 10 outliers can really screw up our line fit!
# ### Now the robust pca version!
# In[57]:
import tga
# In[58]:
reload(tga)
# In[59]:
import logging
logger = logging.getLogger(tga.__name__)
logger.setLevel(logging.INFO)
# ### Factor the matrix into L (low rank) and S (sparse) parts
# In[60]:
v = tga.tga(pts.T, eps=0.0000001, k=1)