<jupyter_start><jupyter_text>Distances metrics from scratch For real valued vector space (continous variables) -1. Euclidean (Pythagorean theoram)2. Manhattan (Also known as cityblock distance, or taxicab geometry)3. chebyshev3. Minkowski (generalized dsitance metrics)Metrics for integer valued vector space (categorical data) - 4. Hamming (metrics for comparing two binary strings)  Preference (Euclidean vs manhattan - high dimentional data) - Many high dimensional indexing structures and algorithms use the euclidean distance metric as a naturalextension of its traditional use in two- or three-dimensional spatial applications (refer paper [2] )- the Manhattan distance metric (L1 norm) is consistently more preferable than theEuclidean distance metric (L2 norm) for high dimensional data miningapplications - (refer paper [2] )  Getting pairwise distance metrics using sklearn's Distance metrics<jupyter_code>from sklearn.neighbors import DistanceMetric
import sklearn
dist = DistanceMetric.get_metric('euclidean') #euclidean
X = [[1,2],[7,8]]
dist.pairwise(X)

DistanceMetric.get_metric('manhattan').pairwise(X) #manhattan
DistanceMetric.get_metric('minkowski').pairwise(X) #minkowski
DistanceMetric.get_metric('hamming').pairwise(X) #hamming
DistanceMetric.get_metric('chebyshev').pairwise(X) #chebyshev<jupyter_output><empty_output><jupyter_text>Calculating distance metrics using scipy<jupyter_code>from scipy.spatial import distance
distance.euclidean(X[0],X[1])
distance.cityblock(X[0],X[1]) #manhattan - named as cityblock in scipy<jupyter_output><empty_output><jupyter_text>Writing my own pairwise Distance metrics from scratch<jupyter_code># Made use of Inner class 

#covers euclidean and manhattan distance metrics

class MyDistanceMetrics:
    
    def __init__(self):
        self.metric = ''
        self.cl = ''
    
    def get_metric(metric):
        if metric == 'euclidean':
            cl = Euclidean
        elif metric == 'manhattan':
            cl = Manhattan
        return cl
    
        
    

class Euclidean:
    def __init__(self,X):
        self.X = X
    def pairwise(X):
        pwise = []
        for i in range(len(X)):
            templist = []
            for j in range(len(X[i])):
                templist.append(math.sqrt((X[i][i]-X[j][i])**2 + (X[i][j]-X[j][j])**2))
            pwise.append(templist)
        return pwise
   

class Manhattan:
        def __init__(self,X):
            self.X=X
            
        def pairwise(X):
            pwise = []
            for i in range(len(X)):
                templist = []
                for j in range(len(X[i])):
                    templist.append(abs(X[i][i]-X[j][i]) + abs(X[i][j]-X[j][j]))
                pwise.append(templist)
            return pwise
        
        

    
# calculation of metrics
dsit = MyDistanceMetrics.get_metric('euclidean') #euclidean
dist.pairwise(X)


MyDistanceMetrics.get_metric('manhattan').pairwise(X) #manhattan<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10496311200536552, 'fraction_numerical': 0.006371562709590879, 'mean_word_length': 3.3357558139534884, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '367125', 'license': 'MIT', 'n_tokens_mistral': 952, 'n_tokens_neox': 879, 'n_words': 237, 'path': 'Algorithms_from_scratch/distance_metrics_from_scratch.ipynb', 'repo_name': 'nagarajbhat/100DaysOfCode', 'size': '2982'}