def __completef(fitinfo, method=None): """Completes missing values in f using matrix-completion library (Duan, 2020).""" mof = fitinfo['mof'] f = fitinfo['f'] if method is None or method == 'svt': # Singular value thresholding fhat = svt_solve(f, ~mof) elif method == 'pmf': # Probablistic matrix factorization fhat = pmf_solve(f, ~mof, k=10, mu=1e-2) elif method == 'bmf': # Biased alternating least squares fhat = biased_mf_solve(f, ~mof, k=10, mu=1e-2) else: raise ValueError( 'Unsupported completion method. {:s}'.format(methodoptionstr)) if not np.isfinite(fhat).all(): raise ValueError('Completion method {:s} failed. {:s} {:s}'.format( method, methodoptionstr, suggeststr)) fitinfo['f'] = fhat fitinfo['completionmethod'] = method return
def matrix_completion(self, matrix, mask, method, param_a, param_b): if method == "BNNR": completed_matrix, iterations = bnnr(matrix.to_numpy(), mask.to_numpy(), alpha=param_a, beta=param_b) elif method == "SVT": completed_matrix = svt_solve(matrix.to_numpy(), mask.to_numpy(), algorithm='randomized', tau=param_a, delta=param_b) elif method == "Candès and Recht's method": completed_matrix = nuclear_norm_solve(matrix.to_numpy(), mask.to_numpy(), mu=param_a) completed_matrix = pd.DataFrame(completed_matrix, index=matrix.index, columns=matrix.columns) return completed_matrix
items = 150 rank = 15 percent_shown = np.arange(0.2, 1.0, 0.1) res_observed = np.zeros((np.size(percent_shown), 3)) res_unobserved = np.zeros((np.size(percent_shown), 3)) for i in range(len(percent_shown)): U = np.random.randn(users, rank) V = np.random.randn(items, rank) # R = np.random.randn(users, items) + np.dot(U, V.T) # with noise R = np.dot(U, V.T) # without noise mask = np.random.binomial(1, percent_shown[i], size=users * items).reshape( (users, items)) rHat_svt = svt_solve(R, mask) res_observed[i, 0] = calc_observed_rmse(R, rHat_svt, mask) res_unobserved[i, 0] = calc_unobserved_rmse(R, rHat_svt, mask) rHat_pmf = pmf_solve(R, mask, 10, 1e-2) res_observed[i, 1] = calc_observed_rmse(R, rHat_pmf, mask) res_unobserved[i, 1] = calc_unobserved_rmse(R, rHat_pmf, mask) rHat_bias = biased_mf_solve(R, mask, 10, 1e-2) res_observed[i, 2] = calc_observed_rmse(R, rHat_bias, mask) res_unobserved[i, 2] = calc_unobserved_rmse(R, rHat_bias, mask) plt.plot(percent_shown, res_observed[:, 0], 'r--', label='SVT') plt.plot(percent_shown, res_observed[:, 1], 'g--', label='PMF') plt.plot(percent_shown, res_observed[:, 2], 'b--', label='BIAS') plt.title('Observed RMSE')
import numpy as np from matrix_completion import svt_solve, pmf_solve, biased_mf_solve import matplotlib.pyplot as plt ratings_100k = np.load('rating-100k.npy') plt.imshow(ratings_100k, cmap='jet', interpolation='nearest') plt.show() mask = (ratings_100k > 0).astype(np.int) rHat_svt = svt_solve(ratings_100k, mask) plt.imshow(rHat_svt, cmap='jet', interpolation='nearest') plt.show() rHat_pmf = pmf_solve(ratings_100k, mask, 10, 1e-2) plt.imshow(rHat_pmf, cmap='jet', interpolation='nearest') plt.show() rHat_bias = biased_mf_solve(ratings_100k, mask, 10, 1e-2) plt.imshow(rHat_bias, cmap='jet', interpolation='nearest') plt.show()
# -*- coding: utf-8 -*- import numpy as np from matrix_completion import svt_solve, calc_unobserved_rmse U = np.random.randn(20, 5) V = np.random.randn(15, 5) R = np.random.randn(20, 15) + np.dot(U, V.T) mask = np.round(np.random.rand(20, 15)) R_hat = svt_solve(R, mask) print("RMSE:", calc_unobserved_rmse(U, V, R_hat, mask))
import numpy as np from matrix_completion import svt_solve, pmf_solve, biased_mf_solve import matplotlib.pyplot as plt ratings_1m = np.load('rating-1m.npy') plt.imshow(ratings_1m, cmap='jet', interpolation='nearest') plt.show() mask = (ratings_1m > 0).astype(np.int) rHat_svt = svt_solve(ratings_1m, mask) plt.imshow(rHat_svt, cmap='jet', interpolation='nearest') plt.show() rHat_pmf = pmf_solve(ratings_1m, mask, 10, 1e-2) plt.imshow(rHat_pmf, cmap='jet', interpolation='nearest') plt.show() rHat_bias = biased_mf_solve(ratings_1m, mask, 10, 1e-2) plt.imshow(rHat_bias, cmap='jet', interpolation='nearest') plt.show()
def Forward_matrix(keypoints_array, groundtruth_array, groundtruth_array_for_normalisation, is_test_sample, N, landmarksfornormalise=None, number_of_different_landmarks=3): keypoints_array = keypoints_array.copy() groundtruth_array = groundtruth_array.copy() groundtruth_array_for_normalisation = groundtruth_array_for_normalisation.copy( ) keypoints_array = keypoints_array.reshape(keypoints_array.shape[0], -1, 2) groundtruth_array_for_normalisation = groundtruth_array_for_normalisation[ is_test_sample == 1] forward_per_landmark = np.zeros(int(groundtruth_array.shape[1] / 2)) train_keypoints_array = keypoints_array[is_test_sample == 0] test_keypoints_array = keypoints_array[is_test_sample == 1] test_groundtruth = groundtruth_array[is_test_sample == 1] train_groundtruth = groundtruth_array[is_test_sample == 0] number_of_test_samples = len(test_keypoints_array) nl = 2 * keypoints_array.shape[1] Xtr_new = train_keypoints_array Xtr_new = Xtr_new.reshape(Xtr_new.shape[0], -1) Xtest_new = test_keypoints_array.reshape(test_keypoints_array.shape[0], keypoints_array.shape[1], 2) DF = pd.DataFrame(Xtr_new) col_means = DF.apply(np.mean, 0) Xc_tr_mean = DF.fillna(value=col_means).to_numpy() / 256.0 Xc_tr = Xc_tr_mean.copy() mask = np.ones_like(Xtr_new.reshape(len(Xtr_new), nl)) mask[np.where(np.isnan(Xtr_new.reshape(len(Xtr_new), nl)))] = 0 R_hat = svt_solve(Xc_tr, np.round(mask)) Xc_tr = 256.0 * R_hat Xc_tr[np.where(mask == 1)] = Xtr_new.reshape(len(Xtr_new), nl)[np.where(mask == 1)] DF = pd.DataFrame(Xtest_new.reshape(Xtest_new.shape[0], nl)) Xc_test = DF.fillna(value=col_means).to_numpy() Ytest = test_groundtruth err_fwd_fs = np.zeros((10, Xc_test.shape[0], Ytest.shape[1] // 2)) err_fwd_io = np.zeros((10, Xc_test.shape[0], Ytest.shape[1] // 2)) for j in range(0, 10): reg_factor = 0.01 ty = 'type2' centre = 256.0 imgs = np.random.permutation(1000)[:N] Ytr_aux = train_groundtruth[imgs, :] Xc_tr_aux = Xc_tr[imgs, :] R, X0, Y0 = train_regressor(Xc_tr_aux, Ytr_aux, reg_factor, centre, ty) for i in range(0, test_keypoints_array.shape[0]): x = Xc_test[i, :] y = test_groundtruth[i, :] x = fit_regressor(R, x, X0, Y0, centre, ty) gt = y.reshape(-1, 2) iod = GetnormaliseDistance(gt, landmarksfornormalise) y = y.reshape(-1, 2) err_fwd_io[j, i, :] = np.sqrt(np.sum((x - y)**2, 1)) / iod err_fwd_io = np.mean(np.mean(err_fwd_io, axis=0), axis=0) return err_fwd_io