# cosine_similarity_top_k

> **Function** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/utils/math/cosine_similarity_top_k)

Row-wise cosine similarity with optional top-k and score threshold filtering.

## Signature

```python
cosine_similarity_top_k(
    X: Matrix,
    Y: Matrix,
    top_k: Optional[int] = 5,
    score_threshold: Optional[float] = None,
) -> Tuple[List[Tuple[int, int]], List[float]]
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `X` | `Matrix` | Yes | Matrix. |
| `Y` | `Matrix` | Yes | Matrix, same width as X. |
| `top_k` | `Optional[int]` | No | Max number of results to return. (default: `5`) |
| `score_threshold` | `Optional[float]` | No | Minimum cosine similarity of results. (default: `None`) |

## Returns

`Tuple[List[Tuple[int, int]], List[float]]`

Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
second contains corresponding cosine similarities.

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/a6a6079511ac8a5c1293337f88096b8641562e77/libs/community/langchain_community/utils/math.py#L46)