This commit is contained in:
Morten Hjorth-Jensen
2021-11-01 16:50:26 +01:00
parent 8fe7a8ec93
commit ec963b76e0
10 changed files with 5155 additions and 2839 deletions
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "2150517f",
"id": "04229efd",
"metadata": {},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
@@ -11,7 +11,7 @@
},
{
"cell_type": "markdown",
"id": "f6e0cd96",
"id": "e2e714e4",
"metadata": {},
"source": [
"# Clustering Analysis\n",
@@ -37,7 +37,7 @@
},
{
"cell_type": "markdown",
"id": "e755ea79",
"id": "1466c1cb",
"metadata": {},
"source": [
"## Basic Idea of the K-means Clustering Algorithm\n",
@@ -68,7 +68,7 @@
},
{
"cell_type": "markdown",
"id": "682f37d4",
"id": "30580c15",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -83,7 +83,7 @@
},
{
"cell_type": "markdown",
"id": "3bd80ebf",
"id": "c430172a",
"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": "c34b3e47",
"id": "885eba7f",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -108,7 +108,7 @@
},
{
"cell_type": "markdown",
"id": "54897ef5",
"id": "485a4b5c",
"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": "02ec7cdd",
"id": "1d3c6ca7",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -135,7 +135,7 @@
},
{
"cell_type": "markdown",
"id": "d15918ea",
"id": "33a14233",
"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": "8576296d",
"id": "7044f7fc",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -169,7 +169,7 @@
},
{
"cell_type": "markdown",
"id": "2736464c",
"id": "7d612869",
"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": "bbe04304",
"id": "3dc3f01f",
"metadata": {},
"source": [
"<!-- Equation labels as ordinary links -->\n",
@@ -198,7 +198,7 @@
},
{
"cell_type": "markdown",
"id": "1e8910ec",
"id": "f51d30c2",
"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": "bb548d32",
"id": "9265c30e",
"metadata": {},
"source": [
"## The K-means Clustering Algorithm\n",
@@ -244,7 +244,7 @@
},
{
"cell_type": "markdown",
"id": "bc569914",
"id": "63dc40f2",
"metadata": {},
"source": [
"## Writing Our Own Code\n",
@@ -255,7 +255,7 @@
},
{
"cell_type": "markdown",
"id": "8601ba47",
"id": "9e110f91",
"metadata": {},
"source": [
"### Basic Python\n",
@@ -277,7 +277,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "3023fb96",
"id": "c5196fbb",
"metadata": {},
"outputs": [],
"source": [
@@ -294,7 +294,7 @@
},
{
"cell_type": "markdown",
"id": "18feed32",
"id": "46f6bf75",
"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": "171b8aa5",
"id": "c5db2b9c",
"metadata": {},
"outputs": [
{
@@ -377,7 +377,7 @@
},
{
"cell_type": "markdown",
"id": "22a5cc09",
"id": "123f8b0a",
"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": "7ef1f112",
"id": "44d87c0c",
"metadata": {},
"outputs": [],
"source": [
@@ -428,7 +428,7 @@
},
{
"cell_type": "markdown",
"id": "6fe8327e",
"id": "b0bf3a01",
"metadata": {},
"source": [
"Let's plot and see"
@@ -437,7 +437,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "79427d57",
"id": "4af50c01",
"metadata": {},
"outputs": [
{
@@ -474,7 +474,7 @@
},
{
"cell_type": "markdown",
"id": "5d14cf57",
"id": "9a370b9d",
"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": "4c9c9a53",
"id": "7e049bf1",
"metadata": {},
"outputs": [
{
@@ -500,7 +500,7 @@
"output_type": "stream",
"text": [
"Converged at iteration 5\n",
"Runtime: 0.24420595169067383 seconds\n"
"Runtime: 0.2396700382232666 seconds\n"
]
}
],
@@ -557,7 +557,7 @@
},
{
"cell_type": "markdown",
"id": "61f30110",
"id": "2602cc83",
"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": "d964670a",
"id": "743467eb",
"metadata": {},
"outputs": [
{
@@ -604,7 +604,7 @@
},
{
"cell_type": "markdown",
"id": "351ef866",
"id": "1f2749e0",
"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": "1d80d4b2",
"id": "ac9fa4a5",
"metadata": {},
"source": [
"## Towards a More Numpythonic Code"
@@ -626,7 +626,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "a88b26c9",
"id": "9edf7db3",
"metadata": {},
"outputs": [
{
@@ -634,7 +634,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 5\n",
"Runtime: 0.20550775527954102 seconds\n"
"Runtime: 0.20168113708496094 seconds\n"
]
}
],
@@ -748,7 +748,7 @@
},
{
"cell_type": "markdown",
"id": "7372c09e",
"id": "212c648e",
"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": "566f0e04",
"id": "07bfff3f",
"metadata": {},
"outputs": [
{
@@ -777,7 +777,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 11\n",
"Runtime: 0.38791799545288086 seconds\n",
"Runtime: 0.389873743057251 seconds\n",
" "
]
}
@@ -789,7 +789,7 @@
},
{
"cell_type": "markdown",
"id": "44ae7ea9",
"id": "b7f27a4a",
"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": "93b58a7f",
"id": "b9ffa227",
"metadata": {},
"outputs": [],
"source": [
@@ -892,7 +892,7 @@
},
{
"cell_type": "markdown",
"id": "b2ce6830",
"id": "da6c65d3",
"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": "cd0e1669",
"id": "ea31860d",
"metadata": {},
"outputs": [
{
@@ -913,7 +913,7 @@
"output_type": "stream",
"text": [
"Converged at iteration: 5\n",
"Runtime: 0.0023779869079589844 seconds\n"
"Runtime: 0.002451181411743164 seconds\n"
]
}
],
@@ -924,7 +924,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d86b2a09",
"id": "2a34f7af",
"metadata": {},
"outputs": [],
"source": []