Exemple #1
0
def MAE_Generate_resultFile(K_start, K_end, K_step, path, path_train,
                            path_test):
    rec = Recommendation(path, path_train, path_test)
    K = K_start
    result_file = os.getcwd() + '\\result\\resultOfMAE_' + os.path.basename(
        path) + '.csv'
    with open(result_file, 'w') as result_f:
        result_f.write(
            'A Hybrid User Similarity Model for Collaborative Filtering\n')
        result_f.write(
            'num_user:%d\nnum_items:%d\nranting:%d\nSparsity level:%.3f\n' %
            (rec.num_users, rec.num_items, rec.num_rating, rec.num_rating /
             (rec.num_items * rec.num_users)))
        result_f.write('{}'.format('K') + '\t' + 'MAE' + '\t' + 'NMAE' + '\n')
        while K <= K_end:
            preditmatrix_bingxing = np.load(
                os.getcwd() + '\\out_file\\predictMatrix_{}_'.format(K) +
                os.path.basename(path_train) + '_bingxing.npy')
            MAE_result, NMAE_result = Evaluate_MAE_AND_NMAE(
                preditmatrix_bingxing, rec.testMatrix)
            # "{} {}".format("hello", "world")
            line = '{}'.format(K) + '\t' + str(MAE_result) + '\t' + str(
                NMAE_result) + '\n'
            result_f.write(line)
            K += K_step
Exemple #2
0
 def add(self, entry):
     new_rec = Recommendation()
     df = pd.read_csv('data_armz.csv', sep=';')
     new_values = pd.DataFrame(entry)
     new_values.head()
     #Deleting possible providers - they cause the BD to be biased
     new_values.drop(
         new_values[new_values.QUANTIDADE.astype(int) > 5].index,
         inplace=True)
     new_df = df.append(new_values, ignore_index=True)
     new_df.to_csv(r'data_armz.csv', sep=';', index=False)
     status = new_rec.retrain_model()
     print(status)
     return (
         "BD atualizada! Compras de quantidade superior a 5 foram desconsideradas."
     )
Exemple #3
0
def MAE_Generate_resultFile(K_start, K_end, K_step, path, path_train, path_test, algorithmname):
    rec = Recommendation(path, path_train, path_test)
    K = K_start
    if algorithmname == 'PCC':
        result_file = os.getcwd() + '\\result\\PCC\\MAE_' + os.path.basename(path) + '.csv'
    if algorithmname == 'Hybird':
        result_file = os.getcwd() + '\\result\\Hybird\\resultOfMAE_' + os.path.basename(path) + '.csv'

    with open(result_file, 'w') as result_f:
        if algorithmname == 'PCC':
            result_f.write('PCC Model for Collaborative Filtering\n')
        if algorithmname == 'Hybird':
            result_f.write('A Hybrid User Similarity Model for Collaborative Filtering\n')
        result_f.write('num_user:%d\nnum_items:%d\nranting:%d\nSparsity level:%.3f\n' % (
            rec.num_users, rec.num_items, rec.num_rating, rec.num_rating / (rec.num_items * rec.num_users)))
        result_f.write("%6.6s\t%6.6s\t%6.6s\n" % ('K', 'MAE', 'NMAE'))

        while K <= K_end:
            # pcc
            if algorithmname == 'PCC':
                preditmatrix_bingxing = np.load(
                    os.getcwd() + '\\out_file\\PCC\\PCC_predictMatrix_{}_'.format(K) + os.path.basename(
                        path_train) + '_bingxing.npy')
            # Hybird
            if algorithmname == 'Hybird':
                preditmatrix_bingxing = np.load(
                    os.getcwd() + '\\out_file\\Hybird\\predictMatrix_{}_'.format(K) + os.path.basename(
                        path_train) + '_bingxing.npy')
            MAE_result, NMAE_result = Evaluate_MAE_AND_NMAE(preditmatrix_bingxing, rec.testMatrix)
            # "{} {}".format("hello", "world")
            line = "%6.6s\t%6.6s\t%6.6s\n" % (K, str(MAE_result), str(NMAE_result))
            result_f.write(line)
            K += K_step
def Generate_HR_resultfile(K_start, K_end, K_step, path, path_train, path_test,
                           top_k, dataname):
    rec = Recommendation(path, path_train, path_test)
    K = K_start
    result_file = os.getcwd() + '\\result\\' + dataname + '\\HR_TOP' + str(
        top_k) + '_' + os.path.basename(path) + '.csv'
    with open(result_file, 'w') as result_f:
        if dataname == 'PCC':
            result_f.write('PCC Model for Collaborative Filtering\n')
            filename = 'PCC_predictMatrix'
        if dataname == 'Hybird':
            result_f.write(
                'A Hybrid User Similarity Model for Collaborative Filtering\n')
            filename = 'predictMatrix'
        result_f.write(
            'num_user:%d\nnum_items:%d\nranting:%d\nSparsity level:%.3f\n' %
            (rec.num_users, rec.num_items, rec.num_rating, rec.num_rating /
             (rec.num_items * rec.num_users)))
        result_f.write("%6.6s\t%6.6s\n" % ('K', 'HR'))
        while K <= K_end:
            preditmatrix_bingxing = np.load(os.getcwd() + '\\out_file\\' +
                                            dataname + '\\' + filename +
                                            '_{}_'.format(K) +
                                            os.path.basename(path_train) +
                                            '_bingxing.npy')
            hr_result = Evaluate_HR(preditmatrix_bingxing, rec, top_k)
            line = "%6.6s\t%6.6s\n" % (K, str(hr_result))
            result_f.write(line)
            K += K_step
