Jan Mühlnikel
sparse matrix changes
f17e764
raw
history blame
2.19 kB
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):
# Ensure the similarity_matrix is in a suitable sparse format like CSR
if not isinstance(similarity_matrix, csr_matrix):
similarity_matrix = csr_matrix(similarity_matrix)
# Filter out just projects from filtered_df
filtered_indices = filtered_df.index.tolist()
# Create a mapping from new position to original indices
index_position_mapping = {position: index for position, index in enumerate(filtered_indices)}
# Extract the submatrix corresponding to the filtered indices
filtered_column_sim_matrix = similarity_matrix[:, filtered_indices]
# Extract the row for the selected project efficiently
# Convert the sparse row slice to a dense array for argsort function
project_row = filtered_column_sim_matrix.getrow(p_index).toarray().ravel()
# Find top_x indices with the highest similarity scores
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]
# Prepare the result DataFrame
result_df = filtered_df.loc[top_indices]
result_df['similarity'] = top_values
return result_df