def save_popularity_statistics(URM_object_list, URM_name_list, output_file_path): shape = URM_object_list[0].shape for URM_object in URM_object_list: assert URM_object.shape == shape n_items = shape[1] ignore_items = np.array([]) latex_file = open(output_file_path, "w") latex_file.write( "\tMax pop \t&Min pop \t&Avg pop \t&Gini Index \t&Kendall Tau \t&Shannon \t&Herfindahl \\\\ \n" ) base_item_popularity = None for URM_index, URM_name in enumerate(URM_name_list): URM_object = URM_object_list[URM_index] URM_object = sps.csc_matrix(URM_object) URM_string = "{}\t&".format(URM_name) item_popularity = np.ediff1d(URM_object.indptr) max_pop = item_popularity.max() min_pop = item_popularity.min() avg_pop = item_popularity.mean() gini_index = gini(item_popularity) if base_item_popularity is None: base_item_popularity = item_popularity.copy() kendall_tau, p_value = kendalltau(base_item_popularity, item_popularity) values_list = [max_pop, min_pop, avg_pop, gini_index, kendall_tau] metric_object_dict = { # "Gini Diversity": Gini_Diversity(n_items, ignore_items), "Shannon Entropy": Shannon_Entropy(n_items, ignore_items), "Herfindahl Index": Diversity_Herfindahl(n_items, ignore_items), } for metric_name, metric_object in metric_object_dict.items(): metric_object.recommended_counter = item_popularity.copy() metric_value = metric_object.get_metric_value() values_list.append(metric_value) URM_string += "\t&".join("{:.2f}".format(el) for el in values_list) latex_file.write(URM_string + "\\\\ \n")
def _create_empty_metrics_dict(cutoff_list, n_items, n_users, URM_train, URM_test, ignore_items, ignore_users, diversity_similarity_object): empty_dict = {} # global_RMSE_object = RMSE(URM_train + URM_test) for cutoff in cutoff_list: cutoff_dict = {} for metric in EvaluatorMetrics: if metric == EvaluatorMetrics.COVERAGE_ITEM: cutoff_dict[metric.value] = Coverage_Item(n_items, ignore_items) elif metric == EvaluatorMetrics.DIVERSITY_GINI: cutoff_dict[metric.value] = Gini_Diversity(n_items, ignore_items) elif metric == EvaluatorMetrics.SHANNON_ENTROPY: cutoff_dict[metric.value] = Shannon_Entropy(n_items, ignore_items) elif metric == EvaluatorMetrics.COVERAGE_USER: cutoff_dict[metric.value] = Coverage_User(n_users, ignore_users) elif metric == EvaluatorMetrics.DIVERSITY_MEAN_INTER_LIST: cutoff_dict[metric.value] = Diversity_MeanInterList(n_items, cutoff) elif metric == EvaluatorMetrics.DIVERSITY_HERFINDAHL: cutoff_dict[metric.value] = Diversity_Herfindahl(n_items, ignore_items) elif metric == EvaluatorMetrics.NOVELTY: cutoff_dict[metric.value] = Novelty(URM_train) elif metric == EvaluatorMetrics.AVERAGE_POPULARITY: cutoff_dict[metric.value] = AveragePopularity(URM_train) elif metric == EvaluatorMetrics.MAP: cutoff_dict[metric.value] = MAP() elif metric == EvaluatorMetrics.MRR: cutoff_dict[metric.value] = MRR() # elif metric == EvaluatorMetrics.RMSE: # cutoff_dict[metric.value] = global_RMSE_object elif metric == EvaluatorMetrics.DIVERSITY_SIMILARITY: if diversity_similarity_object is not None: cutoff_dict[metric.value] = copy.deepcopy(diversity_similarity_object) else: cutoff_dict[metric.value] = 0.0 empty_dict[cutoff] = cutoff_dict return empty_dict
def test_Shannon_Entropy(self): from Base.Evaluation.metrics import Shannon_Entropy n_items = 1000 shannon_entropy = Shannon_Entropy(n_items, ignore_items=np.array([])) shannon_entropy.recommended_counter = np.ones(n_items) assert np.isclose(9.96, shannon_entropy.get_metric_value(), atol=1e-2), "metric incorrect" shannon_entropy.recommended_counter = np.zeros(n_items) shannon_entropy.recommended_counter[0] = 1.0 assert np.isclose(0.0, shannon_entropy.get_metric_value(), atol=1e-3), "metric incorrect" shannon_entropy.recommended_counter = np.random.uniform(0, 100, n_items).astype(np.int) assert np.isclose(9.6, shannon_entropy.get_metric_value(), atol=1e-1), "metric incorrect"
def create_empty_metrics_dict(n_items, n_users, URM_train, ignore_items, ignore_users, cutoff, diversity_similarity_object): empty_dict = {} # from Base.Evaluation.ResultMetric import ResultMetric # empty_dict = ResultMetric() for metric in EvaluatorMetrics: if metric == EvaluatorMetrics.COVERAGE_ITEM: empty_dict[metric.value] = Coverage_Item(n_items, ignore_items) elif metric == EvaluatorMetrics.DIVERSITY_GINI: empty_dict[metric.value] = Gini_Diversity(n_items, ignore_items) elif metric == EvaluatorMetrics.SHANNON_ENTROPY: empty_dict[metric.value] = Shannon_Entropy(n_items, ignore_items) elif metric == EvaluatorMetrics.COVERAGE_USER: empty_dict[metric.value] = Coverage_User(n_users, ignore_users) elif metric == EvaluatorMetrics.DIVERSITY_MEAN_INTER_LIST: empty_dict[metric.value] = Diversity_MeanInterList(n_items, cutoff) elif metric == EvaluatorMetrics.DIVERSITY_HERFINDAHL: empty_dict[metric.value] = Diversity_Herfindahl( n_items, ignore_items) elif metric == EvaluatorMetrics.NOVELTY: empty_dict[metric.value] = Novelty(URM_train) elif metric == EvaluatorMetrics.AVERAGE_POPULARITY: empty_dict[metric.value] = AveragePopularity(URM_train) elif metric == EvaluatorMetrics.MAP: empty_dict[metric.value] = MAP() elif metric == EvaluatorMetrics.MRR: empty_dict[metric.value] = MRR() elif metric == EvaluatorMetrics.DIVERSITY_SIMILARITY: if diversity_similarity_object is not None: empty_dict[metric.value] = copy.deepcopy( diversity_similarity_object) else: empty_dict[metric.value] = 0.0 return empty_dict