import pandas as pd def count_map(actual_list, predicted_list, count_results): """ Funtion for count MAP metric :param actual_list: actual results from annotated datatest :param predicted_list: predicted results from searched engine :return: map value in question MAP value """ # Initialization empty list for results questions = [] k_stops = [] frequencies = [] mAPs = [] # For every question for q, (actual, predicted) in enumerate(zip(actual_list, predicted_list), 1): k_stop = None # initialization value who stop cycle for this question ap_sum = 0 # initialization sum AP on question count = 0 # count value k_stop # Loop throw values k for x, pred_value in enumerate(predicted[:count_results], 1): act_set = set(actual) pred_set = set(predicted[:x]) precision_at_k = len(act_set & pred_set) / x if pred_value in actual: rel_k = 1 else: rel_k = 0 ap_sum += precision_at_k * rel_k # If we have found all the relevant values and we don't have k_stop yet, we stop if len(act_set) == ap_sum and k_stop is None: k_stop = x count += 1 # If we haven't reached k_stop by 15, we set it to 15 if k_stop is None: k_stop = count_results # Count mAP for question ap_q = ap_sum / len(actual) # Save results to list questions.append(q) k_stops.append(k_stop) frequencies.append(count) mAPs.append(round(ap_q, 2)) # Create DataFrame from results df_results = pd.DataFrame({'Question': questions, 'k_stop': k_stops, 'Frequency': frequencies, 'mAP': mAPs}) # Count total mAP total_mAP = round(df_results['mAP'].mean(), 2) print(f"MAP Metric for {count_results} is: {total_mAP}") k_stop_counts = df_results['k_stop'].value_counts() print(f"Count of K_stop \n{k_stop_counts}") return df_results, round(total_mAP,2)