# cosine_similarity_top_k

> **Function** in `langchain_weaviate`

📖 [View in docs](https://reference.langchain.com/python/langchain-weaviate/_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-weaviate/blob/dfe611967046848effcd0955937e4b474c2b94c0/libs/weaviate/langchain_weaviate/_math.py#L104)