284 lines
216 KiB
Plaintext
284 lines
216 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Zadanie 1\n",
|
||
|
"Zapoznaj się z teorią algorytmu klastrowania K-means (klasyczny alogorytm w dziedzinie eksploracji danych (ang. <a href=\"https://en.wikipedia.org/wiki/Data_mining\">data mining))</a>) np. przez stronę <a href=\"https://www.statystyka.eu/analiza-skupien/metoda-k-srednich.php\">metoda-k-średnich</a>. Przestestuj jedną wybraną metrykę, inną niż euklidesowa - wykonaj obrazy pokazujące efekty jej działania."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Do zwizualizowania działania klastrowania K-means potrzebne jest zdefiniowanie punktów reprezentujacych dane, punkty są tak losowane by widoczne było ich pogrupowane rozmieszczenie"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 94,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAApQAAAH5CAYAAAA/Ta4lAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAwNUlEQVR4nO3dfYxcZ3k34HtYNyYC70KiuGy8QxbKRyAhCAhCabpgF4oUIbR03qUoDm0K9J/KEC8VEkorvSH9wEGolYNUpSStAhLYbuV3QgEpTYPkNa5oVBOKlLRSIOA0m82iIAq7SQQGjef942gSr3fWuzNn5nzNdUmryZ4963mycezfeZ77uZ9au91uBwAA9OkFeQ8AAIByEygBAEhFoAQAIBWBEgCAVARKAABSESgBAEhFoAQAIJVtWb/hmTNn4sknn4wdO3ZErVbL+u0BANhEu92Op59+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vide85jXx6KOPpvllAKC0ms2Iubm1YTIiYmkpud5s5jMuyFKqQPnMM8/ED37wg5icnBzUeACgNFqtZGay21pf59r8fHIfVFlPgfITn/hEHD9+PB577LH41re+Fb/7u78bY2Njcf311w9rfABQWCdOrJ+ZPFu7HbG4mNwHVdZT26Annngirr/++vjJT34Sl1xySfzWb/1WPPDAA3HJJZcMa3wAUFjLy4O9D8qqp0B55MiRYY0DAEpnqxVfKsOoOmd5A0CfZmaS3dy1Wvev12oR9XpyH1SZQAkAfRobS1oDRawPlZ3
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x600 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"\n",
|
||
|
"# Generowanie losowych punktów danych\n",
|
||
|
"np.random.seed(23)\n",
|
||
|
"data = np.vstack([\n",
|
||
|
" np.random.normal(loc=(2, 2), scale=0.6, size=(25, 2)),\n",
|
||
|
" np.random.normal(loc=(5, 3), scale=0.8, size=(25, 2)),\n",
|
||
|
" np.random.normal(loc=(5, 5), scale=0.7, size=(25, 2))\n",
|
||
|
"])\n",
|
||
|
"plt.figure(figsize=(8, 6))\n",
|
||
|
"plt.scatter(data[:, 0], data[:, 1], c='b', label=f'dane')\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Następnie trzeba zdefiniować centroidy od jakich zaczyna się algorytm klastrowania, wybieram 3 losowe punkty spośród danych:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 95,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhgAAAGdCAYAAABQEQrmA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pMhQPmYQYDQE2IUo8HIAwIGABqBlPhgDkIGAAACiHhOgIGAERcX5/VDXV+w7Jk0iqepX4FlaLIEwAizD7HZWE3VPscFw6LQ6UIGAAQURxpDi8RMAAgojjHBV4iYABARHGOC7xEwACAiOIcF3iJgAEAEcWR5vASAQMAIoojzeElAgYARBjnuMArNNoCgIjjHBd4gYABAOAcF7jO0RLJN77xDcVisbyv9evXezU2AAAQUo5nMDZu3KgXXnjhgx8QZxIEAADkc5wO4vG4rr32Wi/GAgAAaoTjgHHu3DmtWrVKDQ0N2rJli3p6erR69eqi75+entb09PTcnycnJysbKQCgKhzJDj85qsG46aabdOzYMT333HM6evSoRkZG1NnZqUuXLhX9np6eHjU3N899pVKpqgcNAHCmr09qb5d27JD27bMe29s5LRXeieVyhc7RK8+7776rNWvW6O///u913333FXxPoRmMVCqliYkJNTU1VfqrAQBlso9kX/ivvd1Mi34XKMfk5KSam5vLvn5X1WjrIx/5iNatW6fz588XfU8ikVBTU1PeFwDAHxzJjqBUFTAuX76sX/7yl2rlJBwAMBJHsiMojgLGV7/6VQ0MDOjChQt68cUX9Qd/8Aeqq6vT3r17vRofAKAKHMmOoDjaRfLGG29o7969euedd9TS0qLf/d3f1enTp9XS0uLV+AAAVeBIdgSlqiLPSjgtEgE
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"k = 4\n",
|
||
|
"startCentroids = data[np.random.choice(data.shape[0], k, replace=False)]\n",
|
||
|
"\n",
|
||
|
"plt.scatter(data[:, 0], data[:, 1], c='b', label=f'dane')\n",
|
||
|
"plt.scatter(startCentroids[:, 0], startCentroids[:, 1], c='black', marker='X', s=100, label='Centroids')\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Mając wybrane centroidy należy zdefiniować funkcję odległości, zastosuję odległość Manhattan lub euklidesową"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 96,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Funkcja obliczająca odległość euklidesową\n",
|
||
|
"def euclidean_distance(point1, point2):\n",
|
||
|
" return np.sqrt(np.sum((point1 - point2) ** 2))\n",
|
||
|
"\n",
|
||
|
"# Funkcja obliczająca odległość Manhattan\n",
|
||
|
"def manhattan_distance(point1, point2):\n",
|
||
|
" return np.sum(np.abs((point1 - point2)))\n",
|
||
|
"\n",
|
||
|
"# Funkcja obliczająca odległość euklidesową do kwadratu\n",
|
||
|
"def euclidean2_distance(point1, point2):\n",
|
||
|
" return np.sum((point1 - point2) ** 2)\n",
|
||
|
"\n",
|
||
|
"# Funkcja obliczająca odległość Minkowskiego dla m = 2\n",
|
||
|
"def minkowski_distance(point1, point2):\n",
|
||
|
" m = 2\n",
|
||
|
" return np.sum(np.abs(point1 - point2) ** m) ** (1/m)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Kolejnym etapem jest zdefiniowanie funkcji klastryzującej"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 97,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Funkcja algorytmu K-means\n",
|
||
|
"def k_means(data, k, distance, centroids, max_iters=100):\n",
|
||
|
" \n",
|
||
|
" for _ in range(max_iters):\n",
|
||
|
" clusters = [[] for _ in range(k)]\n",
|
||
|
" \n",
|
||
|
" for point in data:\n",
|
||
|
" distances = [distance(point, centroid) for centroid in centroids]\n",
|
||
|
" cluster_index = np.argmin(distances)\n",
|
||
|
" clusters[cluster_index].append(point)\n",
|
||
|
" \n",
|
||
|
" new_centroids = np.array([np.mean(cluster, axis=0) if cluster else centroids[i]\n",
|
||
|
" for i, cluster in enumerate(clusters)])\n",
|
||
|
" \n",
|
||
|
" if np.all(centroids == new_centroids):\n",
|
||
|
" break\n",
|
||
|
" \n",
|
||
|
" centroids = new_centroids\n",
|
||
|
"\n",
|
||
|
" return clusters, centroids\n",
|
||
|
"\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 98,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqcAAAIjCAYAAAA+xLLKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACB8UlEQVR4nO3de1xT9f8H8NcB3EDkmlOJTQEBRVIzzVIDIfWrXZQC1MxKy+6kYFpf61uppWmlJZVZ+e2rdtE0JLuYt0wuWqRmmnlJMBAkTEoBUQEZ5/fHfluMbbDBLmfj9Xw89qCdc7bz2Yb02ud8Pu+PIIqiCCIiIiIiCXBzdAOIiIiIiLQYTomIiIhIMhhOiYiIiEgyGE6JiIiISDIYTomIiIhIMhhOiYiIiEgyGE6JiIiISDIYTomIiIhIMhhOiYiIiEgyGE6JqEVxcXGIi4tzdDNaJSsrC4IgICsry9FNadbq1ashCAKKiook1w5Hff7O/HtnTSEhIZg6daruviW/03wPyRkxnJJL0P4Pdf/+/XrbKysrMXjwYHh6emLr1q0Oap10/fnnn5g9ezZ69+6Njh07wtvbGwMHDsSCBQtQUVFht3a8/PLL2LRpk93OZw/jxo1Dx44dceHCBZPHTJ48GTKZDH///bcdWyYtR48exbx58xweys0REhICQRCM3saMGePo5hG5DA9HN4DIVqqqqvCvf/0Lv/zyCz7//HP+z6OJffv24dZbb0V1dTXuueceDBw4EACwf/9+LF68GDk5Odi+fb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caMGWMwRKkxU5+79rV6eXlh8ODByM3NNfp4R/29IjIXe07JpVy8eBG33HIL9u3bh4yMDNx+++1mP3bixImIiorC4sWLsXnzZixYsACBgYF47733cPPNN+OVV17BJ598gtmzZ+P6669HbGwsAM3/XMaNG4fdu3fj4YcfRlRUFA4fPow33ngDJ06c0BtLuWLFCkRHR2PcuHHw8PDAV199hccffxwNDQ1ISUnRa09BQQGSk5Mxbdo0TJkyBf/73/8wdepUDBw4ENHR0QA0wXrRokV48MEHMXjwYFRVVWH//v04cOAARo0aZfK1fvnll/Dy8kJycrIF767l6urqMHr0aNTW1mL69Ono1q0bSktL8fXXX6OiogJ+fn746KOPdO1/+OGHAQA9e/YEoBkTe+ONN0IQBDzxxBNQKBTYsmULpk2bhqqqKqSlpemd76WXXoJMJsPs2bNRW1vbbM/b+fPnMWbMGCQmJmLChAnIyMjAv//9b/Tt21fX037x4kXcfPPNKCsrQ2pqKrp164a1a9di165dZr3+yZMnY82aNdiwYQOeeOIJ3fZz585h27ZtmDRpEry8vIw+9uzZs/jXv/4FhUKBOXPmwN/fH0VFRcjMzDTr3E3t2LEDv//+O+6//35069YNR44cwfvvv48jR44gLy8PgiCY/VzLli1DdXW13rY33ngDBw8exFVXXQVAM2zk+++/x1133QWlUomioiKsWLECcXFxOHr0KDp27IjY2FjMmDEDb775Jp599llERUUBgO5nU5cvX0ZcXBwKCgrwxBNPIDQ0FJ999hmmTp2KiooKpKam6h2/du1aXLhwAY888ggEQcCrr76KxMRE/P777+jQoYMlb5/OlStX8Ndffxls9/b2NvlZNmfatGlYvXo1brnlFjz44IOor69Hbm4u8vLyMGjQILOf54MPPsAjjzyCoUOHIi0tDb///jvGjRuHwMBAqFQq3XGO/HtFZDaRyAWsWrVKBCD26NFD7NChg7hp0yazHzt37lwRgPjwww/rttXX14tKpVIUBEFcvHixbvv58+dFLy8vccqUKbptH330kejm5ibm5ubqPe+7774rAhD37Nmj23bp0iWD848ePVoMCwvT29ajRw8RgJiTk6PbdvbsWVEul4uzZs3Sbevfv7942223mf1atQICAsT+/fubffzw4cPF4cOH6+5r3+/CwkK943bt2iUCEHft2iWKoij+/PPPIgDxs88+a/b5vb299d5TrWnTpolBQUHiX3/9pbf9rrvuEv38/HTvp/a8YWFhBu9x0zZpXw8A8cMPP9Rtq62tFbt16yYmJSXpti1dulQEoPf7dPnyZbF3794Gz2lMfX29GBQUJA4ZMkRvu/Z3Y9u2bbptTd/Tzz//XAQg7tu3z+TzG3ttoiiKhYWFIgBx1apVum3GfvfWrVtn8Htm7LNt+vk3tWHDBhGA+OKLLzZ7vh9++MHgff/ss89MvpdNz7ts2TIRgPjxxx/rttXV1YlDhgwRO3XqJFZVVem9/quuuko8d+6c7tgvvvhCBCB+9dVXJl9Lc7T/Lo3dFi1aZLLdWlOmTBF79Oihu//dd9+JAMQZM2YYHNvQ0KB33sb/Ppp+7nV1dWKXLl3Ea6+9VqytrdUd9/7774sA9NriyL9XRObiZX1yKX/++Sc8PT31egrM9eCDD+r+293dHYMGDYIoipg2bZpuu7+/P3r16oXff/9dt+2zzz5DVFQUevfujb/++kt3u/nmmwFAr5etcc9KZWUl/vrrLwwfPhy///677jK3Vp8+fRATE6O7r1AoDM7t7++PI0eOID8/36LXWlVVBR8fH4se0xp+fn4AgG3btuHSpUsWPVYURWzcuBFjx46FKIp67+3o0aNRWVmJAwcO6D1mypQpZvdederUSW/soEwmw+DBg/Xe361btyI4OFh36R0APD098dBDD5l1Dnd3d9x111344Ycf9C5nr127Fl27dsWIESNMPtbf3x8A8PXXX+PKlStmna85jd+Xmpoa/PXXX7jxxhsBwOB9tMTRo0fxwAMPICEhQW8oSOPzXblyBX///TfCw8Ph7+/f6vN988036NatGyZNmqTb1qFDB8yYMQPV1dXIzs7WO37ixIkICAjQ3df+e2r8GVvqhhtuwI4dOwxujdtkro0bN0IQBMydO9dgnyU92fv378fZs2fx6KOP6l0tmDp1qu7foJY
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x600 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqcAAAIjCAYAAAA+xLLKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACDgElEQVR4nO3de1xT9f8H8NcB3EDkmlMJpoCAIqmZZqmBkJpaKQWomZWW3UnBS32tb33V0rTSksqs/PZVu2gakl3Me3LRIjUvmZcEA0HCpBQQFZBxfn/st8XYBhvscjZez8djD9o5Z+e8d4b03ufy/giiKIogIiIiIpIAF3sHQERERESkweSUiIiIiCSDySkRERERSQaTUyIiIiKSDCanRERERCQZTE6JiIiISDKYnBIRERGRZDA5JSIiIiLJYHJKRERERJLB5JSoDYqNjUVsbKy9w2iRzMxMCIKAzMxMe4fSpNWrV0MQBBQWFkouDnt9/o78e2dJwcHBmDJliva5Ob/TvIfUFjA5JUnS/A/1wIEDOtsrKiowcOBAuLu7Y+vWrXaKTrr+/PNPzJ49Gz179kT79u3h6emJ/v37Y8GCBSgvL7dZHK+++io2bdpks+vZwtixY9G+fXtcunTJ6DGTJk2CTCbD33//bcPIpOX48eOYN2+e3ZNyUwQHB0MQBIOPUaNG2Ts8h3Po0CF4eXnBxcUFGzZsMHrc/v378cwzzyAqKgqenp7o2rUrxo8fj1OnTtkwWpIyN3sHQGSqyspK3HHHHfjll1/w5Zdf8n8ejezfvx933nknqqqq8MADD6B///4AgAMHDmDx4sXIzs7G9u3bbRLLq6++iqSkJNxzzz0WP3dMTAyuXr0KmUxm8XM3ZdKkSfjmm2/w5Zdf4qGHHtLbf+XKFXz11VcYNWoUrrvuOjz44IO47777IJfLbRqnKaz5e3D8+HHMnz8fsbGxCA4Ottl1W+rGG2/ErFmz9LZff/31NovBXr/TlnTmzBncdddd8Pb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K5dO3HTpk0mv3bu3LkiAPHxxx/XbqurqxODgoJEQRDExYsXa7dfvHhR9PDwECdPnqzd9sknn4guLi5iTk6Oznnff/99EYC4d+9e7bYrV67oXX/kyJFiaGiozrZu3bqJAMTs7GzttvPnz4tyuVycNWuWdlvfvn3Fu+66y+T3quHn5yf27dvX5OOHDh0qDh06VPtcc78LCgp0jtu9e7cIQNy9e7coiqJ46NAhEYD4xRdfNHl+T09PnXuqMXXqVDEgIED866+/dLbfd999oo+Pj/Z+aq4bGhqqd48bx6R5PwDEjz/+WLutpqZG7NKli5iYmKjdtnTpUhGAzu/T1atXxZ49e+qd05C6ujoxICBAHDRokM52ze/Gtm3btNsa39Mvv/xSBCDu37/f6PkNvTdRFMWCggIRgLhq1SrtNkO/e+vWrdP7PTP02Tb+/BvbsGGDCEB8+eWXm7zejz/+qHffv/jiC6P3svF1ly1bJgIQP/30U+222tpacdCgQWKHDh3EyspKnfd/3XXXiRcuXNAe+9VXX4kAxG+++cboe2mK5t+loceiRYuMxq0xefJksVu3btrn33//vQhAnD59ut6x9fX1Otdt+O+j8edeW1srdurUSbzxxhvFmpoa7XEffvihCEAnFnv+vbKkTz75RAQgfvTRR1Y5PzkWduuTpP35559wd3fXaSkw1aOPPqr9b1dXVwwYMACiKGLq1Kna7b6+vujRowd+//137bYvvvgCkZGR6NmzJ/766y/t4/bbbwcAnVa2hi0rFRUV+OuvvzB06FD8/vvv2m5ujV69eulMDlAoFHrX9vX1xbFjx5CXl2fWe62srISXl5dZr2k
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x600 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqcAAAIjCAYAAAA+xLLKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm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MDPzw9dunTB8uXL9V5DqVRiwYIFiIiIgEwmg0KhwNNPPw2lUqnTbu3atbjlllvQqVMnyGQy9OrVC6tWrdJ7vdDQUNx5553Yu3evdlpCeHg4PvroI512V69exaJFixAZGQlPT0906NABN998M7799tsm3/Pq1atRWlqK119/HT179tQ737lzZzz33HNGn29sfqSh+Z35+fkYO3YsunTpAk9PT4SEhODee+9FZWUlAPXcuUuXLmHdunXaIdGpU6dqn19aWoqHHnoInTt3hkwmQ0xMDP773/8avO7GjRvx3HPPITg4GO3atUNVVZXBmOLi4nDttdfi2LFjiI+PR7t27RAcHIxXX31V772eOnUKd911F7y9vdGpUyfMmTMHO3fubHYeq5eXFxITE7F7926cPXtW7/z69evh6+uLu+66y+g9/fnnnzFq1Ch07NgRXl5eCAsLw0MPPdTk/QaAoqIiCIKg09v122+/YerUqQgPD4enpye6dOmChx56yKQpBY3nfmqGtw39aGI5deoUZsyYgR49esDLywsdOnTAuHHjdN7fhx9+iHHjxgEA4uPj9V7D0JzTs2fPYtq0aejcuTM8PT3Rt29frFu3zuD7X7ZsGd577z10794dMpkMN9xwAw4cONDs+22Kp6cnEhMT9b50bNiwAYGBgRg1apTec0y995q/iwoKCjB16lQEBATA398fDz74IC5fvmwwnq1bt+Laa6/V/r/RePpSaz+H0NBQHD16FFlZWdrjms/k/PnzmDdvHnr37g0fHx/4+fnhtttuw+HDh3Vi0Pw53bx5M5YsWYKQkBB4enpi+PDhKCgoMOm+k2tzt3cARK1x6dIl3HbbbThw4ADS09Nx5513mvzcCRMmIDo6GkuXLsW2bduwePFitG/fHqtXr8Ytt9yCV155BZ9++inmzZuHG264QduzWF9fj7vuugt79+7Fo48+iujoaBw5cgRvvPEGTpw4oTNcvWrVKsTExOCuu+6Cu7s7vvrqK8yYMQP19fWYOXOmTjwFBQVISkrCtGnTMGXKFPz3v//F1KlT0b9/f8TExABQ/2OWmpqKhx9+GAMHDkRVVRV+/vlnHDx4ECNHjjT6Xr/88kt4eXkhKSnJjLtrvtraWowaNQpKpRKzZs1Cly5dUFpaiq+//hoVFRXw9/fHxx9/rI3/0UcfBQB0794dgHpO7E033aRd/CGXy7F9+3ZMmzYNVVVVSElJ0bneSy+9BA8PD8ybNw9KpbLJXrcLFy5g9OjRSExMxPjx45Geno7/+7//Q+/evbU97ZcuXcItt9yCsrIyJCcno0uXLli/fj327Nlj0vufPHky1q1bh82bN+ssXjl//jx27tyJiRMnwsvLy+Bzz549i1tvvRVyuRzz589HQEAAioqKkJGRYdK1G/v222/x559/4sEHH0SXLl1w9OhRvPfeezh69Chyc3PN6rFdsWIFqqurdY698cYbOHToEDp06ABAPW3khx9+wL333ouQkBAUFRVh1apViIuLw7Fjx9CuXTsMHToUs2fPxptvvolnn30W0dHRAKD93diVK1cQFxeHgoICPPHEEwgLC8Nnn32GqVOnoqKiAsnJyTrt169fj4sXL+Kxxx6DIAh49dVXkZiYiD///BNt27Y15/bpmDRpEm699VacPHlS+2d1/fr1SEpKMvi65t778ePHIywsDKmpqTh48CDef/99dOrUCa+88opOu7179yIjIwMzZsyAr68v3nzzTYwdOxbFxcUW+xxWrFiBWbNmwcfHB//5z38AqL+8AsCff/6JrVu3Yty4cQgLC8Pff/+N1atXY9iwYTh27BiuueYanXiXLl2KNm3aYN68eaisrMSrr76KyZMn46effmrxZ0EuQiRyQGvXrhUBiN26dRPbtm0rbt261eTnLliwQAQgPvroo9pjdXV1YkhIiCgIgrh06VLt8QsXLoheXl7ilClTtMc+/vhjsU2bNmJOTo7O67777rsiAHHfvn3aY5cvX9a7/qhRo8Tw8HCdY926dRMBiNnZ2dpjZ8+eFWUymTh37lztsb59+4p33HGHye9VIzAwUOzbt6/J7YcNGyYOGzZM+1hzvwsLC3Xa7dmzRwQg7tmzRxRFUfz1119FAOJnn33W5Ot7e3vr3FONadOmiUFBQeI///yjc/zee+8V/f39tfdTc93w8HC9e9w4Js37ASB+9NFH2mNKpVLs0qWLOHbsWO2x5cuXiwB0/jxduXJF7Nmzp95rGlJXVycGBQWJgwYN0jmu+bOxc+dO7bHG9/Tzzz8XAYgHDhww+vqG3psoimJhYaEIQFy7dq32mKE/exs2bND7c2bos238+Te2efNmEYD44osvNnm9H3/8Ue++f/bZZ0bvZePrrlixQgQgfvLJJ9pjtbW14qBBg0QfHx+xqqpK5/136NBBPH/+vLbtF198IQIQv/rqK6PvpSndunUT77jjDrGurk7s0qWL+NJLL4miKIrHjh0TAYhZWVna+9fwczP13mv+LnrooYd02t5zzz1ihw4ddI4BED08PMSCggLtscOHD4sAxLfeeqvJa5v7OcTExBj8/GtqakSVSqVzrLCwUJTJZDp/FjR/TqOjo0WlUqk9npaWJgIQjxw5ovfaRA1xWJ8c2t9//w1PT08oFAqzn/vwww9r/9vNzQ0DBgyAKIqYNm2a9nhAQAB69OiBP//8U3vss88+Q3R0NHr27Il//vlH+3PLLbcAgE4vW8NessrKSvzzzz8YNmwY/vzzT+0wt0avXr0QGxurfSyXy/WuHRAQgKNHjyI/P9+s91pVVQVfX1+zntMS/v7+AICdO3caHZY0RhRFbNm
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x600 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAqcAAAIjCAYAAAA+xLLKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAACE/0lEQVR4nO3deVhUZf8G8PsAMiCy6qjEjAICirhkmqUGQmpavUoBamalZYtpCi6V9VZqaVpmSWVW1mu2uIVoi7kni5apuVSiCQaChEkp4AbIcH5/zG8mhpmBGZjlzHB/rouL5pwzc545Y3rPc57n+wiiKIogIiIiIpIAF3s3gIiIiIhIg+GUiIiIiCSD4ZSIiIiIJIPhlIiIiIgkg+GUiIiIiCSD4Z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LWsf95ly5aJAMTPP/9cu626ulocMGCA2KZNG7GiokLn/bdt21a8cOGC9tivvvpKBCB+8803Rt9LQzp37izefffdYk1NjdixY0fxlVdeEUVRFHNyckQAYmZmpvb61f3cNH/H1AVAdHd3F/Py8rTbjh07JgIQ33nnHb3nlpaWiidOnBBvuOEG8eabb9Z5XykpKSIAnb+DLl26JIaEhIjBwcGiSqUSRVEUlyxZIrq6umqv09tvvy127txZ7N+/v/jss8+KoiiKKpVK9PPzE2fMmCGKovrvPADikiVLTLo2oiiKqampoiAI2utT/33PnTu3wdci0uBtfXIKf/31Fzw8PKBUKs1+7qOPPqr9b1d
|
||
|
"text/plain": [
|
||
|
"<Figure size 800x600 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\n",
|
||
|
"clusters, centroids = k_means(data, k, euclidean_distance, startCentroids)\n",
|
||
|
"\n",
|
||
|
"# Wizualizacja\n",
|
||
|
"plt.figure(figsize=(8, 6))\n",
|
||
|
"colors = ['r', 'g', 'b', 'y']\n",
|
||
|
"for i, cluster in enumerate(clusters):\n",
|
||
|
" cluster = np.array(cluster)\n",
|
||
|
" plt.scatter(cluster[:, 0], cluster[:, 1], c=colors[i], label=f'Cluster {i+1}')\n",
|
||
|
"plt.scatter(centroids[:, 0], centroids[:, 1], c='black', marker='X', s=100, label='Centroids')\n",
|
||
|
"plt.legend()\n",
|
||
|
"plt.title(\"K-means Clustering Visualization, Euclidean\")\n",
|
||
|
"plt.xlabel(\"X coordinate\")\n",
|
||
|
"plt.ylabel(\"Y coordinate\")\n",
|
||
|
"plt.show()\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"clusters, centroids = k_means(data, k, euclidean2_distance, startCentroids)\n",
|
||
|
"\n",
|
||
|
"# Wizualizacja\n",
|
||
|
"plt.figure(figsize=(8, 6))\n",
|
||
|
"colors = ['r', 'g', 'b', 'y']\n",
|
||
|
"for i, cluster in enumerate(clusters):\n",
|
||
|
" cluster = np.array(cluster)\n",
|
||
|
" plt.scatter(cluster[:, 0], cluster[:, 1], c=colors[i], label=f'Cluster {i+1}')\n",
|
||
|
"plt.scatter(centroids[:, 0], centroids[:, 1], c='black', marker='X', s=100, label='Centroids')\n",
|
||
|
"plt.legend()\n",
|
||
|
"plt.title(\"K-means Clustering Visualization, Euclidean^2\")\n",
|
||
|
"plt.xlabel(\"X coordinate\")\n",
|
||
|
"plt.ylabel(\"Y coordinate\")\n",
|
||
|
"plt.show()\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"clusters, centroids = k_means(data, k, manhattan_distance, startCentroids)\n",
|
||
|
"\n",
|
||
|
"# Wizualizacja\n",
|
||
|
"plt.figure(figsize=(8, 6))\n",
|
||
|
"colors = ['r', 'g', 'b', 'y']\n",
|
||
|
"for i, cluster in enumerate(clusters):\n",
|
||
|
" cluster = np.array(cluster)\n",
|
||
|
" plt.scatter(cluster[:, 0], cluster[:, 1], c=colors[i], label=f'Cluster {i+1}')\n",
|
||
|
"plt.scatter(centroids[:, 0], centroids[:, 1], c='black', marker='X', s=100, label='Centroids')\n",
|
||
|
"plt.legend()\n",
|
||
|
"plt.title(\"K-means Clustering Visualization, Manhattan\")\n",
|
||
|
"plt.xlabel(\"X coordinate\")\n",
|
||
|
"plt.ylabel(\"Y coordinate\")\n",
|
||
|
"plt.show()\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"clusters, centroids = k_means(data, k, minkowski_distance, startCentroids)\n",
|
||
|
"\n",
|
||
|
"# Wizualizacja\n",
|
||
|
"plt.figure(figsize=(8, 6))\n",
|
||
|
"colors = ['r', 'g', 'b', 'y']\n",
|
||
|
"for i, cluster in enumerate(clusters):\n",
|
||
|
" cluster = np.array(cluster)\n",
|
||
|
" plt.scatter(cluster[:, 0], cluster[:, 1], c=colors[i], label=f'Cluster {i+1}')\n",
|
||
|
"plt.scatter(centroids[:, 0], centroids[:, 1], c='black', marker='X', s=100, label='Centroids')\n",
|
||
|
"plt.legend()\n",
|
||
|
"plt.title(\"K-means Clustering Visualization, Minkowski\")\n",
|
||
|
"plt.xlabel(\"X coordinate\")\n",
|
||
|
"plt.ylabel(\"Y coordinate\")\n",
|
||
|
"plt.show()\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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.13.0"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|