-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix_fun.py
67 lines (57 loc) · 1.95 KB
/
matrix_fun.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 3 12:13:19 2019
@author: autol
"""
import numpy as np
def Fill(A,fill='zero'): # Fill funtion of imputing: "zero", "mean", "median", "min", "random"
Anas = np.isnan(A)
X = A.copy()
if fill == 'zero':
return np.nan_to_num(X)
elif fill == 'mean':
col_fill = np.nanmean(X, axis=0) # 忽略nan的均值
elif fill == 'median':
col_fill = np.nanmedian(X, axis=0)
elif fill == 'min':
col_fill = np.nanmin(X, axis=0)
elif fill == 'random':
B = np.full(X.shape,np.nan)
B[Anas] = np.random.randn(Anas.sum())
B = B * np.nanstd(X, axis=0) + np.nanmean(X, axis=0)
X[Anas] = B[Anas]
return X
np.copyto(X, col_fill, where=np.isnan(X))
return X
def Prox(X,λ): # Pro function a proximate projection of X and lambda
return np.maximum(X-λ,0) # np.sign(X)
def Frob1(U0,d0,V0,U,d,V): # from github
denom = (d0 ** 2).sum()
utu = d * (U.T @ U0)
vtv = d0 * (V0.T @ V)
uvprod = (utu @ vtv).diagonal().sum()
num = denom + (d**2).sum() - 2*uvprod
return num/max(denom, 1e-9)
def Frob2(Z,Z1): # from textbook
a = np.trace((Z-Z1).T@(Z-Z1)) # (E**2).sum()
b = np.trace(Z.T@Z)
return a/max(b, 1e-9)
def obj1(Z,Z1,xnas,nz): # origin
E = (Z-Z1)[~xnas]
return 1./2*(E**2).sum()/nz
def obj2(Z,Z1,xnas,nz,d,λ): # penalty
E = (Z-Z1)[~xnas]
return (1./2*(E**2).sum()+λ*d.sum())/nz # d.sum() = np.trace(np.diag(d))
def svd_(Z):
return np.linalg.svd(Z,full_matrices=0)
def svdk(Z,k):
U,d,Vt = np.linalg.svd(Z,full_matrices=0)
U,d,Vt = U[:,:k],d[:k],Vt[:k,:]
return U,d,Vt
#def Frob1(U0,D0,V0,U,D,V): # from github
# denom = (D0 ** 2).sum()
# utu = D * (U.T.dot(U0))
# vtv = D0 * (V0.T.dot(V))
# uvprod = utu.dot(vtv).diagonal().sum()
# num = denom + (D**2).sum() - 2*uvprod
# return num/max(denom, 1e-9)