<jupyter_start><jupyter_text>Chapter 1: Introduction to Clustering Exercise 1.02<jupyter_code>import math
import numpy as np

def dist(a, b):
    return math.sqrt(math.pow(a[0]-b[0],2) + math.pow(a[1]-b[1],2))
centroids = [ (2, 5), (8, 3), (4,5) ]
x = (0, 8)
# Calculating Euclidean Distance between x and centroid
centroid_distances =[]
for centroid in centroids:
    print("Euclidean Distance between x {} and centroid {} is {}".format(x ,centroid, dist(x,centroid)))
    centroid_distances.append(dist(x,centroid))
### Calculating Euclidean Distance between x and centroid
import unittest

class TestEuclideanDistance(unittest.TestCase):
    """unittest for dist function"""
    centroids = [ (2, 5), (8, 3), (4,5) ]
    x = (0, 8)
    def test_e_distance_between_x_centroid(self):
        self.centroids=centroids
        self.assertAlmostEqual(dist(x,centroids[0]),3.605551275463989)
        self.assertAlmostEqual(dist(x,centroids[1]),9.433981132056603)
        self.assertAlmostEqual(dist(x,centroids[2]),5.0)
suite = unittest.TestLoader().loadTestsFromTestCase(TestEuclideanDistance)
unittest.TextTestRunner(verbosity=2).run(suite)<jupyter_output>test_e_distance_between_x_centroid (__main__.TestEuclideanDistance) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK
{'fraction_non_alphanumeric': 0.18099547511312217, 'fraction_numerical': 0.05203619909502263, 'mean_word_length': 3.9700374531835205, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '437859', 'license': 'MIT', 'n_tokens_mistral': 509, 'n_tokens_neox': 448, 'n_words': 91, 'path': 'Chapter01/tests/Exercise1.02.ipynb', 'repo_name': 'PacktWorkshops/Applied-Unsupervised-Learning-with-Python', 'size': '1326'}