# print(f'New ranking: {all_new}') # print(f'Correct items: {all_correct[f]}') # print() # Calculating all metrics if len(all_correct) > 0: # print_rerank_count(all_old, all_new) # all_old, all_new, all_correct[f] = filter_sames(all_old, all_new, all_correct[f]) # print_rerank_count(all_old, all_new) # if metrics_positive_only: # all_correct[f], all_old, all_new = keep_pos_only(all_correct[f], all_old, all_new) if metric_to_use == 'MAP': for l in lambdas: measures_lambda[l][f'{metric_to_use}_{metrics_sizes[0]}_initial'].append( mapk(all_old, all_correct[f], metrics_sizes[0])) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[0]}_rerank'].append( mapk(all_new[l], all_correct[f], metrics_sizes[0])) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[1]}_initial'].append( mapk(all_old, all_correct[f], metrics_sizes[1])) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[1]}_rerank'].append( mapk(all_new[l], all_correct[f], metrics_sizes[1])) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[2]}_initial'].append( mapk(all_old, all_correct[f], test_item_amount)) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[2]}_rerank'].append( mapk(all_new[l], all_correct[f], test_item_amount)) elif metric_to_use == 'NDCG': for l in lambdas: measures_lambda[l][f'{metric_to_use}_{metrics_sizes[0]}_initial'].append( ndcg_at(all_old, all_correct[f], metrics_sizes[0])) measures_lambda[l][f'{metric_to_use}_{metrics_sizes[0]}_rerank'].append(
for i, r in enumerate(initial_pred_list[f]): save_correct_items(f, r) re_rank(extract_predictions(r), fold_item_init_dist[f][i], rerank_size) print( f'{input_rec_path}-{len(all_old)}-{len(all_new[1])}-{len(all_correct)}' ) # Calculating all metrics if len(all_correct) > 0: if metric_to_use == 'MAP': for l in lambdas: measures_lambda[l][ f'{metric_to_use}_{metrics_sizes[0]}_initial'].append( mapk(all_old, all_correct[f], metrics_sizes[0])) measures_lambda[l][ f'{metric_to_use}_{metrics_sizes[0]}_rerank'].append( mapk(all_new[l], all_correct[f], metrics_sizes[0])) measures_lambda[l][ f'{metric_to_use}_{metrics_sizes[1]}_initial'].append( mapk(all_old, all_correct[f], metrics_sizes[1])) measures_lambda[l][ f'{metric_to_use}_{metrics_sizes[1]}_rerank'].append( mapk(all_new[l], all_correct[f], metrics_sizes[1])) measures_lambda[l][ f'{metric_to_use}_{metrics_sizes[2]}_initial'].append( mapk(all_old, all_correct[f], rerank_size))
correct_items = [] for xx in initial_pred['correctItems']: correct_items.append([int(x) for x in xx.split(';')]) predictions = [] for index, row in initial_pred.iterrows(): cntr = 1 pred = [] while cntr <= test_item_amount: if type(row[f'p{cntr}']) is str: pred.append(int(row[f'p{cntr}'].split(';')[0])) cntr = cntr + 1 predictions.append(pred) map_10.append(mapk(predictions, correct_items, 10)) map_25.append(mapk(predictions, correct_items, 25)) prec_10.append(precision_at(predictions, correct_items, 10)) prec_25.append(precision_at(predictions, correct_items, 25)) ndcg_10.append(ndcg_at(predictions, correct_items, 10)) ndcg_25.append(ndcg_at(predictions, correct_items, 25)) final_df['test_amount'].append(test_item_amount) final_df['MAP_10'].append(mean(map_10)) final_df['MAP_25'].append(mean(map_25)) final_df['Prec_10'].append(mean(prec_10)) final_df['Prec_25'].append(mean(prec_25)) final_df['NDCG_10'].append(mean(ndcg_10)) final_df['NDCG_25'].append(mean(ndcg_25)) pd.DataFrame.from_dict(final_df).to_csv(