我写了以下代码
import cvxpy
import numpy as np
def missingMat(A, mask):##true and false for known and missing entries
M=np.array(A)
for i in range(0, mask.shape[0]):
for j in range(0, mask.shape[1]):
if mask[i][j]==False:
M[i][j]=float("nan")
return M
def constraintsFun(X, missing_mask, S, error_tolerance):
ok_mask = ~missing_mask
masked_X = cvxpy.mul_elemwise(ok_mask, X)
masked_S = cvxpy.mul_elemwise(ok_mask, S)
abs_diff = cvxpy.abs(masked_S - masked_X)
close_to_data =(abs_diff <= 0.0001)
constraints = [close_to_data]
return constraints
def create_objective(m, n):
S=cvxpy.Semidef(m,n)
norm = cvxpy.norm(S, "nuc")
objective = cvxpy.Minimize(norm)
return S, objective
def solve(X, missing_mask):
m, n = X.shape
S, objective = create_objective(m, n)
constraints = constraintsFun(X=X, missing_mask=missing_mask, S=S, error_tolerance=0.0001)
problem = cvxpy.Problem(objective, constraints)
solverLis=cvxpy.installed_solvers()
for solv in solverLis:
try:
problem.solve(solver=solv)##.................error line
except:
print "did not solve", solv
print S.value
return problem, S
A=np.random.uniform(low=1, high=15, size=(8,8))
mask = np.round(np.random.rand(A.shape[0], A.shape[1]))
mask=(mask >= 1)
M=missingMat(A, mask)
prob, S=solve(M, mask)
我试图了解如何使用cvxpy来处理矩阵完成问题。
我有一个矩阵M,缺少与mask
矩阵对应的条目。它被最小化到S的核范数,并且约束对应于在一定容差内的S和M的mask
- True
条目的匹配。
有人可以帮助我找到我的代码或逻辑中的错误吗?我无法找到solve()
的正确用法。我试图通过S.value
获取缺失值。
min S || S || * s.t.已知条目的S = M