{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 1: Introduction to Clustering\n", "\n", "## Exercise 1.02" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import math\n", "import numpy as np\n", "\n", "def dist(a, b):\n", " return math.sqrt(math.pow(a[0]-b[0],2) + math.pow(a[1]-b[1],2))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "centroids = [ (2, 5), (8, 3), (4,5) ]\n", "x = (0, 8)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Euclidean Distance between x (0, 8) and centroid (2, 5) is 3.605551275463989\n", "Euclidean Distance between x (0, 8) and centroid (8, 3) is 9.433981132056603\n", "Euclidean Distance between x (0, 8) and centroid (4, 5) is 5.0\n" ] } ], "source": [ "# Calculating Euclidean Distance between x and centroid\n", "centroid_distances =[]\n", "for centroid in centroids:\n", " print(\"Euclidean Distance between x {} and centroid {} is {}\".format(x ,centroid, dist(x,centroid)))\n", " centroid_distances.append(dist(x,centroid))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }