Beispiel #1
0
def crossValidate(logFile, test_size=0.01) :
    """
    :param recommender: we assume it has an s_recommend method
    :param test_size: how big is the test size in the test split
    :return: prints to screen and to logfile the cv average of the metrics
    """
    Kaggle = False

    if Kaggle == True:
        interactionsCsv = pd.read_csv("../input/train.csv")
        targetList = pd.read_csv("../input/target_playlists.csv").iloc[:, 0]
        tracksCsv = pd.read_csv("../input/tracks.csv")
    else:
        interactionsCsv = pd.read_csv("input/train.csv")
        targetList = pd.read_csv("input/target_playlists.csv").iloc[:, 0]
        tracksCsv = pd.read_csv("input/tracks.csv")

    cumulative_precision = [None] * 8
    cumulative_recall = [None] * 8
    cumulative_MAP = [None] * 8


    cf_parameters = {'topK': 80,
                         'alpha': 1,
                         'beta': 0.27,
                         'normalize_similarity': True,
                         'implicit': True,
                         'norm': 'l1'}

    for seed, i in zip([13, 17, 23, 33, 45, 57, 69, 77], range(8)):

        X_train, X_test = train_test_split(interactionsCsv, test_size=test_size, random_state=seed)
        urm_train = URM(X_train)
        urm_test = URM(X_test)

        urm = urm_train


        rp3b = RP3betaRecommender(urm.getCSR())
        rp3b.fit(**cf_parameters)


        cumulative_precision[i], cumulative_recall[i], cumulative_MAP[i] = evaluate_algorithm(urm_test, rp3b)


    cumulative_precision = np.array(cumulative_precision)
    cumulative_recall = np.array(cumulative_recall)
    cumulative_MAP = np.array(cumulative_MAP)

    print("Recommender, performance is: Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}"
              .format(cumulative_precision.mean(), cumulative_recall.mean(), cumulative_MAP.mean()))

    logFile.write("Test case: {}, Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}\n".format(cf_parameters,
                                                                                                  cumulative_precision.mean(),
                                                                                                  cumulative_recall.mean(),
                                                                                                  cumulative_MAP.mean()))
    logFile.flush()
                'weight_dict'    : {'cbi_weight': 0, 'cbu_weight': 0.325, 'cbf_weight': 0.2, 'rp3b_weight': 0.225, 'slim_weight': 0.325},
                'loadSLIM': True, 'slimPath': 'slim_G2.pickle'
                }

group_param = [group1_param, group2_param]

nphybrid = NewPopulationHybrid(matrices, group1, group2, group1_param, group2_param, enable_dict)

if submission:
    print("Preparing submission")
    recommended_items = nphybrid.m_recommend(targetList, nRec=10)
    generate_output(targetList, recommended_items, path="submission.csv")

else:
    cumulative_precision, cumulative_recall, cumulative_MAP = evaluate_algorithm(urm_test, nphybrid)
    print("Recommender, performance is: Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}"
          .format(cumulative_precision, cumulative_recall, cumulative_MAP))


    """             Print to File        """
    logFile = open("Tuning.txt", "a")
    logFile.write("Test group {}, params: {}, Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}\n"
                    .format(enable_dict, group_param, cumulative_precision.mean(), cumulative_recall.mean(),
                    cumulative_MAP.mean()))

    logFile.flush()


""" 
    FOR TUNING AFTER BUILDING MODEL
Beispiel #3
0
    'CBFNorm': 'l2'
}

groups = [group1, group2, group3]
group_params = [group1_param, group2_param, group3_param]

nphybrid = NewPopulationHybrid(matrices, groups, group_params, enable_dict)

if submission:
    print("Preparing submission")
    recommended_items = nphybrid.m_recommend(targetList, nRec=10)
    generate_output(targetList, recommended_items, path="submission.csv")

else:
    cumulative_precision, cumulative_recall, cumulative_MAP = tqdm(
        evaluate_algorithm(urm_test, nphybrid))
    print(
        "Recommender, performance is: Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}"
        .format(cumulative_precision, cumulative_recall, cumulative_MAP))
    """             Print to File        """
    logFile = open("Tuning_vittorio.txt", "a")
    logFile.write(
        "Test group {}, params: {}, Precision = {:.4f}, Recall = {:.4f}, MAP = {:.6f}\n"
        .format(enable_dict, group2_param, cumulative_precision.mean(),
                cumulative_recall.mean(), cumulative_MAP.mean()))

    logFile.flush()
"""
    FOR TUNING AFTER BUILDING MODEL

for i in range(70):
Beispiel #4
0
        'group_1_2_TH': group_1_2_TH
    }

    phy = PopulationHybrid(urm,
                           urm_t,
                           icm,
                           icm2,
                           enable_dict,
                           param_dict,
                           urm_test=urm_test)
    if submission:
        recommended_items = phy.m_recommend(targetList, nRec=10)
        generate_output(targetList, recommended_items)
    else:
        print("Evaluating")
        cumulative_precision, cumulative_recall, cumulative_MAP = evaluate_algorithm(
            urm_test, phy)
        print(
            "Recommender, performance is: Precision = {:.4f}, Recall = {:.4f}, MAP = {:.5f}"
            .format(cumulative_precision, cumulative_recall, cumulative_MAP))

elif htype == "uii":

    print("Fitting hybrid recommender")

    enable_dict = {
        'enableSVD': True,
        'enableSLIM': True,
        'enableCBF2': True,
        'enableP3A': False,
        'enableLFM': False,
        'enableRP3B': True