-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create euclidean_distance.py #3350
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
Changes from 26 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
91b6ee6
Create distance_formula.py
andrewctam eb044a3
Remove whitespace
andrewctam 861ce33
Update distance_formula.py
andrewctam 9102f3e
Update distance_formula.py
andrewctam 2cd354b
Update distance_formula.py
andrewctam 36f90ae
Generalize
andrewctam 9186ba9
Grammar mistake
andrewctam 4dd283a
Rename distance_formula.py to euclidean_distance.py
andrewctam 01a1ea9
Update euclidean_distance.py
andrewctam 0451a5b
v1 - > v2
andrewctam 3940049
Update euclidean_distance.py
andrewctam de64392
Update euclidean_distance.py
andrewctam 5a092eb
Update euclidean_distance.py
andrewctam b5152b2
Update euclidean_distance.py
andrewctam a1b9a86
Update euclidean_distance.py
andrewctam ac0ff37
Update euclidean_distance.py
andrewctam 6dad371
Update maths/euclidean_distance.py
andrewctam 36185ab
Update euclidean_distance.py
andrewctam d69c65f
Update euclidean_distance.py
andrewctam 68635e2
Update euclidean_distance.py
andrewctam aedcb59
Update euclidean_distance.py
andrewctam 0581677
Update euclidean_distance.py
andrewctam a278402
Update euclidean_distance.py
andrewctam e12d807
Update euclidean_distance.py
andrewctam 3ea94fa
Update euclidean_distance.py
andrewctam 21bba6a
Update euclidean_distance.py
andrewctam 39e0b46
Update euclidean_distance.py
andrewctam 0531471
Update euclidean_distance.py
andrewctam 764f07c
Update euclidean_distance.py
andrewctam 3d0cd8e
Update euclidean_distance.py
andrewctam 78d4644
Update euclidean_distance.py
andrewctam aad0d3a
Update euclidean_distance.py
andrewctam 4a08254
Update euclidean_distance.py
andrewctam 017eb73
Update euclidean_distance.py
andrewctam f16b2bb
Update euclidean_distance.py
andrewctam 8e83ae6
Update euclidean_distance.py
andrewctam ccdd50e
Update maths/euclidean_distance.py
andrewctam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Iterable, Union | ||
|
||
import numpy as np | ||
|
||
Vector = Union[Iterable[float], Iterable[int], np.ndarray] | ||
VectorOut = Union[np.float64, int, float] | ||
|
||
|
||
def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: | ||
""" | ||
Calculate the distance between the two endpoints of two vectors. | ||
A vector is defined as a list, tuple, or numpy 1D array. | ||
>>> euclidean_distance((0, 0), (2, 2)) | ||
2.8284271247461903 | ||
>>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) | ||
3.4641016151377544 | ||
>>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) | ||
8.0 | ||
>>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) | ||
8.0 | ||
""" | ||
return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) | ||
|
||
|
||
def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: | ||
""" | ||
Calculate the distance between the two endpoints of two vectors without numpy. | ||
A vector is defined as a list, tuple, or numpy 1D array. | ||
>>> euclidean_distance((0, 0), (2, 2)) | ||
2.8284271247461903 | ||
>>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) | ||
8.0 | ||
""" | ||
the_sum = 0 | ||
for i in range(len(vector_1)): | ||
the_sum += (vector_2[i] - vector_1[i]) ** 2 | ||
return the_sum ** (1 / 2) | ||
|
||
|
||
if __name__ == "__main__": | ||
point = np.array([2, 2]) | ||
point_2 = np.array([0, 0]) | ||
print(euclidean_distance(point, point_2)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.