Jan Mühlnikel
commited on
Commit
·
0ef6d21
1
Parent(s):
7d8805d
experiment
Browse files
functions/{single_similar.py → single_project_matching.py}
RENAMED
|
@@ -1,54 +1,42 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
import numpy as np
|
| 3 |
from scipy.sparse import csr_matrix
|
| 4 |
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# filter out just projects from filtered df
|
| 9 |
-
filtered_indices = filtered_df.index.tolist()
|
| 10 |
-
|
| 11 |
-
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)}
|
| 12 |
-
|
| 13 |
-
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices]
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
top_10_indices_descending = sorted_indices[-10:][::-1]
|
| 19 |
-
#top_10_original_indices = [index_position_mapping[position] for position in top_10_indices_descending]
|
| 20 |
-
top_10_values_descending = project_row[top_10_indices_descending]
|
| 21 |
-
|
| 22 |
-
result_df = filtered_df.iloc[top_10_indices_descending]
|
| 23 |
-
result_df["similarity"] = top_10_values_descending
|
| 24 |
-
|
| 25 |
-
return result_df
|
| 26 |
"""
|
| 27 |
|
| 28 |
def find_similar(p_index, similarity_matrix, filtered_df, top_x):
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if not isinstance(similarity_matrix, csr_matrix):
|
| 31 |
similarity_matrix = csr_matrix(similarity_matrix)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
filtered_indices =
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)}
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices]
|
| 41 |
-
|
| 42 |
-
# Extract the row for the selected project efficiently
|
| 43 |
-
# Convert the sparse row slice to a dense array for argsort function
|
| 44 |
project_row = filtered_column_sim_matrix.getrow(p_index).toarray().ravel()
|
| 45 |
|
| 46 |
-
#
|
| 47 |
sorted_indices = np.argsort(project_row)[-top_x:][::-1]
|
| 48 |
top_indices = [index_position_mapping[i] for i in sorted_indices]
|
| 49 |
top_values = project_row[sorted_indices]
|
| 50 |
|
| 51 |
-
#
|
| 52 |
result_df = filtered_df.loc[top_indices]
|
| 53 |
result_df['similarity'] = top_values
|
| 54 |
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from scipy.sparse import csr_matrix
|
| 3 |
|
| 4 |
"""
|
| 5 |
+
Function to find similar project for the single project matching
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
Single Project Matching empowers you to choose an individual project using
|
| 8 |
+
either the project IATI ID or title, and then unveils the top x projects within a filter (filtered_df) that
|
| 9 |
+
bear the closest resemblance to your selected one (p_index).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
|
| 12 |
def find_similar(p_index, similarity_matrix, filtered_df, top_x):
|
| 13 |
+
"""
|
| 14 |
+
p_index: index of selected project
|
| 15 |
+
similarity_matrix: matrix with similarities of all projects
|
| 16 |
+
filtered_df: df with filter applied
|
| 17 |
+
top_x: top x project which should be displayed
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
# convert npz sparse matrix into csr matrix
|
| 21 |
if not isinstance(similarity_matrix, csr_matrix):
|
| 22 |
similarity_matrix = csr_matrix(similarity_matrix)
|
| 23 |
|
| 24 |
+
# filter out just projects from filtered_df
|
| 25 |
+
filtered_indices = filtered_df.index.tolist()
|
| 26 |
+
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices]
|
| 27 |
|
| 28 |
+
# create a mapping from new position to original indices
|
| 29 |
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)}
|
| 30 |
|
| 31 |
+
# select just the row of th similarity matrix of the selected project index
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
project_row = filtered_column_sim_matrix.getrow(p_index).toarray().ravel()
|
| 33 |
|
| 34 |
+
# find top_x indices with the highest similarity scores in the row
|
| 35 |
sorted_indices = np.argsort(project_row)[-top_x:][::-1]
|
| 36 |
top_indices = [index_position_mapping[i] for i in sorted_indices]
|
| 37 |
top_values = project_row[sorted_indices]
|
| 38 |
|
| 39 |
+
# create result df with all top_x similar projects
|
| 40 |
result_df = filtered_df.loc[top_indices]
|
| 41 |
result_df['similarity'] = top_values
|
| 42 |
|
similarity_page.py
CHANGED
|
@@ -16,7 +16,7 @@ from functions.filter_projects import filter_projects
|
|
| 16 |
from functions.filter_single import filter_single
|
| 17 |
from functions.multi_project_matching import calc_multi_matches
|
| 18 |
from functions.same_country_filter import same_country_filter
|
| 19 |
-
from functions.
|
| 20 |
#import psutil
|
| 21 |
import os
|
| 22 |
import gc
|
|
|
|
| 16 |
from functions.filter_single import filter_single
|
| 17 |
from functions.multi_project_matching import calc_multi_matches
|
| 18 |
from functions.same_country_filter import same_country_filter
|
| 19 |
+
from functions.single_project_matching import find_similar
|
| 20 |
#import psutil
|
| 21 |
import os
|
| 22 |
import gc
|