<jupyter_start><jupyter_text>Distance Metrics<jupyter_code>import numpy as np
point_1 = (2, 3, 5)
point_2 = (1, -1, 3)
def manhatten_distance(p1, p2):
    distance = 0
    for i in range(len(p1)):
        distance += np.abs(p1[i] - p2[i])
    return distance
manhatten_distance(point_1, point_2)
def distance(p1, p2, c=2, verbose=True):
    names = {
        1: 'Manhatten',
        2: 'Euclidean',
        3: 'Minkowski'
    }
    d = sum([(np.abs(x1 - x2)) ** c for x1, x2 in zip(p1, p2)]) ** (1/c)
    if verbose:
        print('{} distance for norm {}: {}'.format(names[c if c < 3 else 3], c, d))
    return d
distance(point_1, point_2, 1)
distance(point_1, point_2, 2)
distance(point_1, point_2, 3)
point_3 = (-2, -5.8, 14, 15, 7, 9)
point_4 = (3, -9.2, -33, -21, 7, 9)
distance(point_3, point_4)
point_5 = [0, 0, 0, 7, 16, 2, 0, 1, 12]
point_6 = [21, -1, 35, 17, 14, 73, -22, 33, 3]
distance_between(point_5, point_6, 1)
point_7 = (-32, 47, 223.4)
point_8 = (883, 34, 199.5)
distance_between(point_7, point_8, 3.5)
def distance_2(c=2):
    return (lambda p1, p2: sum([(np.abs(x1 - x2)) ** c for x1, x2 in zip(p1, p2)]) ** (1/c))

fn = distance_2(3.5)
fn(point_7, point_8)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18889809444904723, 'fraction_numerical': 0.11019055509527755, 'mean_word_length': 3.4411764705882355, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '390650', 'license': 'MIT', 'n_tokens_mistral': 614, 'n_tokens_neox': 550, 'n_words': 147, 'path': '001_distance_metrics.ipynb', 'repo_name': 'tigerarcades/nf-datascience-distance_metrics-exercises', 'size': '1207'}