Exemple #5
0
def HR_Generate_result(path, path_train, path_test, top_k):
    preditmatrix_bingxing = np.load(os.getcwd() +
                                    '\\out_file\\predictMatrix_{}_'.format(8) +
                                    os.path.basename(path_train) +
                                    '_bingxing.npy')
    rec = Recommendation(path, path_train, path_test)

    return Evaluate_HR(preditmatrix_bingxing, rec.testMatrix, top_k)
Exemple #6
0
def DrawData_Distribute(dataname, trainname, testname):
    rec = Recommendation(dataname, trainname, testname)
    alldata_matrix = rec.Transform_csv_To_RatingMatrix(dataname)
    num_user = alldata_matrix.shape[0]
    count_rating_list = []
    for i in range(num_user):
        count_rating_list.append(sp.dok_matrix.count_nonzero(
            alldata_matrix[i]))
    # 创建一个点数为 8 x 6 的窗口, 并设置分辨率为 80像素/每英寸
    plt.figure(figsize=(8, 6), dpi=80)
    # 再创建一个规格为 1 x 1 的子图
    plt.subplot(1, 1, 1)
    plt.margins(0.5)
    # 柱子总数
    N = len(count_rating_list)
    # 包含每个柱子对应值的序列
    values = count_rating_list
    # 包含每个柱子下标的序列
    index = np.arange(N)
    # 柱子的宽度
    width = 0.01
    # 绘制柱状图, 每根柱子的颜色为紫罗兰色
    p2 = plt.bar(index, values, label="rainfall", color="#87CEFA")
    # 设置横轴标签
    plt.xlabel('UserID')
    # 设置纵轴标签
    plt.ylabel('NumOfRating')
    # 添加标题
    plt.title('mk-1m评分分布')
    print(count_rating_list)
    # 添加纵横轴的刻度
    plt.xticks(index[np.array(count_rating_list) > 60])
    # plt.xticks((index for index in range(0,num_user,10)),(i for i in range(0,num_user,10)))
    # 添加图例
    plt.legend((u"用户的评分数量", ))

    plt.show()
Exemple #7
0
def Generate_HR_resultfile(path, path_train, path_test, dataname):
    rec = Recommendation(path, path_train, path_test)
    top_k = 5
    result_file = os.getcwd(
    ) + '\\result\\' + dataname + '\\HR' + '_' + os.path.basename(
        path) + '.csv'
    with open(result_file, 'w') as result_f:
        result_f.write(
            'Deep Matrix Factorization Models for Recommender Systems \n')
        filename = 'predictMatrix'
        result_f.write(
            'num_user:%d\nnum_items:%d\nranting:%d\nSparsity level:%.3f\n' %
            (rec.num_users, rec.num_items, rec.num_rating, rec.num_rating /
             (rec.num_items * rec.num_users)))
        result_f.write("%9.9s\t%6.6s\n" % ('item_topk', 'HR'))
        while top_k <= 10:
            preditmatrix_bingxing = np.load(os.getcwd() + '\\out_file\\' +
                                            dataname + '\\' + filename + '_' +
                                            os.path.basename(path_train) +
                                            '_DMF.npy')
            hr_result = Evaluate_HR(preditmatrix_bingxing, rec, top_k)
            line = "%9.9s\t%6.6s\n" % (top_k, str(hr_result))
            result_f.write(line)
            top_k += 5
    def __init__(self, main, ad, influence, recommendation, input, graph, env, people):
        self.mainConfig = main
        self.influenceConfig = influence
        self.adConfig = ad
        self.recommendationConfig = recommendation
        self.inputConfig = input
        self.graphConfig = graph
        self.envConfig = env
        self.graph = people
        self.environment = Environment(self.graph, self.envConfig)
        pygame.init()
        surface = pygame.display.set_mode((1920, 1080))
        pygame.display.set_caption("User Interactions")
        clock = pygame.time.Clock()

        self.graphDrawrer = GraphDrawrer(self.graph, self.mainConfig, self.influenceConfig, self.adConfig, self.recommendationConfig, self.graphConfig, self.envConfig)

        counter = 0
        record = {}
        if self.mainConfig.influence and self.influenceConfig.type == "1":
            influence = Influence1(self.graph)
            influence.initialiseGraph()
            influence.startingNodes(int(self.influenceConfig.seed))

        if self.mainConfig.influence and self.influenceConfig.type == "2":
            influence = Influence2(self.graph)
            influence.initialiseGraph()
            influence.startingNodes(int(self.influenceConfig.seed))

        if self.mainConfig.recommendation:
            recommendation = Recommendation(self.graph)
            recommendation.intialise(self.recommendationConfig.seed)
            for person in self.graph.people:
                person.item1 = recommendation.getRecommendation(person, "item1")
            for person in self.graph.people:
                person.item2 = recommendation.getRecommendation(person, "item2")
            for person in self.graph.people:
                person.item3 = recommendation.getRecommendation(person, "item3")

        if self.mainConfig.social:
            social = Social(self.graph, self.graphDrawrer)

        if int(self.inputConfig.cull) < len(self.graph.connections):
            self.graph.trim(int(self.inputConfig.cull))
            for n in range(0,10):
                for connection in self.graph.connections:
                    if not (connection.between[0] in self.graph.people):
                        self.graph.connections.remove(connection)

                for connection in self.graph.connections:
                    if not (connection.between[1] in self.graph.people):
                        self.graph.connections.remove(connection)

