Exemplo n.º 1
0
def test(num_feature=30, delta=1, lamda_p=2):
    bmf_model = example()
    rating_matrix = bmf_model.matrix
    user_feature = bmf_model.user_features
    print 'user_feature.shape', len(user_feature)
    # required parameters
    beta_item = bmf_model.beta_item
    N = bmf_model.num_item
    # item mean and variance
    mu_Sigma_items = bmf_model.ret_mu_Sigma_item

    # split data to training & validation
    num_user, num_item, ratings = build_ml_len()
    matrix = build_rating_matrix(num_user, num_item, ratings)
    train_matrix = train_rating120(matrix)
    test = train_matrix[0]
    ratings = []
    for item in xrange(len(test)):
        rating = [0 for x in xrange(3)]
        if test[item] != 0:
            rating[1] = item
            rating[2] = test[item]
            rating = np.array(rating)
            ratings.append(rating)
    ratings = np.array(ratings)

    ts_model = ThompsonSampling(rating_matrix, user_feature, num_feature,
                                mu_Sigma_items, delta, lamda_p, ratings)
    result = ts_model._estimate(ratings)
    thompson_sampling_file = open('../result/ret_thompson_sampling_test', 'w')
    for item in result:
        print >> thompson_sampling_file, item
    thompson_sampling_file.close()
Exemplo n.º 2
0
def test(num_feature=30, delta=1, lamda_p=2, alpha=1.8):
    bmf_model = example()
    rating_matrix = bmf_model.matrix
    user_feature = bmf_model.user_features
    print 'user_feature.shape', len(user_feature)
    # required parameters
    beta_item = bmf_model.beta_item
    N = bmf_model.num_item
    # item mean and variance
    item_features = bmf_model.item_features

    num_user, num_item, ratings = build_ml_len()
    
    matrix = build_rating_matrix(num_user, num_item, ratings)
    train_matrix = train_rating120(matrix)  
    test = train_matrix[0]
    ratings = []
    for item in xrange(len(test)):
         rating = [0 for x in xrange(3)]
         if test[item] != 0:
             rating[1] = item
             rating[2] = test[item]
             rating = np.array(rating)
             ratings.append(rating)
    ratings = np.array(ratings)

    linearUCB_cluster_model = LinearUCBCluster(rating_matrix, user_feature, num_feature, item_features, delta, lamda_p, alpha, ratings) 
    linearUCB_cluster_model._init_parameters_cluster_user(20, num_user)
    
    result = linearUCB_cluster_model._estimate(ratings)
    cluster_linearUCB_file = open('../result/ret_cluster_Linear_UCB_test', 'w')
    for item in result:
        cluster_linearUCB_file.write(item + '\n')
    cluster_linearUCB_file.close()    
Exemplo n.º 3
0
def example():
    """simple test and performance measure
    """

    num_user, num_item, ratings = build_ml_len()
    # suffle_data
    np.random.shuffle(ratings)

    # split data to training & validation
    train_pct = 0.9
    train_size = int(train_pct * len(ratings))
    train = ratings[:train_size]
    validation = ratings[train_size:]

    # params
    num_feature = 10
    mf_model = MatrixFactorization(num_user,
                                   num_item,
                                   num_feature,
                                   train,
                                   validation,
                                   max_rating=5,
                                   min_rating=1,
                                   batch_size=100000)

    start_time = time.clock()
    mf_model.estimate(5)
    end_time = time.clock()
    print "time spend = %.3f" % (end_time - start_time)

    return mf_model
Exemplo n.º 4
0
def test(num_feature=30, delta=1, lamda_p=2, alpha=1.8):
    bmf_model = example()
    rating_matrix = bmf_model.matrix
    user_feature = bmf_model.user_features
    print 'user_feature.shape', len(user_feature)
    # required parameters
    beta_item = bmf_model.beta_item
    N = bmf_model.num_item
    # item features from MAP solution
    item_features = bmf_model.item_features
    
    # split data to training & validation
    num_user, num_item, ratings = build_ml_len()
    matrix = build_rating_matrix(num_user, num_item, ratings)
    train_matrix = train_rating120(matrix)  
    test = train_matrix[0]
    ratings = []
    for item in xrange(len(test)):
         rating = [0 for x in xrange(3)]
         if test[item] != 0:
             rating[1] = item
             rating[2] = test[item]
             rating = np.array(rating)
             ratings.append(rating)
    ratings = np.array(ratings)
   
    ts_model = EpsilonGreedy(rating_matrix, user_feature, num_feature, item_features, delta, lamda_p, alpha, ratings) 
    result = ts_model._estimate(ratings)
    thompson_sampling_file = open('../result/ret_linear_UCB_test', 'w')
    for item in result:
        print>>thompson_sampling_file, item
    thompson_sampling_file.close()    
Exemplo n.º 5
0
def draw_sparsity_user():
    num_user, num_item, ratings = build_ml_len()
    matrix = build_rating_matrix(num_user, num_item, ratings)
    print matrix.shape
    
    x = np.arange(num_item)
      
    plt.plot(x, matrix[6], 'k.')
    plt.axis([0,1000,1,6])
      
    plt.show()
