model_name = None models_names = [model_str for model_str in MODELS if model_str in arguments and arguments[model_str]] if models_names and len(models_names) == 1: model_name = models_names[0] if not model_name: print 'Please insert a valid model name' exit(0) ip_dir = arguments['--ip_dir'] rating_file = os.path.join(ip_dir, EP_RATING) trust_file = os.path.join(ip_dir, EP_TRUST) uik = int(arguments['--uik']) uuk = int(arguments['--uuk']) generator = DataGen(rating_file, trust_file, uik=uik, uuk=uuk) generator.generate() print 'User Item Matrix Size:', generator.ui_matrix.shape print 'User User Matrix Size:', generator.uu_matrix.shape results = None threshold = None if model_name == 'mf': import matrixfac K = int(arguments['--K']) steps = int(arguments['--steps']) alpha = float(arguments['--alpha']) beta = float(arguments['--beta']) threshold = float(arguments['--threshold']) print 'Running MF with K=%d, steps=%d, threshold=%f, alpha=%f and beta=%f' % (K, steps, threshold, alpha, beta) results = matrixfac.run_rec(generator, k=K, steps=steps, alpha=alpha, beta=beta) print "RMSE: %f" % recstats.rmse(generator.ui_matrix, results) print "Coverage: %f " % recstats.coverage(results, threshold if threshold else 1)
X = len(gen.ui_matrix[0]) P = np.random.rand(N, k) Q = np.random.rand(M, k) R = uiu_matrix nP, nQ = matrix_factorization(R, P, Q, k, steps=steps, alpha=alpha, beta=beta) mf_results = np.dot(nP, nQ.T) # trimming off the trust part of the matrix mf_user_item_matrix = mf_results[0:W, 0:X] return mf_user_item_matrix if __name__ == '__main__': import recstats # current file directory, keep the mat files in the same place ip_dir = os.getcwd() rating_file = os.path.join(ip_dir, EP_RATING) trust_file = os.path.join(ip_dir, EP_TRUST) generator = DataGen(rating_file, trust_file) ui_matrix = run_rec(generator, 100) print "RMSE: %f Coverage: %f " % (recstats.rmse(generator.ui_matrix, ui_matrix), recstats.coverage(ui_matrix, 1))
print 'Please insert a valid model name' exit(0) ip_dir = arguments['--ip_dir'] rating_file = os.path.join(ip_dir, EP_RATING) trust_file = os.path.join(ip_dir, EP_TRUST) uik = int(arguments['--uik']) uuk = int(arguments['--uuk']) generator = DataGen(rating_file, trust_file, uik=uik, uuk=uuk) generator.generate() print 'User Item Matrix Size:', generator.ui_matrix.shape print 'User User Matrix Size:', generator.uu_matrix.shape results = None threshold = None if model_name == 'mf': import matrixfac K = int(arguments['--K']) steps = int(arguments['--steps']) alpha = float(arguments['--alpha']) beta = float(arguments['--beta']) threshold = float(arguments['--threshold']) print 'Running MF with K=%d, steps=%d, threshold=%f, alpha=%f and beta=%f' % ( K, steps, threshold, alpha, beta) results = matrixfac.run_rec(generator, k=K, steps=steps, alpha=alpha, beta=beta) print "RMSE: %f" % recstats.rmse(generator.ui_matrix, results) print "Coverage: %f " % recstats.coverage(results, threshold if threshold else 1)