#        LineIntersection(self.graph)

        for n in range(0, 20):
            Layout(self.graph).force_directed()
            if self.mainConfig.second:
                graph = pygame.transform.scale(self.graphDrawrer.make_frame(True), (960 , 1080))
                env = pygame.transform.scale(self.environment.make_frame(), (960, 1080))
                surface.blit(graph, (0, 0))
                surface.blit(env, (960, 0))
            else:
                surface.blit(self.graphDrawrer.make_frame(True), (0,0))
            pygame.display.update()
            clock.tick(60)

#        LineIntersection(self.graph)

        if self.mainConfig.advert:
            viral = Viral(self.graph, float(self.adConfig.seed), float(self.adConfig.email), float(self.adConfig.ad), float(self.adConfig.mu), float(self.adConfig.sigma))
            for person in self.graph.people:
                person.totalPart = 0
            viral.makeCampaign(self.graph)
            viral.seedEmail(self.graph, 5, 0)
            viral.seedAdvert(self.graph, 0.1, 0)
            record[counter] = viral.record(self.graph, 0)

        while self.graphDrawrer.go:
            counter += 1
            if self.mainConfig.social:
                if (counter%60 == 0):
                    for person in self.graph.people:
                        social.checkWall(person)
                        if random.random() < 0.1:
                            social.postPhoto(person)
                        if random.random() < 0.1:
                            social.postStatus(person)
            if self.mainConfig.second:
                graph = pygame.transform.scale(self.graphDrawrer.make_frame(), (960 , 1080))
                env = pygame.transform.scale(self.environment.make_frame(), (960, 1080))
                surface.blit(graph, (0, 0))
                surface.blit(env, (960, 0))
            else:
                surface.blit(self.graphDrawrer.make_frame(), (0,0))
            pygame.display.update()
            clock.tick(60)
            if counter % 5 == 0:
                if self.mainConfig.advert:
                    viral.checkEmail(self.graph, 0)
                    record[counter] = viral.record(self.graph, 0)
                if self.mainConfig.influence:
                    influence.process(counter)

        Plotter(record)
