Update Clustering.ipynb

This commit is contained in:
Morten Hjorth-Jensen
2021-11-20 23:22:57 +01:00
parent d58d7b3dd8
commit 01610bc12d
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "04229efd",
"id": "d0a34092",
"metadata": {},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
@@ -11,7 +11,7 @@
},
{
"cell_type": "markdown",
"id": "e2e714e4",
"id": "a4d8f404",
"metadata": {},
"source": [
"# Clustering Analysis\n",
@@ -37,7 +37,7 @@
},
{
"cell_type": "markdown",
"id": "1466c1cb",
"id": "ae95f76d",
"metadata": {},
"source": [
"## Basic Idea of the K-means Clustering Algorithm\n",
@@ -68,7 +68,7 @@
},
{
"cell_type": "markdown",
"id": "30580c15",
"id": "a919a12d",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -83,7 +83,7 @@
},
{
"cell_type": "markdown",
"id": "c430172a",
"id": "1afc1372",
"metadata": {},
"source": [
"which we wish to group into $K < n$ clusters. For our dissimilarity measure we\n",
@@ -92,7 +92,7 @@
},
{
"cell_type": "markdown",
"id": "885eba7f",
"id": "c4332ba1",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -108,7 +108,7 @@
},
{
"cell_type": "markdown",
"id": "485a4b5c",
"id": "7c6eca50",
"metadata": {},
"source": [
"Next we define the so called *within-cluster point scatter* which gives us a\n",
@@ -118,7 +118,7 @@
},
{
"cell_type": "markdown",
"id": "1d3c6ca7",
"id": "d762d24f",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -135,7 +135,7 @@
},
{
"cell_type": "markdown",
"id": "33a14233",
"id": "058e6d60",
"metadata": {},
"source": [
"where $\\boldsymbol{\\overline{x_k}}$ is the mean vector associated with the $k$-th\n",
@@ -150,7 +150,7 @@
},
{
"cell_type": "markdown",
"id": "7044f7fc",
"id": "191959b0",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -169,7 +169,7 @@
},
{
"cell_type": "markdown",
"id": "7d612869",
"id": "c170bd21",
"metadata": {},
"source": [
"Which is a quantity that is conserved throughout the $k$-means algorithm. It can\n",
@@ -183,7 +183,7 @@
},
{
"cell_type": "markdown",
"id": "3dc3f01f",
"id": "d495c32e",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -198,7 +198,7 @@
},
{
"cell_type": "markdown",
"id": "f51d30c2",
"id": "5835ab97",
"metadata": {},
"source": [
"Now we have all the pieces necessary to formally revisit the k-means algorithm.\n",
@@ -209,7 +209,7 @@
},
{
"cell_type": "markdown",
"id": "9265c30e",
"id": "5804711e",
"metadata": {},
"source": [
"## The K-means Clustering Algorithm\n",
@@ -244,7 +244,7 @@
},
{
"cell_type": "markdown",
"id": "63dc40f2",
"id": "bda4544a",
"metadata": {},
"source": [
"## Writing Our Own Code\n",
@@ -255,7 +255,7 @@
},
{
"cell_type": "markdown",
"id": "9e110f91",
"id": "0229351b",
"metadata": {},
"source": [
"### Basic Python\n",
@@ -277,7 +277,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "c5196fbb",
"id": "4d4cb587",
"metadata": {},
"outputs": [],
"source": [
@@ -294,7 +294,7 @@
},
{
"cell_type": "markdown",
"id": "46f6bf75",
"id": "adbf5dfb",
"metadata": {},
"source": [
"Next we define functions, for ease of use later, to generate Gaussians and to\n",
@@ -304,7 +304,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "c5db2b9c",
"id": "432afa10",
"metadata": {},
"outputs": [
{
@@ -377,7 +377,7 @@
},
{
"cell_type": "markdown",
"id": "123f8b0a",
"id": "5da6553d",
"metadata": {},
"source": [
"Now that we are our, albeit very simple, dataset we are ready to start\n",
@@ -387,7 +387,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "44d87c0c",
"id": "50fdce3a",
"metadata": {},
"outputs": [],
"source": [
@@ -428,7 +428,7 @@
},
{
"cell_type": "markdown",
"id": "b0bf3a01",
"id": "4a192c1a",
"metadata": {},
"source": [
"Let's plot and see"
@@ -437,7 +437,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "4af50c01",
"id": "e3bd3ed5",
"metadata": {},
"outputs": [
{
@@ -474,7 +474,7 @@
},
{
"cell_type": "markdown",
"id": "9a370b9d",
"id": "ca5cdbba",
"metadata": {},
"source": [
"So what do we have so far? We have 'picked' $k$ centroids at random from our\n",
@@ -492,7 +492,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "7e049bf1",
"id": "683d9e56",
"metadata": {},
"outputs": [
{
@@ -500,7 +500,7 @@
"output_type": "stream",
"text": [
"Converged at iteration 5\n",
"Runtime: 0.2396700382232666 seconds\n"
"Runtime: 0.23237395286560059 seconds\n"
]
}
],
@@ -557,7 +557,7 @@
},
{
"cell_type": "markdown",
"id": "2602cc83",
"id": "77d7b6c9",
"metadata": {},
"source": [
"And thats it! We now have an extremely barebones, un-optimized k-means\n",
@@ -567,7 +567,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "743467eb",
"id": "de21744c",
"metadata": {},
"outputs": [
{
@@ -604,7 +604,7 @@
},
{
"cell_type": "markdown",
"id": "1f2749e0",
"id": "1d3be7e4",
"metadata": {},
"source": [
"Now there are a few glaring improvements to be done here. First of all is\n",
@@ -617,7 +617,7 @@
},
{
"cell_type": "markdown",
"id": "ac9fa4a5",
"id": "4d4b64fd",
"metadata": {},
"source": [
"## Towards a More Numpythonic Code"
@@ -626,7 +626,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "9edf7db3",
"id": "4249aa95",
"metadata": {},
"outputs": [
{
@@ -634,7 +634,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 5\n",
"Runtime: 0.20168113708496094 seconds\n"
"Runtime: 0.19273090362548828 seconds\n"
]
}
],
@@ -748,7 +748,7 @@
},
{
"cell_type": "markdown",
"id": "212c648e",
"id": "fdbad890",
"metadata": {},
"source": [
"**Note**: the start of the timing is after the random initialization, and first\n",
@@ -769,7 +769,7 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "07bfff3f",
"id": "0ffe5738",
"metadata": {},
"outputs": [
{
@@ -777,7 +777,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 11\n",
"Runtime: 0.389873743057251 seconds\n",
"Runtime: 0.386091947555542 seconds\n",
" "
]
}
@@ -789,7 +789,7 @@
},
{
"cell_type": "markdown",
"id": "b7f27a4a",
"id": "649e0d75",
"metadata": {},
"source": [
"Here we can see the reason for profiling. We now know for certain a lot can be\n",
@@ -801,7 +801,7 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "b9ffa227",
"id": "f5b94362",
"metadata": {},
"outputs": [],
"source": [
@@ -892,7 +892,7 @@
},
{
"cell_type": "markdown",
"id": "da6c65d3",
"id": "65816e1e",
"metadata": {},
"source": [
"When working towards becoming a data scientist using Python this last step is\n",
@@ -905,7 +905,7 @@
{
"cell_type": "code",
"execution_count": 10,
"id": "ea31860d",
"id": "dab77d43",
"metadata": {},
"outputs": [
{
@@ -913,7 +913,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 5\n",
"Runtime: 0.002451181411743164 seconds\n"
"Runtime: 0.002377033233642578 seconds\n"
]
}
],
@@ -924,7 +924,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "2a34f7af",
"id": "6ac85377",
"metadata": {},
"outputs": [],
"source": []