def test_Diversity_list_all_equals(self): from Base.Evaluation.metrics import Diversity_MeanInterList import scipy.sparse as sps n_items = 3 n_users = 10 cutoff = min(5, n_items) # create recommendation list URM_predicted_row = [] URM_predicted_col = [] diversity_list = Diversity_MeanInterList(n_items, cutoff) item_id_list = np.arange(0, n_items, dtype=np.int) for n_user in range(n_users): np.random.shuffle(item_id_list) recommended = item_id_list[:cutoff] URM_predicted_row.extend([n_user] * cutoff) URM_predicted_col.extend(recommended) diversity_list.add_recommendations(recommended) object_diversity = diversity_list.get_metric_value() URM_predicted_data = np.ones_like(URM_predicted_row) URM_predicted_sparse = sps.csr_matrix( (URM_predicted_data, (URM_predicted_row, URM_predicted_col)), dtype=np.int) co_counts = URM_predicted_sparse.dot(URM_predicted_sparse.T).toarray() np.fill_diagonal(co_counts, 0) all_user_couples_count = n_users**2 - n_users diversity_cumulative = 1 - co_counts / cutoff np.fill_diagonal(diversity_cumulative, 0) diversity_cooccurrence = diversity_cumulative.sum( ) / all_user_couples_count assert np.isclose(diversity_cooccurrence, object_diversity, atol=1e-4), "metric incorrect"
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 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