Exemple #9
0
    def runAlgorithms(self, algorithms, diffLists):
        self.startTime = datetime.datetime.now()
        timeRun = self.startTime.strftime('_%m_%d_%H_%M')
        filenameWriteRegret = os.path.join(save_address,
                                           'AccRegret' + timeRun + '.csv')
        filenameWritePara = os.path.join(
            save_address, 'ParameterEstimation' + timeRun + '.csv')

        # compute co-theta for every user
        tim_ = []
        BatchCumlateRegret = {}
        AlgRegret = {}
        CoThetaVDiffList = {}
        RDiffList = {}
        RVDiffList = {}

        CoThetaVDiff = {}
        RDiff = {}
        RVDiff = {}

        Var = {}

        # Initialization
        userSize = len(self.users)
        for alg_name, alg in algorithms.items():
            AlgRegret[alg_name] = []
            BatchCumlateRegret[alg_name] = []
            Var[alg_name] = []

        with open(filenameWriteRegret, 'w') as f:
            f.write('Time(Iteration)')
            f.write(
                ',' +
                ','.join([str(alg_name)
                          for alg_name in algorithms.iterkeys()]))
            f.write('\n')

        with open(filenameWritePara, 'w') as f:
            f.write('Time(Iteration)')
            diffLists.initial_write(f)
            f.write('\n')

        # Training
        shuffle(self.articles)
        for iter_ in range(self.training_iterations):
            article = self.articles[iter_]
            for u in self.users:
                noise = self.noise()
                reward = self.reward.getReward(u, article)
                reward += noise
                for alg_name, alg in algorithms.items():
                    alg.updateParameters(article, reward, u.id)

            if 'syncCoLinUCB' in algorithms:
                algorithms['syncCoLinUCB'].LateUpdate()

        #Testing
        for iter_ in range(self.testing_iterations):
            total = 0
            counter = 0
            for u in self.users:
                self.regulateArticlePool()  # select random articles

                noise = self.noise()
                #get optimal reward for user x at time t
                #pool_copy = copy.deepcopy(self.articlePool)
                OptimalReward, OptimalArticle = self.reward.getOptimalReward(
                    u, self.articlePool)
                # print "Optimal Reward", OptimalReward
                #OptimalReward = self.reward.getOptimalRecommendationReward(u, self.articlePool, self.k)
                OptimalReward += noise

                for alg_name, alg in algorithms.items():
                    if alg_name == 'FairUCB':
                        recommendation = alg.createIncentivizedRecommendation(
                            self.articlePool, u.id, self.k)
                        total += recommendation.k
                        counter += 1
                        # Have the user choose what is the best article for them
                        article, incentive = u.chooseArticle(recommendation)
                        # Tell the system the users choice
                        best_rec = Recommendation(1, [article])
                        noise = self.noise()
                        reward, pickedArticle = self.reward.getRecommendationReward(
                            u, best_rec, noise)
                        u.updateParameters(pickedArticle.contextFeatureVector,
                                           reward)
                    else:
                        recommendation = alg.createRecommendation(
                            self.articlePool, u.id, self.k)

                        # Assuming that the user will always be selecting one item for each iteration
                        #pickedArticle = recommendation.articles[0]
                        reward, pickedArticle = self.reward.getRecommendationReward(
                            u, recommendation, noise)
                        # print "ActualReward", reward
                    if (self.testing_method == "online"):
                        alg.updateParameters(pickedArticle, reward, u.id)
                        #alg.updateRecommendationParameters(recommendation, rewardList, u.id)
                        if alg_name == 'CLUB':
                            n_components = alg.updateGraphClusters(
                                u.id, 'False')

                    # print "Regret", float(OptimalReward - reward)
                    regret = OptimalReward - reward
                    AlgRegret[alg_name].append(regret)

                    if u.id == 0:
                        if alg_name in [
                                'LBFGS_random', 'LBFGS_random_around',
                                'LinUCB', 'LBFGS_gradient_inc'
                        ]:
                            means, vars = alg.getProb(self.articlePool, u.id)
                            Var[alg_name].append(vars[0])

                    # #update parameter estimation record
                    diffLists.update_parameters(alg_name, self, u, alg,
                                                pickedArticle, reward, noise)
            if 'syncCoLinUCB' in algorithms:
                algorithms['syncCoLinUCB'].LateUpdate()
            diffLists.append_to_lists(userSize)

            if iter_ % self.batchSize == 0:
                self.batchRecord(iter_)
                tim_.append(iter_)
                for alg_name in algorithms.iterkeys():
                    BatchCumlateRegret[alg_name].append(
                        sum(AlgRegret[alg_name]))

                with open(filenameWriteRegret, 'a+') as f:
                    f.write(str(iter_))
                    f.write(',' + ','.join([
                        str(BatchCumlateRegret[alg_name][-1])
                        for alg_name in algorithms.iterkeys()
                    ]))
                    f.write('\n')
                with open(filenameWritePara, 'a+') as f:
                    f.write(str(iter_))
                    diffLists.iteration_write(f)
                    f.write('\n')

        if (self.plot == True):  # only plot
            # plot the results
            f, axa = plt.subplots(1, sharex=True)
            for alg_name in algorithms.iterkeys():
                axa.plot(tim_, BatchCumlateRegret[alg_name], label=alg_name)
                print '%s: %.2f' % (alg_name, BatchCumlateRegret[alg_name][-1])
            axa.legend(loc='upper left', prop={'size': 9})
            axa.set_xlabel("Iteration")
            axa.set_ylabel("Regret")
            axa.set_title("Accumulated Regret")
            plt.show()

            # plot the estimation error of co-theta
            f, axa = plt.subplots(1, sharex=True)
            time = range(self.testing_iterations)
            diffLists.plot_diff_lists(axa, time)

            axa.legend(loc='upper right', prop={'size': 6})
            axa.set_xlabel("Iteration")
            axa.set_ylabel("L2 Diff")
            axa.set_yscale('log')
            axa.set_title("Parameter estimation error")
            plt.show()

        finalRegret = {}
        for alg_name in algorithms.iterkeys():
            finalRegret[alg_name] = BatchCumlateRegret[alg_name][:-1]
        return finalRegret
Exemple #10
0
    num_users = test.shape[0]
    num_items = test.shape[1]
    print(num_users)
    print(num_items)
    for (u, i) in test.keys():
        user_input.append(u)
        item_input.append(i)
        labels.append(test[u, i])
    print(len(user_input))
    print('加载数据完成!')
    return user_input, item_input, labels


