Commit
·
d1f3499
1
Parent(s):
44fce2f
Add utils
Browse files
utils.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def find_exact_match(matrix, query_vector, decimals=9):
|
| 5 |
+
"""
|
| 6 |
+
Finds the index of the vector in 'matrix' that is the closest match to 'query_vector'
|
| 7 |
+
when considering rounding to a specified number of decimal places.
|
| 8 |
+
|
| 9 |
+
Parameters:
|
| 10 |
+
- matrix: 2D numpy array where each row is a vector.
|
| 11 |
+
- query_vector: 1D numpy array representing the vector to be matched.
|
| 12 |
+
- decimals: Number of decimals to round to for the match.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
- Index of the exact match if found, or -1 if no match is found.
|
| 16 |
+
"""
|
| 17 |
+
# Round the matrix and query vector to the specified number of decimals
|
| 18 |
+
rounded_matrix = np.round(matrix, decimals=decimals)
|
| 19 |
+
rounded_query = np.round(query_vector, decimals=decimals)
|
| 20 |
+
|
| 21 |
+
# Find the index where all elements match
|
| 22 |
+
matches = np.all(rounded_matrix == rounded_query, axis=1)
|
| 23 |
+
|
| 24 |
+
# Return the index if a match is found, otherwise return -1
|
| 25 |
+
if np.any(matches):
|
| 26 |
+
return np.where(matches)[0][0] # Return the first match
|
| 27 |
+
else:
|
| 28 |
+
return -1
|