Singular Value Thresholding (SVT)
import os
import xlrd
import xlwt
import numpy as np
import pandas as pd
from time import time
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, mean_absolute_error, f1_score
from sklearn.utils import check_array
class SVT_MC():
def __init__(self):
self.svd_algorithm = "arpack"
def impute(self, X, observed_mask, maxiter, rho=0.005):
X = check_array(X, force_all_finite=False)
nSample, nDim = X.shape
observed_mask = np.asarray(observed_mask)
missing_mask = 1 - observed_mask
X_filled = X * missing_mask #所有缺失项变成零
observed_index = np.where(observed_mask != 0)
T = np.zeros(X_filled.shape)
for _ in range(maxiter):
U, sigma, VT = np.linalg.svd(X_filled + T/rho, full_matrices=0)
vec = sigma - rho
vec[np.where(vec < 0)] = 0
Z = np.matmul(np.matmul(U, np.diag(vec)), VT)
X_filled = Z - T * rho
X_filled[observed_index] = X[observed_index]
T = T - rho * (Z - X_filled)
return X_filled