Initial commit
This commit is contained in:
@@ -931,7 +931,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 24,
|
"execution_count": null,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@@ -954,19 +954,21 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"plt.figure(figsize=(15, 5))\n",
|
"plt.figure(figsize=(15, 5))\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"# RGB format : img[red, green, blue] -> img[:,:,0], img[:,:,1], img[:,:,2]\n",
|
||||||
|
"\n",
|
||||||
"# Red channel\n",
|
"# Red channel\n",
|
||||||
"plt.subplot(1, 3, 1)\n",
|
"plt.subplot(1, 3, 1)\n",
|
||||||
"plt.imshow(img[:, :, 0], cmap='Reds')\n",
|
"plt.imshow(img[:,:,0], cmap='Reds')\n",
|
||||||
"plt.title('Red Channel')\n",
|
"plt.title('Red Channel')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Green channel\n",
|
"# Green channel\n",
|
||||||
"plt.subplot(1, 3, 2)\n",
|
"plt.subplot(1, 3, 2)\n",
|
||||||
"plt.imshow(img[:, :, 1], cmap='Greens')\n",
|
"plt.imshow(img[:,:,1], cmap='Greens')\n",
|
||||||
"plt.title('Green Channel')\n",
|
"plt.title('Green Channel')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"# Blue channel\n",
|
"# Blue channel\n",
|
||||||
"plt.subplot(1, 3, 3)\n",
|
"plt.subplot(1, 3, 3)\n",
|
||||||
"plt.imshow(img[:, :, 2], cmap='Blues')\n",
|
"plt.imshow(img[:,:,2], cmap='Blues')\n",
|
||||||
"plt.title('Blue Channel')\n",
|
"plt.title('Blue Channel')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"plt.show()\n",
|
"plt.show()\n",
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 5,
|
||||||
"id": "26758936",
|
"id": "26758936",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -63,7 +63,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 6,
|
||||||
"id": "a23f62b5",
|
"id": "a23f62b5",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -81,7 +81,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 7,
|
||||||
"id": "f4bec500",
|
"id": "f4bec500",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@@ -186,7 +186,7 @@
|
|||||||
"4 Y "
|
"4 Y "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 7,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@@ -228,7 +228,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 8,
|
||||||
"id": "2c56efa5",
|
"id": "2c56efa5",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -246,7 +246,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 9,
|
||||||
"id": "dc5f9cda",
|
"id": "dc5f9cda",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -264,7 +264,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 6,
|
"execution_count": 10,
|
||||||
"id": "83beacfb",
|
"id": "83beacfb",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -283,7 +283,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 7,
|
"execution_count": 11,
|
||||||
"id": "9c567bb7",
|
"id": "9c567bb7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -301,7 +301,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 12,
|
||||||
"id": "c0db7c1f",
|
"id": "c0db7c1f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -319,7 +319,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 9,
|
"execution_count": 13,
|
||||||
"id": "b05be2cc",
|
"id": "b05be2cc",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -345,7 +345,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 10,
|
"execution_count": null,
|
||||||
"id": "30919672",
|
"id": "30919672",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -356,36 +356,20 @@
|
|||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" Initialize the class.\n",
|
" Initialize the class.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass\n",
|
" self.classes_ = None\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def fit(self, X, y):\n",
|
" def fit(self, X, y):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" Fit the dummy classifier.\n",
|
" Fit the dummy classifier.\n",
|
||||||
" \n",
|
|
||||||
" Parameters\n",
|
|
||||||
" ----------\n",
|
|
||||||
" X : Numpy array or Pandas DataFrame of shape (n_samples, n_features)\n",
|
|
||||||
" Training data.\n",
|
|
||||||
" y : Numpy array or Pandas DataFrame of shape (n_samples,)\n",
|
|
||||||
" Target values.\n",
|
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass\n",
|
" self.classes_ = np.unique(y)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def predict(self, X):\n",
|
" def predict(self, X):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" Predict the class labels for the provided data.\n",
|
" Predict the class labels for the provided data.\n",
|
||||||
"\n",
|
|
||||||
" Parameters\n",
|
|
||||||
" ----------\n",
|
|
||||||
" X : Numpy array or Pandas DataFrame of shape (n_queries, n_features)\n",
|
|
||||||
" Test samples.\n",
|
|
||||||
"\n",
|
|
||||||
" Returns\n",
|
|
||||||
" -------\n",
|
|
||||||
" y : Numpy array or Pandas DataFrame of shape (n_queries,)\n",
|
|
||||||
" Class labels for each data sample.\n",
|
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass"
|
" n_samples = len(X)\n",
|
||||||
|
" return np.random.choice(self.classes_, size=n_samples)\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -398,7 +382,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 11,
|
"execution_count": 15,
|
||||||
"id": "184f3905",
|
"id": "184f3905",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -441,7 +425,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 16,
|
||||||
"id": "759e924e",
|
"id": "759e924e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@@ -457,7 +441,10 @@
|
|||||||
" n_neighbors : int, default=3\n",
|
" n_neighbors : int, default=3\n",
|
||||||
" Number of neighbors to use by default.\n",
|
" Number of neighbors to use by default.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass\n",
|
"\n",
|
||||||
|
" self.n_neighbors = n_neighbors\n",
|
||||||
|
" self.X_train = None\n",
|
||||||
|
" self.y_train = None\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def fit(self, X, y):\n",
|
" def fit(self, X, y):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
@@ -470,7 +457,9 @@
|
|||||||
" y : Numpy array or Pandas DataFrame of shape (n_samples,)\n",
|
" y : Numpy array or Pandas DataFrame of shape (n_samples,)\n",
|
||||||
" Target values.\n",
|
" Target values.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass\n",
|
"\n",
|
||||||
|
" self.X_train = np.array(X)\n",
|
||||||
|
" self.y_train = np.array(y)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" @staticmethod\n",
|
" @staticmethod\n",
|
||||||
" def _euclidian_distance(a, b):\n",
|
" def _euclidian_distance(a, b):\n",
|
||||||
@@ -484,7 +473,23 @@
|
|||||||
" b : Numpy array or Pandas DataFrame\n",
|
" b : Numpy array or Pandas DataFrame\n",
|
||||||
" Second operand.\n",
|
" Second operand.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass\n",
|
"\n",
|
||||||
|
" return np.sqrt(np.sum((a - b) ** 2, axis=1))\n",
|
||||||
|
" \n",
|
||||||
|
" @staticmethod\n",
|
||||||
|
" def _manhattan_distance(a, b):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Utility function to compute the Manhattan distance.\n",
|
||||||
|
" \n",
|
||||||
|
" Parameters\n",
|
||||||
|
" ----------\n",
|
||||||
|
" a : Numpy array or Pandas DataFrame\n",
|
||||||
|
" First operand.\n",
|
||||||
|
" b : Numpy array or Pandas DataFrame\n",
|
||||||
|
" Second operand.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" \n",
|
||||||
|
" return np.sum(np.abs(a - b), axis=1)\n",
|
||||||
" \n",
|
" \n",
|
||||||
" def predict(self, X):\n",
|
" def predict(self, X):\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
@@ -500,7 +505,16 @@
|
|||||||
" y : Numpy array or Pandas DataFrame of shape (n_queries,)\n",
|
" y : Numpy array or Pandas DataFrame of shape (n_queries,)\n",
|
||||||
" Class labels for each data sample.\n",
|
" Class labels for each data sample.\n",
|
||||||
" \"\"\"\n",
|
" \"\"\"\n",
|
||||||
" pass"
|
" X = np.array(X)\n",
|
||||||
|
" predictions = []\n",
|
||||||
|
" for x in X:\n",
|
||||||
|
" distances = self._euclidian_distance(self.X_train, x)\n",
|
||||||
|
" neighbor_idxs = np.argsort(distances)[:self.n_neighbors]\n",
|
||||||
|
" neighbor_labels = self.y_train[neighbor_idxs]\n",
|
||||||
|
" \n",
|
||||||
|
" counts = np.bincount(neighbor_labels.astype(int))\n",
|
||||||
|
" predictions.append(np.argmax(counts))\n",
|
||||||
|
" return np.array(predictions)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -511,6 +525,32 @@
|
|||||||
"Compute the performance of the system as a function of $k = 1...7$."
|
"Compute the performance of the system as a function of $k = 1...7$."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 18,
|
||||||
|
"id": "4214d221",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"KNN accuracies for k=1 to 7: [None, None, None, None, None, None, None]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"knn_accuracies = []\n",
|
||||||
|
"for k in range(1, 8):\n",
|
||||||
|
" knn = KNNClassifier(n_neighbors=k)\n",
|
||||||
|
" knn.fit(X_train, y_train)\n",
|
||||||
|
" y_pred = knn.predict(X_test)\n",
|
||||||
|
" acc = accuracy_score(y_test, y_pred)\n",
|
||||||
|
" knn_accuracies.append(acc)\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"KNN accuracies for k=1 to 7:\", knn_accuracies)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -578,7 +618,7 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3 (ipykernel)",
|
"display_name": "MLvenv",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
@@ -592,7 +632,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.7.11"
|
"version": "3.10.18"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
Reference in New Issue
Block a user