Jan Mühlnikel
commited on
Commit
·
cce39ff
1
Parent(s):
046c858
experiment
Browse files- functions/calc_matches.py +6 -82
functions/calc_matches.py
CHANGED
|
@@ -13,89 +13,13 @@ def calc_matches(filtered_df, project_df, similarity_matrix, top_x):
|
|
| 13 |
if not isinstance(similarity_matrix, csr_matrix):
|
| 14 |
similarity_matrix = csr_matrix(similarity_matrix)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
project_df_indices = project_df.index.to_list()
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
project_df_index_map = {i: index for i, index in enumerate(project_df_indices)}
|
| 23 |
|
| 24 |
-
|
| 25 |
-
match_matrix = similarity_matrix[filtered_df_indices, :][:, project_df_indices]
|
| 26 |
-
|
| 27 |
-
coo = match_matrix.tocoo()
|
| 28 |
-
|
| 29 |
-
data = coo.data
|
| 30 |
-
row_indices = coo.row
|
| 31 |
-
col_indices = coo.col
|
| 32 |
-
|
| 33 |
-
top_n = 15
|
| 34 |
-
if len(data) < top_n:
|
| 35 |
-
top_n = len(data)
|
| 36 |
-
top_n_indices = np.argsort(data)[-top_n:][::-1]
|
| 37 |
-
|
| 38 |
-
top_n_percentages = data[top_n_indices]
|
| 39 |
-
top_n_row_indices = row_indices[top_n_indices]
|
| 40 |
-
top_n_col_indices = col_indices[top_n_indices]
|
| 41 |
-
|
| 42 |
-
st.write("row")
|
| 43 |
-
st.write(filtered_df_index_map)
|
| 44 |
-
st.write(top_n_row_indices)
|
| 45 |
-
original_row_indices = [project_df_index_map[i].value for i in top_n_row_indices]
|
| 46 |
-
st.write(original_row_indices)
|
| 47 |
-
|
| 48 |
-
st.write("col")
|
| 49 |
-
st.write(top_n_col_indices)
|
| 50 |
-
original_col_indices = [filtered_df_index_map[i] for i in top_n_col_indices]
|
| 51 |
-
st.write(original_col_indices)
|
| 52 |
-
|
| 53 |
-
# Get the linear indices of the top 'top_x' values
|
| 54 |
-
# (flattened index to handle the sparse matrix more effectively)
|
| 55 |
-
#linear_indices = np.argsort(match_matrix.data)[-top_x:]
|
| 56 |
-
#if len(linear_indices) < top_x:
|
| 57 |
-
# top_x = len(linear_indices)
|
| 58 |
-
|
| 59 |
-
# Convert flat indices to 2D indices using the shape of the submatrix
|
| 60 |
-
#top_indices = np.unravel_index(linear_indices, match_matrix.shape)
|
| 61 |
-
|
| 62 |
-
# Get the corresponding similarity values
|
| 63 |
-
#top_values = match_matrix.data[linear_indices]
|
| 64 |
-
|
| 65 |
-
#flat_data = match_matrix.data
|
| 66 |
-
|
| 67 |
-
# Get the indices that would sort the data array in descending order
|
| 68 |
-
#sorted_indices = np.argsort(flat_data)[::-1]
|
| 69 |
-
|
| 70 |
-
# Take the first k indices to get the top k maximum values
|
| 71 |
-
#top_indices = sorted_indices[:top_x]
|
| 72 |
-
#top_row_indices = []
|
| 73 |
-
#top_col_indices = []
|
| 74 |
-
|
| 75 |
-
#for idx in top_indices:
|
| 76 |
-
# row, col = np.unravel_index(idx, match_matrix.shape)
|
| 77 |
-
# top_row_indices.append(row)
|
| 78 |
-
# top_col_indices.append(col)
|
| 79 |
-
|
| 80 |
-
#st.write(top_col_indices)
|
| 81 |
-
# Convert flat indices to 2D row and column indices
|
| 82 |
-
#row_indices, col_indices = match_matrix.nonzero()
|
| 83 |
-
#row_indices = row_indices[top_indices]
|
| 84 |
-
#col_indices = col_indices[top_indices]
|
| 85 |
-
|
| 86 |
-
# Get the values corresponding to the top k indices
|
| 87 |
-
#top_values = flat_data[top_indices]
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Get the values corresponding to the top k indices
|
| 91 |
-
#top_values = match_matrix[row_indices, col_indices]
|
| 92 |
-
|
| 93 |
-
#top_filtered_df_indices = [filtered_df_index_map[i] for i in top_col_indices]
|
| 94 |
-
#top_project_df_indices = [project_df_index_map[i] for i in top_row_indices]
|
| 95 |
-
|
| 96 |
-
#st.write(top_filtered_df_indices)
|
| 97 |
-
|
| 98 |
-
# Create resulting dataframes with top matches and their similarity scores
|
| 99 |
p1_df = filtered_df.loc[top_col_indices].copy()
|
| 100 |
p1_df['similarity'] = top_values
|
| 101 |
|
|
@@ -104,4 +28,4 @@ def calc_matches(filtered_df, project_df, similarity_matrix, top_x):
|
|
| 104 |
print("finished calc matches")
|
| 105 |
|
| 106 |
return p1_df, p2_df
|
| 107 |
-
|
|
|
|
| 13 |
if not isinstance(similarity_matrix, csr_matrix):
|
| 14 |
similarity_matrix = csr_matrix(similarity_matrix)
|
| 15 |
|
| 16 |
+
filtered_indices = filtered_df.index.to_list()
|
| 17 |
+
project_indices = project_df.index.to_list()
|
|
|
|
| 18 |
|
| 19 |
+
st.write(filtered_indices[:100])
|
| 20 |
+
st.write(project_indices[:100])
|
|
|
|
| 21 |
|
| 22 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
p1_df = filtered_df.loc[top_col_indices].copy()
|
| 24 |
p1_df['similarity'] = top_values
|
| 25 |
|
|
|
|
| 28 |
print("finished calc matches")
|
| 29 |
|
| 30 |
return p1_df, p2_df
|
| 31 |
+
"""
|