-
Notifications
You must be signed in to change notification settings - Fork 11
/
soft_impute.py
116 lines (96 loc) · 3.36 KB
/
soft_impute.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'''
Copyright 2015 Travis Brady
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from __future__ import print_function
import numpy as np
def frob(Uold, Dsqold, Vold, U, Dsq, V):
denom = (Dsqold ** 2).sum()
utu = Dsq * (U.T.dot(Uold))
vtv = Dsqold * (Vold.T.dot(V))
uvprod = utu.dot(vtv).diagonal().sum()
num = denom + (Dsqold ** 2).sum() - 2*uvprod
return num / max(denom, 1e-9)
class SoftImpute:
def __init__(self, J=2, thresh=1e-05, lambda_=0, maxit=100, random_state=None, verbose=False):
self.J = J
self.thresh = thresh
self.lambda_ = lambda_
self.maxit = maxit
self.rs = np.random.RandomState(random_state)
self.verbose = verbose
self.u = None
self.d = None
self.v = None
def fit(self, X):
n,m = X.shape
xnas = np.isnan(X)
nz = m*n - xnas.sum()
xfill = X.copy()
V = np.zeros((m, self.J))
U = self.rs.normal(0.0, 1.0, (n, self.J))
U, _, _ = np.linalg.svd(U, full_matrices=False)
Dsq = np.ones((self.J, 1))
#xfill[xnas] = 0.0
col_means = np.nanmean(xfill, axis=0)
np.copyto(xfill, col_means, where=np.isnan(xfill))
ratio = 1.0
iters = 0
while ratio > self.thresh and iters < self.maxit:
iters += 1
U_old = U
V_old = V
Dsq_old = Dsq
B = U.T.dot(xfill)
if self.lambda_ > 0:
tmp = (Dsq / (Dsq + self.lambda_))
B = B * tmp
Bsvd = np.linalg.svd(B.T, full_matrices=False)
V = Bsvd[0]
Dsq = Bsvd[1][:, np.newaxis]
U = U.dot(Bsvd[2])
tmp = Dsq * V.T
xhat = U.dot(tmp)
xfill[xnas] = xhat[xnas]
A = xfill.dot(V).T
Asvd = np.linalg.svd(A.T, full_matrices=False)
U = Asvd[0]
Dsq = Asvd[1][:, np.newaxis]
V = V.dot(Asvd[2])
tmp = Dsq * V.T
xhat = U.dot(tmp)
xfill[xnas] = xhat[xnas]
ratio = frob(U_old, Dsq_old, V_old, U, Dsq, V)
if self.verbose:
print('iter: %4d ratio = %.5f' % (iters, ratio))
self.u = U[:,:self.J]
self.d = Dsq[:self.J]
self.v = V[:,:self.J]
return self
def suv(self, vd):
res = self.u.dot(vd.T)
return res
def predict(self, X, copyto=False):
vd = self.v * np.outer(np.ones(self.v.shape[0]), self.d)
X_imp = self.suv(vd)
if copyto:
np.copyto(X, X_imp, where=np.isnan(X))
else:
return X_imp
def main():
X = np.random.random((10,3)) + (np.arange(10).reshape(10,1) ** 2)
clf = SoftImpute(J=2, lambda_=0.0)
fit = clf.fit(X)
X_test = X.copy()
X_test[3,1] = np.nan
X_imp = clf.predict(X_test)
if __name__ == '__main__':
main()