Exemplo n.º 6
0
Arquivo: test.py Projeto: KeSyren/pmf
    def setUp(self):
        num_user, num_item, ratings = build_ml_len()
        np.random.shuffle(ratings)
        self.num_user = num_user
        self.num_item = num_item
        self.ratings = ratings
        self.num_feature = 10

        train_pct = 0.9
        train_size = int(train_pct * len(self.ratings))
        self.train = self.ratings[:train_size]
        self.validation = self.ratings[train_size:]
Exemplo n.º 7
0
def test(num_feature=30, delta=1, lamda_p=2, alpha=1.8):
    bmf_model = example()
    rating_matrix = bmf_model.matrix
    user_feature = bmf_model.user_features
    print 'user_feature.shape', len(user_feature)
    # required parameters
    beta_item = bmf_model.beta_item
    N = bmf_model.num_item
    # item features from MAP solution
    #     item_features = bmf_model.item_features
    #
    #     mat = np.matrix(item_features)
    #     dataframe = pd.DataFrame(data=mat.astype(float))
    #     dataframe.to_csv('../result/item_feature.csv', sep=',', header=False, float_format='%.2f', index=False)
    df = pd.read_csv('../result/item_feature.csv', sep=',', header=None)
    item_features = np.array(df)

    # split data to training & validation
    num_user, num_item, ratings = build_ml_len()
    matrix = build_rating_matrix(num_user, num_item, ratings)
    train_matrix = train_rating120(matrix)
    #     test = train_matrix[0]
    #     ratings = []
    #     for item in xrange(len(test)):
    #          rating = [0 for x in xrange(3)]
    #          if test[item] != 0:
    #              rating[1] = item
    #              rating[2] = test[item]
    #              rating = np.array(rating)
    #              ratings.append(rating)
    #     ratings = np.array(ratings)

    UCB_Dependence_model = LinearUCBDependence(rating_matrix, user_feature,
                                               num_feature, item_features,
                                               delta, lamda_p, alpha, ratings)

    result = UCB_Dependence_model._estimate(ratings)
    UCB_Dependence_model = open('../result/ret_linear_UCB_dependence', 'w')
    for item in result:
        print >> UCB_Dependence_model, item
    UCB_Dependence_model.close()
Exemplo n.º 8
0
def test(num_feature=30, delta=1, lamda_p=2):
    bmf_model = example()
    rating_matrix = bmf_model.matrix
    user_feature = bmf_model.user_features
    print 'user_feature.shape', len(user_feature)
    # required parameters
    beta_item = bmf_model.beta_item
    N = bmf_model.num_item
    # item mean and variance
    mu_Sigma_items = bmf_model.ret_mu_Sigma_item

    num_user, num_item, ratings = build_ml_len()

    matrix = build_rating_matrix(num_user, num_item, ratings)

    #     train_matrix = train_rating120(matrix)
    #     test = train_matrix[0]
    #     ratings = []
    #     for item in xrange(len(test)):
    #          rating = [0 for x in xrange(3)]
    #          if test[item] != 0:
    #              rating[1] = item
    #              rating[2] = test[item]
    #              rating = np.array(rating)
    #              ratings.append(rating)
    #     ratings = np.array(ratings)

    ts_model = ThompsonSamplingCluster(rating_matrix, user_feature,
                                       num_feature, mu_Sigma_items, delta,
                                       lamda_p, ratings)
    ts_model._init_parameters_cluster_user(20, num_user)

    result = ts_model._estimate(ratings)
    thompson_sampling_file = open('../result/ret_cluster_thompson_sampling',
                                  'w')
    for item in result:
        thompson_sampling_file.write(item + '\n')
    thompson_sampling_file.close()
Exemplo n.º 9
0
def test():
    num_user, num_item, ratings = build_ml_len()
    np.random.shuffle(ratings)
    matrix = build_rating_matrix(num_user, num_item, ratings)
    train_matrix = train_rating120(matrix)  
    test = train_matrix[0]
#     ratings = []
#     for item in xrange(len(test)):
#          rating = [0 for x in xrange(3)]
#          if test[item] != 0:
#              rating[1] = item
#              rating[2] = test[item]
#              rating = np.array(rating)
#              ratings.append(rating)
#     ratings = np.array(ratings)
     
    
    random_model = RandomSampling(matrix, ratings)
    result = random_model._estimate(ratings, num_item)
    
    random_sampling_file = open('../result/ret_random_sampling', 'w')
    for item in result:
        print>>random_sampling_file, item
    random_sampling_file.close()
Exemplo n.º 10
0
import numpy as np

from util.load_data import build_ml_len
from mf.matrix_factorization import MatrixFactorization
from mf.bayesian_matrix_factorization import BayesianMatrixFactorization
from util.evaluation_metrics import RMSE

if __name__ == "__main__":

    # load MovieLens data
    num_user, num_item, ratings = build_ml_len()
    np.random.shuffle(ratings)

    # set feature numbers
    num_feature = 30

    # set max_iterations
    max_iter = 30

    # split data to training & testing
    train_pct = 0.9
    train_size = int(train_pct * len(ratings))
    train = ratings[:train_size]
    validation = ratings[train_size:]

    # models
    rec = MatrixFactorization(num_user,
                              num_item,
                              num_feature,
                              train,
                              validation,