if __name__ == '__main__':
    list_dataset = ntf.get_dataset_path(1)
    recommemdation = Recommendation(list_dataset[0], list_dataset[1],
                                    list_dataset[2])
    modelsavepath = os.getcwd() + '\\model\\' + os.path.basename(
        list_dataset[0]) + '_model.ckpt'
    num_users, num_items = recommemdation.num_users, recommemdation.num_items
    # 定义输入输出
    # 用的就是训练矩阵预测测试的值
    embedding = a = np.asarray(recommemdation.trainMatrix.toarray())
    keep_prob = tf.placeholder(tf.float32)
    with tf.name_scope('input_vector_user'):
        user_index = tf.placeholder(dtype='int32',
                                    shape=(None),
                                    name='user_index-input')
        user_vector = tf.nn.embedding_lookup(embedding, user_index)
    with tf.name_scope('input_vector_item'):
        item_index = tf.placeholder(dtype='int32',
                                    shape=(None),
Exemple #11
0
import sys

sys.path.append(".")
from flask import Flask, jsonify, request, json, Response
import json
import pandas as pd
from Recommendation import Recommendation
from CartRecom import CartRecom
from Bicluster.BiclusterRecom import BiclusterRecom
from BdManagement import BdManagement
from ClientRecom import ClientRecom

app = Flask(__name__)

retrain_recom = Recommendation()
cart_recom = CartRecom()
bicluster_recom = BiclusterRecom()
bd_manager = BdManagement()
client_recom = ClientRecom()

# ************************************************************* #
# *********************** MÉTODOS GET ************************* #
# ************************************************************* #


@app.route('/')
def home():
    return "Você se conectou."

    # Pcc_MAE = pd.DataFrame(columns=name,data=[mae_pcc,nmae_pcc])
    # Pcc_MAE.to_csv(os.getcwd()+'\\result\\PCC\\Pcc_MAE_'+ os.path.basename(list_dataset[0])+'.csv')

    ## Hybird
    mae_hybird, nmae_hybird = MAE_Generate_resultFile(4, 20, 4, list_dataset[0], list_dataset[1], list_dataset[2], 'Hybird')
    Hybird_MAE = pd.DataFrame(columns=name,data=[mae_hybird,nmae_hybird])
    Hybird_MAE.to_csv(os.getcwd()+'\\result\\Hybird\\Hybird_MAE_'+ os.path.basename(list_dataset[0])+'.csv')
    '''
    ## PMF
    # predictmatrix_pmf= np.load(os.getcwd()+'\\out_file\\PMF\\predictMatrix_m1-200-1000.csv_train.csv_PMF.npy')
    predictmatrix_pmf = np.load(os.getcwd() +
                                '\\out_file\\PMF\\predictMatrix_' +
                                os.path.basename(list_dataset[1]) + '_PMF.npy')
    Pmf_mae, Pmf_nmae = Evaluate_MAE_AND_NMAE(
        predictmatrix_pmf,
        Recommendation(list_dataset[0], list_dataset[1],
                       list_dataset[2]).testMatrix)
    Pmf_MAE = pd.DataFrame(columns=['pmf_MAE_NMAE'], data=[Pmf_mae, Pmf_nmae])
    Pmf_MAE.to_csv(os.getcwd() + '\\result\\PMF\\Pmf_MAE' +
                   os.path.basename(list_dataset[0]) + '.csv')
    '''
    # HR
    ## PCC
    # Generate_HR_resultfile(4, 20, 4, list_dataset[0], list_dataset[1], list_dataset[2], 5, 'PCC')
    # Generate_HR_resultfile(4, 20, 4, list_dataset[0], list_dataset[1], list_dataset[2], 10, 'PCC')
    ## Hybird
    Generate_HR_resultfile(4, 20, 4, list_dataset[0], list_dataset[1], list_dataset[2], 5, 'Hybird')
    Generate_HR_resultfile(4, 20, 4, list_dataset[0], list_dataset[1], list_dataset[2], 10, 'Hybird')
    '''
    ## Pmf
    rec = Recommendation(list_dataset[0], list_dataset[1], list_dataset[2])
    predictmatrix_pmf_hr = np.load(os.getcwd() +
Exemple #13
0
    typeparam = int(request.args.get('type'))
    userid = request.args.get('userid')
    if typeparam == 0:
        result = engine.listPopulerSong()
        return flask.Response(result)
    elif typeparam == 1:
        result = engine.ratingbyUserID(int(userid))
        return jsonify(result)
    elif typeparam == 2:
        result = engine.listPopulerGenre(int(userid))
        return flask.Response(result)
    elif typeparam == 3:
        return None
    elif typeparam == 4:
        return jsonify(request.args)


mining = Mining()


@app.route('/test', methods=['POST'])
def test():
    text = request.form['text']
    result = engine.spark.sql(text).toJSON().collect()
    return flask.Response(result)


if __name__ == '__main__':
    engine = Recommendation()
    app.run(host='0.0.0.0')
Exemple #14
0
 def createRecommendation(self, pool_articles, userID, k):
     articles = self.decide(pool_articles, userID, k)
     recommendation = Recommendation(k, articles)
     return recommendation
Exemple #15
0
    def testAlgorithms(self, algorithms, diffLists, AlgRegret, BatchCumlateRegret,\
                     Var, userSize, tim_, filenameWriteRegret, filenameWritePara):
        #Testing
        for iter_ in range(self.testing_iterations):
            total = 0
            counter = 0
            for u in self.users:
                self.regulateArticlePool()  # select random articles

                noise = self.noise()
                OptimalReward, OptimalArticle = self.reward.getOptimalReward(
                    u, self.articlePool)
                OptimalReward += noise

                for alg_name, alg in algorithms.items():
                    if alg_name == 'FairUCB':
                        recommendation = alg.createIncentivizedRecommendation(
                            self.articlePool, u.id, self.k)
                        total += recommendation.k
                        counter += 1
                        # Have the user choose what is the best article for them
                        article, incentive = u.chooseArticle(recommendation)
                        # Tell the system the users choice
                        best_rec = Recommendation(1, [article])
                        noise = self.noise()
                        reward, pickedArticle = self.reward.getRecommendationReward(
                            u, best_rec, noise)
                        u.updateParameters(pickedArticle.contextFeatureVector,
                                           reward)
                    else:
                        recommendation = alg.createRecommendation(
                            self.articlePool, u.id, self.k)

                        # Assuming that the user will always be selecting one item for each iteration
                        #pickedArticle = recommendation.articles[0]
                        reward, pickedArticle = self.reward.getRecommendationReward(
                            u, recommendation, noise)
                    if (self.testing_method == "online"):
                        alg.updateParameters(pickedArticle, reward, u.id)
                        if alg_name == 'CLUB':
                            n_components = alg.updateGraphClusters(
                                u.id, 'False')

                    regret = OptimalReward - reward
                    AlgRegret[alg_name].append(regret)

                    if u.id == 0:
                        if alg_name in [
                                'LBFGS_random', 'LBFGS_random_around',
                                'LinUCB', 'LBFGS_gradient_inc'
                        ]:
                            means, vars = alg.getProb(self.articlePool, u.id)
                            Var[alg_name].append(vars[0])

                    # #update parameter estimation record
                    diffLists.update_parameters(alg_name, self, u, alg,
                                                pickedArticle, reward, noise)
            if 'syncCoLinUCB' in algorithms:
                algorithms['syncCoLinUCB'].LateUpdate()
            diffLists.append_to_lists(userSize)

            if iter_ % self.batchSize == 0:
                self.batchRecord(iter_)
                tim_.append(iter_)
                for alg_name in algorithms.iterkeys():
                    BatchCumlateRegret[alg_name].append(
                        sum(AlgRegret[alg_name]))

                with open(filenameWriteRegret, 'a+') as f:
                    f.write(str(iter_))
                    f.write(',' + ','.join([
                        str(BatchCumlateRegret[alg_name][-1])
                        for alg_name in algorithms.iterkeys()
                    ]))
                    f.write('\n')
                with open(filenameWritePara, 'a+') as f:
                    f.write(str(iter_))
                    diffLists.iteration_write(f)
                    f.write('\n')
    def get_user_input(self) -> None:
        """Designed to be the command for the button 'ENTER':

        Here it stores user input of ONLY the new playlist generation (link, scale, playlist name)

        Furthermore, it also executes recommendation and from that, also computes the link
        to the newly generated Spotify Playlist

        It then passes it on to NewPlaylistOutput, to create a Top Level window for user to
        access the newly generated Spotify Playlist

        """

        # Here we update the playlist entry attribute
        self.playlist_entry = self._link_entry.get()

        # Here we update the scale entry attribute
        self.scale_entry = self._slider.get()

        # Update the desired new playlists name
        self.new_playlist_name = self._new_playlist_name_entry.get()

        self.token = self._token_entry.get()

        # Need to check if user has inputted everything needed, and only then go into this branch
        if self.playlist_entry != '' and self.scale_entry != '' \
                and self.new_playlist_name != '' and self.token != '':

            try:

                tk.Label(
                    self.root,
                    text=
                    'YOUR *PLAYLIST* INFORMATION HAS BEEN RECORDED. \n THANK YOU!',
                    font=("Proxima nova", "9", "bold"),
                    fg='white',
                    bg='black').grid()

                # Recommendation computation from module
                recommended_song_ids = Recommendation(
                    self.playlist_entry, self.scale_entry, self.data_obj,
                    self.sp, self.centroid_to_graph).action()

                # Generating new link
                new_playlist_link = SpotifyClient(recommended_song_ids,
                                                  self.new_playlist_name,
                                                  self.token).url

                # Calculating old playlist averages to display
                aves = [0] * 9  # 9 features
                num_songs = 0
                for song_id in recommended_song_ids:
                    num_songs += 1
                    features = self.data_obj.normalize_value(
                        get_features(song_id, self.sp))
                    # Removing duration(ms) and key
                    cols_removed_features = features[:3] + features[4:10]
                    for i in range(len(aves)):
                        aves[i] += cols_removed_features[i]

                aves = list(map(lambda ave: round(ave / num_songs * 100),
                                aves))

                output_playlist_summary = {
                    'Acousticness': aves[0],
                    'Danceability': aves[1],
                    'Energy': aves[2],
                    'Instrumentalness': aves[3],
                    'Valence': aves[4],
                    'Tempo': aves[5],
                    'Liveness': aves[6],
                    'Loudness': aves[7],
                    'Speechiness': aves[8]
                }

                # Running another Tkinter window (Top Level) to display
                # computations(aka new playlist)
                output_root = tk.Toplevel()
                output_window = NewPlaylistOutput(output_root,
                                                  new_playlist_link,
                                                  output_playlist_summary)
                output_window.run_window()
                output_root.mainloop()

            except KeyError:
                print(
                    '1 hour has passed and the authorization token has expired. \n'
                    'Please follow the same steps to regenerate another token, and try again.'
                )

            except TypeError:

                print(
                    'NOTICE: You have a song that is not registered with Spotify \n'
                    'Please input another playlist!')
def search(uid, max_distance, ideal_distance, current_location, current_time,
           event_location, event_time, spare_time, bias, mode):

    print('searching....')
    print('event_location', event_location)
    print('event_time', event_time)
    print('spare_time', spare_time)
    print('bias', bias)

    radius = DistanceCalculator.mile_to_meter(\
        min(ideal_distance, max_distance))*\
        scorer.DISTANCE_REGULARIZED

    print('radius', radius)

    keyword = set()
    for b in bias:
        keyword = keyword | set(database_getter.EVENT_TYPES[b])

    if spare_time < scorer.MAX_SPARE_TIME:
        keyword = keyword | scorer.SHORT_STOP_TYPES
    else:
        keyword = keyword | scorer.LONG_STOP_TYPES

    routes = defaultdict(Route)

    # search nearby places around both start ptr and dest ptr
    for location in [current_location, event_location]:
        if location != None:
            for k in keyword:
                for place in FindPlaces.get_nearby_places(location=location,
                                                          radius=radius,
                                                          keyword=k):
                    if place[1] not in routes:
                        routes[place[1]] = Route(stop=place,
                                                 origin=current_location,
                                                 dest=event_location,
                                                 mode=mode)
                        routes[place[1]].set_staying_time(uid)

    for stop_id, route in routes.items():
        print("=================", route.stop_name, "=================")
        print("goal distance scoring")
        route.add_score(
            scorer.goal_distance_func(
                DistanceCalculator.mile_to_meter(ideal_distance),
                route.total_dist))
        print(route.score)
        print("location history scoring")
        route.add_score(
            scorer.past_location_func(
                database_getter.is_past_location(route.stop_id)))
        print(route.score)
        print("staying time scoring")
        route.add_score(
            scorer.staying_time_func(spare_time - route.total_time,
                                     route.staying_time))
        print(route.score)
        print("event bias scoring")
        route.add_score(scorer.event_bias_func(bias, route.stop_types))
        print(route.score)

    recommendations = []
    for stop_id, route in sorted(routes.items(),
                                 key=lambda x: x[1].score,
                                 reverse=True)[:3]:
        report = '''
"{}" at "{}"
Primary location type: "{}"
It is {:.1f} miles away. It takes {} steps to walk there.
===========================================================
It will take you {} minutes to walk there.
You will arrive at {} if you leave now.
===========================================================
After completing this walk, your steps will be {} for today.
===========================================================
You can hang at this location for {} minutes before you start walking to your scheduled event.
            '''.format(
                    route.stop_name, route.stop_address,
                    route.stop_types[0],
                    DistanceCalculator.meter_to_mile(route.origin_to_stop[0])\
                        if route.origin_to_stop[0] != 0 else DistanceCalculator.meter_to_mile(
                        route.stop_to_dest[0]),
                    route.step_to_stop \
                        if route.step_to_stop != 0 else route.step_to_dest,
                    round(route.origin_to_stop[1]/60) \
                        if route.origin_to_stop[1] != 0 else round(route.stop_to_dest[1]/60),
                    (current_time+timedelta(
                        seconds=route.origin_to_stop[1])).strftime('%Y-%m-%d %H:%M:%S')\
                        if route.origin_to_stop[1] != 0 else (current_time+timedelta(
                            seconds=route.origin_to_stop[1])).strftime('%Y-%m-%d %H:%M:%S'),
                    route.step_to_stop+database_getter.get_user_current_step(uid)\
                        if route.step_to_stop != 0 \
                        else route.step_to_dest+database_getter.get_user_current_step(uid),
                    spare_time-route.trip_time
                )
        print(report)

        rec=Recommendation(
            uid=uid, type=0 if not bias else 1,
            start_time=current_time,
            duration=route.origin_to_stop[1] \
                if route.origin_to_stop[1] != 0 else route.step_to_stop[0],
            place_id=route.stop_id,
            place_name=route.stop_name,
            place_address=route.stop_address,
            place_types=route.stop_types,
            steps=route.step_to_stop \
                if route.step_to_stop != 0 else route.step_to_dest,
            report=report
        )
        recommendations.append(rec)
        Recommendation.add_to_db(rec)

    ## TODO
    if len(recommendations) < 3:
        recommendations.append(recommendations[0])

    return recommendations
    def get_user_input(self) -> None:
        """Designed to be the command for the button 'ENTER':

        Here it stores user input of ONLY the new playlist generation (link, scale, playlist name)

        Furthermore, it also executes recommendation and from that, also computes the link
        to the newly generated Spotify Playlist

        It then passes it on to NewPlaylistOutput, to create a Top Level window for user to
        access the newly generated Spotify Playlist

        Lastly, it also computes the normalized averages of the attributes of the old
        inputted playlist. Assigned as a dictionary, and passed on to NewPlaylistOutput
        to display with bar graphs.
        """

        # Here we update the playlist entry attribute
        self.playlist_entry = self._link_entry.get()

        # Here we update the scale entry attribute
        self.scale_entry = int(self._slider.get())

        # Update the desired new playlists name
        self.new_playlist_name = self._new_playlist_name_entry.get()

        # Need to check if user has inputted everything needed, and only then go into this branch
        if self.playlist_entry != '' and self.scale_entry != '' \
                and self.new_playlist_name != '':

            try:
                tk.Label(self.root,
                         text='YOUR *PLAYLIST* INFORMATION HAS BEEN RECORDED.'
                         ' \n THANK YOU!',
                         font=("Proxima nova", "9", "bold"),
                         fg='white',
                         bg='black').grid()

                # Recommendation computation from module
                recommended_song_ids = Recommendation(
                    self.playlist_entry, self.scale_entry, self.data_obj,
                    self.sp, self.centroid_to_graph).action()

                # Generating new link
                # new_playlist_link = SpotifyClient(recommended_song_ids,
                # self.new_playlist_name).url
                spotify_instance = Spotify_Client()
                new_playlist_link = spotify_instance.create_playlist(
                    self.new_playlist_name, recommended_song_ids)

                # Calculating old playlist averages to display
                aves = [0] * 9  # 9 features
                num_songs = 0
                for song_id in recommended_song_ids:
                    num_songs += 1
                    # features = self.data_obj.normalize_value(get_features(song_id, self.sp))

                    features = self.data_obj.normalize_value(
                        spotify_instance.get_song_features(song_id))

                    # Removing duration(ms) and key
                    cols_removed_features = features[:3] + features[4:10]
                    for i in range(len(aves)):
                        aves[i] += cols_removed_features[i]

                aves = list(map(lambda ave: round(ave / num_songs * 100),
                                aves))

                output_playlist_summary = {
                    'Acousticness': aves[0],
                    'Danceability': aves[1],
                    'Energy': aves[2],
                    'Instrumentalness': aves[3],
                    'Valence': aves[4],
                    'Tempo': aves[5],
                    'Liveness': aves[6],
                    'Loudness': aves[7],
                    'Speechiness': aves[8]
                }

                # Running another Tkinter window (Top Level) to
                # display computations(aka new playlist)
                output_root = tk.Toplevel()
                output_window = NewPlaylistOutput(output_root,
                                                  new_playlist_link,
                                                  output_playlist_summary)
                output_window.run_window()
                output_root.mainloop()

            # Case where there may be in song in user playlist that is not recognized by API
            except TypeError:
                print(
                    'There is a song in this playlist that the Spotipy API cannot read. \n'
                    'This is because this song is not defined in Spotify but rather '
                    'it most likely is from a local file. \n Please input a new playlist! '
                )
        else:
            print('Invalid playlist entry inputs.\nPlease input all entries!.')

        print(
            'Playlist generation over. If you want, input another playlist link, \n'
            'try to visualize, or close window!')
from BooksDataSet import BooksDataSet
from Recommendation import Recommendation
from surprise import KNNBasic
import heapq
from collections import defaultdict
from operator import itemgetter

testSubject = '85'
k = 10
user_id = 11676

data_set = BooksDataSet()

users = data_set.getUsersData()
books = data_set.getBooksData()
ratings = data_set.getRatingsData()

print('Print the loaded data')
data_set.printData()

print('Recommend the books based on rating count')
recommendation = Recommendation()
recommended_items_by_rating = recommendation.getRecommendationsByRatingCounts(
    ratings, books)
recommendation.printItem(recommended_items_by_rating)

print('Recommend the books based on collaborative user filtering algorithm')
ratings_matrix = recommendation.getRecommendationsByCfUser(user_id, ratings)
recommended = recommendation.recommendItem(user_id)
recommendation.printRecommendations(books, recommended)
                j = np.random.randint(num_items)
            user_input.append(user_vector_u)
            user_vector_j = sp.dok_matrix.transpose(train).toarray()[j]
            item_input.append(user_vector_j)
            labels.append(0)
    return user_input, item_input, labels


if __name__ == '__main__':
    batch_size = 256
    topK = 10
    csv_path = 'C:\\Users\\suxik\\Desktop\\text\\graduation-project-\\prepare_datasets\\test.csv'
    csv_path1 = 'C:\\Users\\suxik\Desktop\\text\graduation-project-\\prepare_datasets\\ml-1m.train.rating'
    # Loading data
    t1 = time()
    Rec = Recommendation(csv_path)
    train, testRatings, testNegatives = Rec.ratingMatrix, Rec.testRatings, Rec.testNegatives
    num_users, num_items = train.shape
    print("Load data done [%.1f s]. #user=%d, #item=%d, #train=%d, #test=%d" %
          (time() - t1, num_users, num_items, train.nnz, len(testRatings)))

    # Build model
    model = get_model(num_users, num_items, layers, reg_layers)
    model.compile(optimizer=SGD(lr=learning_rate), loss='binary_crossentropy')

    # Check Init performance
    t1 = time()
    (hits, ndcgs) = evaluate_model(model, testRatings, testNegatives, topK,
                                   evaluation_threads)
    hr, ndcg = np.array(hits).mean(), np.array(ndcgs).mean()
    print('Init: HR = %.4f, NDCG = %.4f [%.1f]' % (hr, ndcg, time() - t1))