-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
39 lines (35 loc) · 895 Bytes
/
Copy pathtools.py
File metadata and controls
39 lines (35 loc) · 895 Bytes
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
"""
Implementation of the method proposed in the paper:
'A Fast Algorithm for Simultaneous Sparse Approximation'
by Guihong Wan, Haim Schweitzer.
Published at PAKDD 2021.
Copyright (C) 2021
Guihong Wan
The University of Texas at Dallas.
"""
import numpy as np
def error(Y, S, X=np.empty((0,0))):
'''
If X is not given, X = Y.
'''
if X.shape[0] > 0:
XS = X[:,S]
else:
XS = Y[:,S]
Q,_ = np.linalg.qr(XS)
W = Q.T@Y
total = np.linalg.norm(Y)**2
explained = np.linalg.norm(W)**2
error = total - explained
return error, (error/total)*100, explained, (explained/total)*100,total
def PCAerror(X, k):
'''
X: column matrix
'''
[U,S,V]=np.linalg.svd(X,full_matrices=False)
U=U[:,:k]
W = U.T@X
total = np.linalg.norm(X)**2
explained = np.linalg.norm(W)**2
error = total - explained
return error, (total/total)*100, explained, (explained/total)*100