Distance Between Vectors
Glossary
Class: a new data type with its own methods and attributes.
Euclidian distance: the between vectors and is the sum of squared differences of coordinates:
Manhattan distance/city block distance: the sum of modules of coordinate differences of vectors and :
Scalar product: an operation resulting in a number (scalar) that is equal to the sum of element-by-element products of two vectors' elements
Practice
1# Scalar product of vectors23import numpy as np45dot_value1 = np.dot(vector1, vector2)6dot_value2 = vector1 @ vector2
1# Euclidean distance between vectors23import numpy as np4from scipy.spatial import distance56d = distance.euclidean(a, b)
1# Manhattan distance between vectors23import numpy as np4from scipy.spatial import distance56d = distance.cityblock(a, b)
1# Indices of minimum and maximum elements in array23index = np.array(distances).argmin() # minimum element index4index = np.array(distances).argmax() # maximum element index
1# Creating class23class ClassName:4 def fit(self, arg1, arg2, ...): # class method5 # method content