| import numpy as np |
| from scipy.sparse import csr_matrix |
|
|
| """ |
| Function to calculate the multi project matching results |
| |
| The Multi-Project Matching Feature uncovers synergy opportunities among various development banks and organizations by facilitating the search for similar projects |
| within a selected filter setting (filtered_df) and all projects (project_df). |
| """ |
|
|
| def calc_multi_matches(filtered_df, project_df, similarity_matrix, top_x): |
| """ |
| filtered_df: df with applied filters |
| project_df: df with all projects |
| similarity_matrix: np sparse matrix with all similarities between projects |
| top_x: top x project which should be displayed |
| """ |
|
|
| |
| if not isinstance(similarity_matrix, csr_matrix): |
| similarity_matrix = csr_matrix(similarity_matrix) |
|
|
| |
| filtered_indices = filtered_df.index.to_list() |
| project_indices = project_df.index.to_list() |
|
|
| |
| match_matrix = similarity_matrix[project_indices, :][:, filtered_indices] |
| dense_match_matrix = match_matrix.toarray() |
| flat_matrix = dense_match_matrix.flatten() |
| |
| |
| top_15_indices = np.argsort(flat_matrix)[-top_x:] |
|
|
| |
| top_15_2d_indices = np.unravel_index(top_15_indices, dense_match_matrix.shape) |
| |
| |
| top_15_values = flat_matrix[top_15_indices] |
|
|
| |
| org_rows = [] |
| org_cols = [] |
| for value, row, col in zip(top_15_values, top_15_2d_indices[0], top_15_2d_indices[1]): |
| original_row_index = project_indices[row] |
| original_col_index = filtered_indices[col] |
| org_rows.append(original_row_index) |
| org_cols.append(original_col_index) |
| |
|
|
| |
|
|
| """ |
| p1_df: first results of match |
| p2_df: matching result |
| |
| matches are displayed through the indecies od p1 and p2 dfs |
| |
| match1 p1_df.iloc[0] & p2_df.iloc[0] |
| match2 p1_df.iloc[1] & p2_df.iloc[1] |
| """ |
| p1_df = filtered_df.loc[org_cols].copy() |
| p1_df['similarity'] = top_15_values |
|
|
| p2_df = project_df.loc[org_rows].copy() |
| p2_df['similarity'] = top_15_values |
|
|
| |
| return p1_df, p2_df |
|
|