Skip to content

Commit

Permalink
modif_lab11
Browse files Browse the repository at this point in the history
  • Loading branch information
CamilleDelgrange committed Jan 8, 2022
1 parent 8722abf commit 83a7251
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
Binary file added labs/ex11/exercise11_annot.pdf
Binary file not shown.
77 changes: 69 additions & 8 deletions labs/ex11/solution/ex11.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"# Useful starting lines\n",
"%matplotlib inline\n",
Expand All @@ -23,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 23,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -53,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -69,10 +78,61 @@
" \"\"\"\n",
" ### SOLUTION\n",
" N, d = data.shape\n",
" return data[np.random.choice(N, k)]\n",
" #print(data)\n",
" #print(np.random.choice(N, k)) # picks 4 sample numbers: e.g. 213, 45, 138 and 73 along with their dimension for each cluster\n",
" return data[np.random.choice(N, k)] #generates a random sample from np.arange(272) of size 4 --> # of clusters we want\n",
" ### END SOLUTION"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 48 53 66 146]\n"
]
},
{
"data": {
"text/plain": [
"array([[ 4.567, 78. ],\n",
" [ 4. , 70. ],\n",
" [ 4.25 , 83. ],\n",
" [ 1.833, 57. ]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"initialize_clusters(data, 4)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"82.0\n"
]
}
],
"source": [
"print(data[48, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -92,7 +152,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -126,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -148,6 +208,7 @@
" distance_matrix = build_distance_matrix(data, mu_old)\n",
" losses = np.min(distance_matrix, axis=1)\n",
" assignments = np.argmin(distance_matrix, axis=1)\n",
" print(assignments.shape)\n",
"\n",
" # update the mu\n",
" mu = np.zeros((k, d))\n",
Expand Down Expand Up @@ -523,7 +584,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion labs/ex11/template/ex11.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 83a7251

Please sign in to comment.