Skip to content

[mypy] Add/fix type annotations for similarity search in machine learning #4088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 22, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion machine_learning/similarity_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
2. distance between the vector and the nearest vector (float)
"""
import math
from typing import List

import numpy as np

Expand All @@ -30,7 +31,9 @@ def euclidean(input_a: np.ndarray, input_b: np.ndarray) -> float:
return math.sqrt(sum(pow(a - b, 2) for a, b in zip(input_a, input_b)))


def similarity_search(dataset: np.ndarray, value_array: np.ndarray) -> list:
def similarity_search(
dataset: np.ndarray, value_array: np.ndarray
) -> List[List[float]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type hint does not seem to be correct as per the given examples:

[[[0], 0.0]]
[[[0, 0, 0], 1.0]]
[[[0, 0, 0], 0.0], [[0, 0, 0], 1.0]]

Maybe:

List[List[Union[List[float], float]]]

I would suggest changing the return data type but that is out of scope for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

"""
:param dataset: Set containing the vectors. Should be ndarray.
:param value_array: vector/vectors we want to know the nearest vector from dataset.
Expand Down