import pandas as pd import numpy as np def calc_matches(filtered_df, project_df, similarity_matrix, top_x): # matching project2 can be nay project # indecies (rows) = project1 # columns = project2 # -> find matches # filter out all row considering the filter filtered_df_indecies_list = filtered_df.index project_df_indecies_list = project_df.index np.fill_diagonal(similarity_matrix, 0) match_matrix = similarity_matrix[filtered_df_indecies_list, :][:, project_df_indecies_list] best_matches_list = np.argsort(match_matrix, axis=None) if len(best_matches_list) < top_x: top_x = len(best_matches_list) # get row (project1) and column (project2) with highest similarity in filtered df top_indices = np.unravel_index(best_matches_list[-top_x:], match_matrix.shape) # get the corresponding similarity values top_values = match_matrix[top_indices] p1_df = filtered_df.iloc[top_indices[0]] p1_df["similarity"] = top_values p2_df = project_df.iloc[top_indices[1]] p2_df["similarity"] = top_values return p1_df, p2_df