Skip to content

Commit 60ef86b

Browse files
committed
cosine sim python code
1 parent 591cca1 commit 60ef86b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

articles/ai-services/computer-vision/how-to/image-retrieval.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: nitinme
88

99
ms.service: azure-ai-vision
1010
ms.topic: how-to
11-
ms.date: 02/21/2023
11+
ms.date: 01/30/2024
1212
ms.author: pafarley
1313
ms.custom: references_regions
1414
---
@@ -90,7 +90,9 @@ The API call returns a **vector** JSON object, which defines the text string's c
9090

9191
Cosine similarity is a method for measuring the similarity of two vectors. In an image retrieval scenario, you'll compare the search query vector with each image's vector. Images that are above a certain threshold of similarity can then be returned as search results.
9292

93-
The following example C# code calculates the cosine similarity between two vectors. It's up to you to decide what similarity threshold to use for returning images as search results.
93+
The following example code calculates the cosine similarity between two vectors. It's up to you to decide what similarity threshold to use for returning images as search results.
94+
95+
#### [C#](#tab/csharp)
9496

9597
```csharp
9698
public static float GetCosineSimilarity(float[] vector1, float[] vector2)
@@ -108,6 +110,17 @@ public static float GetCosineSimilarity(float[] vector1, float[] vector2)
108110
}
109111
```
110112

113+
#### [Python](#tab/python)
114+
115+
```python
116+
import numpy as np
117+
118+
def cosine_similarity(vector1, vector2):
119+
return np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2))
120+
```
121+
122+
---
123+
111124
## Next steps
112125

113126
[Image retrieval concepts](../concept-image-retrieval.md)

0 commit comments

Comments
 (0)