Beispiel #1
0
    def __init__(self,
                 train_file,
                 test_file,
                 metadata_file=None,
                 similarity_matrix_file=None,
                 prediction_file=None,
                 neighbors=30,
                 similarity_metric="correlation",
                 space_type='\t'):
        ItemKNN.__init__(self,
                         train_file,
                         test_file,
                         prediction_file=prediction_file,
                         neighbors=neighbors,
                         similarity_metric=similarity_metric,
                         space_type=space_type)

        if metadata_file is None and similarity_matrix_file is None:
            print(
                "This algorithm needs a similarity matrix or a metadata file!")
            sys.exit(0)

        if metadata_file is not None:
            self.metadata = ReadFile(
                metadata_file, space_type=space_type).read_metadata(self.items)
            self.matrix = self.metadata['matrix'].T
        self.similarity_matrix_file = similarity_matrix_file
Beispiel #2
0
 def get_itemknn(self):
     ItemKNN(train_file=self.train_file,
             test_file=self.test_file,
             output_file=self.output_file,
             similarity_metric=self.similarity_metric,
             k_neighbors=int(self.k_neighbors),
             sep=self.sep,
             output_sep=self.output_sep).compute(verbose_evaluation=False)
Beispiel #3
0
"""

# # Cross Validation
# recommender = UserKNN()
#
# CrossValidation(input_file=db, recommender=recommender, dir_folds=folds_path, header=1, k_folds=5).compute()
#
# # # Simple
# UserKNN(tr, te).compute()
# # UserAttributeKNN(tr, te, metadata_file=metadata_user).compute()
# # UserAttributeKNN(tr, te, similarity_file=sm_user).compute()
"""

    ItemKNN

"""

# # Cross Validation
recommender = ItemKNN()

CrossValidation(input_file=db,
                recommender=recommender,
                dir_folds=folds_path,
                header=1,
                k_folds=5).compute()
#
# # Simple
# ItemKNN(tr, te).compute()
# ItemAttributeKNN(tr, te, metadata_file=metadata_item).compute()
# ItemAttributeKNN(tr, te, similarity_file=sm_item).compute()
    def run_recommenders(self, r):
        """
        1: Item KNN
        2: User KNN
        3: Matrix Factorization
        4: SVD++
        """

        flag = True

        if not self.unlabeled_data[r]:
            flag = False

            return [], flag

        else:
            if r == 1:
                rec = ItemKNN(self.labeled_files[r], self.unlabeled_files[r], as_similar_first=True)
                rec.read_files()
                rec.init_model()
                rec.train_baselines()
                rec.predict()
                self.recommenders_predictions.setdefault(r, rec.predictions)
            elif r == 2:
                rec = UserKNN(self.labeled_files[r], self.unlabeled_files[r])
                rec.read_files()
                rec.init_model()
                rec.train_baselines()
                rec.predict()
                self.recommenders_predictions.setdefault(r, rec.predictions)
            elif r == 3:
                rec = MatrixFactorization(self.labeled_files[r], self.unlabeled_files[r], random_seed=1, baseline=True)
                rec.read_files()
                rec.init_model()
                rec.fit()
                rec.predict()
                self.recommenders_predictions.setdefault(r, rec.predictions)
            elif r == 4:
                rec = SVDPlusPlus(self.labeled_files[r], self.unlabeled_files[r], random_seed=1)
                rec.read_files()
                rec.fit()
                rec.predict()
                self.recommenders_predictions.setdefault(r, rec.predictions)

            else:
                raise NameError('Invalid Recommender!')

            return rec.predictions, flag
Beispiel #5
0
CrossValidation(input_file=db,
                recommender=recommender,
                dir_folds=folds_path,
                header=1,
                k_folds=5).compute()

# # Simple
UserKNN(tr, te).compute()
# UserAttributeKNN(tr, te, metadata_file=metadata_user).compute()
# UserAttributeKNN(tr, te, similarity_file=sm_user).compute()
"""

    ItemKNN

"""

# # Cross Validation
recommender = ItemKNN()

CrossValidation(input_file=db,
                recommender=recommender,
                dir_folds=folds_path,
                header=1,
                k_folds=5).compute()
#
# # Simple
ItemKNN(tr, te).compute()
# ItemAttributeKNN(tr, te, metadata_file=metadata_item).compute()
# ItemAttributeKNN(tr, te, similarity_file=sm_item).compute()
Beispiel #6
0
           te_film,
           number_sample=20,
           confidence_measure='knn',
           ensemble_method=True).compute()  # 运行划分三个文件(每个用户预测20个对象评分)

    # UserKNN(tr_film, te_film, as_similar_first=True, k_neighbors=60).compute()

    # UserKNN(label_set1, te_film, as_similar_first=True, k_neighbors=60).compute()

    # UserKNN(label_set2, te_film, as_similar_first=True, k_neighbors=60).compute()

    # UserKNN(ensemble, te_film, as_similar_first=True, k_neighbors=60).compute()

    # ItemKNN(tr_film, te_film, as_similar_first=True).compute()

    ItemKNN(label_set1, te_film,
            as_similar_first=True).compute()  # Item_KNN以Labled_set_1为训练集的性能结果

    ItemKNN(label_set2, te_film,
            as_similar_first=True).compute()  # Item_KNN以Labled_set_2为训练集的性能结果

    ItemKNN(ensemble, te_film,
            as_similar_first=True).compute()  # Item_KNN以ensemble_set为训练集的性能结果

    # ECoRec(tr_film, te_film, number_sample=30, confidence_measure='ec', ensemble_method=True).compute()

    # ItemKNN(label_set1, te_film, as_similar_first=True).compute()

    # ItemKNN(label_set2, te_film, as_similar_first=True).compute()

    # ItemKNN(ensemble, te_film, as_similar_first=True).compute()