Last active
April 9, 2022 13:07
-
-
Save PulkitS01/fadcf66d95184e98946483172cb03080 to your computer and use it in GitHub Desktop.
Distance Functions in Machine Learning
This file contains 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
# defining two strings | |
string_1 = 'euclidean' | |
string_2 = 'manhattan' |
This file contains 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
# strings of different shapes | |
new_string_1 = 'data' | |
new_string_2 = 'science' | |
len(new_string_1), len(new_string_2) |
This file contains 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
# computing the euclidean distance | |
euclidean_distance = distance.euclidean(point_1, point_2) | |
print('Euclidean Distance b/w', point_1, 'and', point_2, 'is: ', euclidean_distance) |
This file contains 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
# computing the hamming distance | |
hamming_distance = distance.hamming(list(new_string_1), list(new_string_2)) |
This file contains 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
# computing the hamming distance | |
hamming_distance = distance.hamming(list(string_1), list(string_2))*len(string_1) | |
print('Hamming Distance b/w', string_1, 'and', string_2, 'is: ', hamming_distance) |
This file contains 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
# importing the library | |
from scipy.spatial import distance | |
# defining the points | |
point_1 = (1, 2, 3) | |
point_2 = (4, 5, 6) | |
point_1, point_2 |
This file contains 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
# computing the manhattan distance | |
manhattan_distance = distance.cityblock(point_1, point_2) | |
print('Manhattan Distance b/w', point_1, 'and', point_2, 'is: ', manhattan_distance) |
This file contains 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
# computing the minkowski distance | |
minkowski_distance = distance.minkowski(point_1, point_2, p=3) | |
print('Minkowski Distance b/w', point_1, 'and', point_2, 'is: ', minkowski_distance) |
This file contains 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
# minkowski and euclidean distance | |
minkowski_distance_order_2 = distance.minkowski(point_1, point_2, p=2) | |
print('Minkowski Distance of order 2:',minkowski_distance_order_2, '\nEuclidean Distance: ',euclidean_distance) |
This file contains 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
# minkowski and manhattan distance | |
minkowski_distance_order_1 = distance.minkowski(point_1, point_2, p=1) | |
print('Minkowski Distance of order 1:',minkowski_distance_order_1, '\nManhattan Distance: ',manhattan_distance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanksss!