<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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.
Step1:  Make Some Toy Data
Step2:  Add Some Outliers to Make Life Difficult
Step3:  Compute SVD on both the clean data and the outliery data
Step4:  Just 10 outliers can really screw up our line fit!
Step5:  Now the robust pca version!
Step6:  Factor the matrix into L (low rank) and S (sparse) parts
Step7:  And have a look at this!
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
Explanation: 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.
End of explanation
import matplotlib.pyplot as plt
import numpy as np
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
rot_mat = mk_rot_mat( np.pi / 4)
x = np.random.randn(100) * 5
y = np.random.randn(100)
points = np.vstack([y,x])
rotated = np.dot(points.T, rot_mat).T
Explanation: Make Some Toy Data
End of explanation
outliers = np.tile([15,-10], 10).reshape((-1,2))
pts = np.vstack([rotated.T, outliers]).T
Explanation: Add Some Outliers to Make Life Difficult
End of explanation
U,s,Vt = np.linalg.svd(rotated)
U_n,s_n,Vt_n = np.linalg.svd(pts)
Explanation: Compute SVD on both the clean data and the outliery data
End of explanation
plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*pts)
pca_line = np.dot(U[0].reshape((2,1)), np.array([-20,20]).reshape((1,2)))
plt.plot(*pca_line)
rpca_line = np.dot(U_n[0].reshape((2,1)), np.array([-20,20]).reshape((1,2)))
plt.plot(*rpca_line, c='r')
Explanation: Just 10 outliers can really screw up our line fit!
End of explanation
import tga
reload(tga)
import logging
logger = logging.getLogger(tga.__name__)
logger.setLevel(logging.INFO)
Explanation: Now the robust pca version!
End of explanation
X = pts.copy()
v = tga.tga(X.T, eps=1e-5, k=1, p=0.0)
Explanation: Factor the matrix into L (low rank) and S (sparse) parts
End of explanation
plt.ylim([-20,20])
plt.xlim([-20,20])
plt.scatter(*pts)
tga_line = np.dot(v[0].reshape((2,1)), np.array([-20,20]).reshape((1,2)))
plt.plot(*tga_line)
#plt.scatter(*L, c='red')
Explanation: And have a look at this!
End of explanation
<END_TASK>