|
import pandas as pd |
|
import numpy as np |
|
from scipy.sparse import csr_matrix |
|
|
|
""" |
|
def find_similar(p_index, similarity_matrix, filtered_df, top_x): |
|
|
|
# filter out just projects from filtered df |
|
filtered_indices = filtered_df.index.tolist() |
|
|
|
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)} |
|
|
|
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices] |
|
|
|
# filter out the row of the selected poject |
|
project_row = filtered_column_sim_matrix[p_index] |
|
sorted_indices = np.argsort(project_row) |
|
top_10_indices_descending = sorted_indices[-10:][::-1] |
|
#top_10_original_indices = [index_position_mapping[position] for position in top_10_indices_descending] |
|
top_10_values_descending = project_row[top_10_indices_descending] |
|
|
|
result_df = filtered_df.iloc[top_10_indices_descending] |
|
result_df["similarity"] = top_10_values_descending |
|
|
|
return result_df |
|
""" |
|
def find_similar(p_index, similarity_matrix, filtered_df, top_x): |
|
|
|
if not isinstance(similarity_matrix, csr_matrix): |
|
similarity_matrix = csr_matrix(similarity_matrix) |
|
|
|
|
|
filtered_indices = filtered_df.index.tolist() |
|
|
|
|
|
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)} |
|
|
|
|
|
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices] |
|
|
|
|
|
|
|
project_row = filtered_column_sim_matrix.getrow(p_index).toarray().ravel() |
|
|
|
|
|
sorted_indices = np.argsort(project_row)[-top_x:][::-1] |
|
top_indices = [index_position_mapping[i] for i in sorted_indices] |
|
top_values = project_row[sorted_indices] |
|
|
|
|
|
result_df = filtered_df.loc[top_indices] |
|
result_df['similarity'] = top_values |
|
|
|
return result_df |
|
|