254 lines
56 KiB
Plaintext
254 lines
56 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "48214590-414c-4dd0-9d1f-9446c1d751a0",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "f0738b63-6ea5-4bb0-81e3-101ff9d67a63",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from sklearn import datasets, metrics, svm\n",
|
||
|
"from sklearn.model_selection import train_test_split"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "8a26a325-096c-45fd-b0ef-cb0166a4833d",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxsAAADQCAYAAABvGXwjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAARa0lEQVR4nO3da2yW5f0H8F9nCVCRtbAhEg+lceqmYqNuDpeFomU6Mi1uKwRtZilOlhkXNpKVF2Zithl4pTvgRjLEbUYmEoRMhc1CW5c4hjSD7Dw3zsPFRco2nSNF7v+LhWZd+dsC17VnLZ9P0oRefe7vffXwo8+399OnZUVRFAEAAJDYO0q9AQAAYHhSNgAAgCyUDQAAIAtlAwAAyELZAAAAslA2AACALJQNAAAgC2UDAADIQtkAAACyGDZlo6ysbFAvHR0dp3WeJUuWRFlZ2Skd29HRkWQPqb366qvR3Nwc73rXu6KioiKmTp0amzdvLvW2yMSsnJoDBw7EwoULY9q0aVFZWRllZWXx2G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EAWygYAAJCFsgEAAGShbAAAAFkoGwAAQBbKBgAAkEV5qTeQWmtra/LM3bt3J8/s7u5OnhkRMW7cuOSZa9asSZ7Z2NiYPJPSqqysTJ7Z2dmZPDMior29PXlmQ0ND8kxKa8eOHckzp0+fnjzzne98Z/LMiIg9e/ZkyaW0Fi9enDwzx/2EFStWJM9csGBB8syIiK6uruSZ9fX
|
||
|
"text/plain": [
|
||
|
"<Figure size 1000x300 with 4 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"### load dataset \n",
|
||
|
"\n",
|
||
|
"digits = datasets.load_digits()\n",
|
||
|
"\n",
|
||
|
"### show training data\n",
|
||
|
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10,3))\n",
|
||
|
"for ax, image, label in zip(axes, digits.images, digits.target):\n",
|
||
|
" ax.set_axis_off()\n",
|
||
|
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
|
||
|
" ax.set_title(\"Training: %i\" % label)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "8355cc2f-3128-43a3-8122-1517d11b911a",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"[5 6 1 6 5 7 1 5 3 7 4 1 2 5 4 9 8 2 6 6 0 5 4 9 7 4 8 7 5 6 5 3 8 9 9 6 1\n",
|
||
|
" 1 0 6 8 4 8 8 8 1 2 7 1 2 0 1 8 8 9 6 4 6 7 0 4 1 0 5 4 1 7 2 0 6 7 9 1 5\n",
|
||
|
" 5 4 0 6 6 2 5 4 1 1 0 9 5 6 5 0 5 1 5 1 3 0 2 5 8 0 1 9 1 1 7 0 6 4 9 8 7\n",
|
||
|
" 3 7 4 4 2 7 3 6 2 6 1 9 7 4 8 5 5 1 3 9 4 3 7 8 0 5 8 9 5 5 0 0 5 0 6 1 8\n",
|
||
|
" 8 4 0 4 5 0 1 7 6 9 7 9 5 7 5 6 9 5 0 6 9 6 7 6 2 0 3 3 8 4 9 7 1 2 9 5 5\n",
|
||
|
" 7 0 9 1 4 6 1 4 0 5 4 9 6 7 1 6 8 1 7 0 7 0 1 5 7 1 6 1 5 1 6 6 7 3 2 7 3\n",
|
||
|
" 6 4 2 5 1 2 4 6 4 8 1 2 2 5 6 2 4 8 5 6 3 1 6 8 2 8 3 9 7 1 5 7 3 4 8 4 5\n",
|
||
|
" 1 5 4 3 9 0 1 9 5 4 1 4 3 2 2 4 5 7 6 2 0 9 2 6 4 6 8 4 4 5 8 0 4 1 1 3 4\n",
|
||
|
" 8 3 3 5 6 3 3 6 7 0 8 3 4 2 8 5 1 0 8 4 7 1 6 3 6 2 7 0 5 8 2 0 0 0 1 3 0\n",
|
||
|
" 7 8 8 5 1 0 6 8 2 0 0 5 9 7 5 4 5 7 6 3 5 3 9 3 5 5 3]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from sklearn.tree import DecisionTreeClassifier\n",
|
||
|
"# gini a criterion to minimize the probability of misclassification\n",
|
||
|
"clf_dt = DecisionTreeClassifier(criterion = 'gini')\n",
|
||
|
"\n",
|
||
|
"X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, shuffle=True)\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"clf_dt.fit(X_train, y_train)\n",
|
||
|
"predicted = clf_dt.predict(X_test)\n",
|
||
|
"print(predicted)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "a4c53a12-f834-426d-8217-ea98962fda64",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"0.8805555555555555"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from sklearn.metrics import accuracy_score\n",
|
||
|
"\n",
|
||
|
"accuracy_score(y_test , predicted)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "e2b99748-73e4-4e6a-bd3f-4a095eec4c12",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAxsAAADQCAYAAABvGXwjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAWnklEQVR4nO3dfZBWZf0/8M/CIsjjCoIYJiSh0KhsxhiOBos4kAjh0zCOmkBqWtaID8k42QRqSGqJY5jTmCz4UJkWTJKYNSyGomKmo2M5WG6MmoTI+ojy4PX7w99u3SwPC56Le5fv6zWzf3Ducz7nuu49n/vaN2fveytSSikAAAAK1q7cAwAAAPZOwgYAAJCFsAEAAGQhbAAAAFkIGwAAQBbCBgAAkIWwAQAAZCFsAAAAWQgbAABAFrsUNmpra6OioqLpq7KyMg466KCYOnVqvPrqq7nGWGLAgAExZcqUpn/X1dVFRUVF1NXV7VKdxx57LGbMmBENDQ3NHqupqYmamppPNM4iNc5xW1+PP/54uYe3U/X19VFRURG1tbW7dXxbm78+Ka/ly5fHuHHjYr/99ot99903Bg0aFNdcc025h7VTn7RPGrWV+euT8njnnXfiiiuuiDFjxkTv3r2joqIiZsyYUe5htVgRffLkk0/G2LFjo1u3btG1a9cYNWpUPProo8UNsmB6pbzaymvq1lrTmlK5OyeeN29eDB48ODZs2BCPPPJIXHfddbFs2bJ47rnnokuXLrtTcrcdddRRsWLFivjc5z63S8c99thjMXPmzJgyZUpUVV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aNWsiIuK8886Lb3/72xER8Zvf/CZWrFgRK1asiKOOOmqb9T744IMYNWpULFiwIC699NJYvHhxnH322XH99dfHqaee2mz/xYsXx09+8pO4+uqr4/7774+ePXvGKaecEv/85z9L9quoqNil30+86KKLorKyMrp37x5jx46N5cuXt/jY7bnyyitj9erVcdttt8Xvfve76NOnT9x1110xZsyY6N69e8yfPz/uvffe6NmzZ4wdO7bkon/ttdeiV69eMXv27FiyZEnMnTs3Kisr44tf/GK8+OKLOz13a5j/nqRPShXdJ4888kj07Nkz/v73v0d1dXVUVlZGnz594sILL4y33357p8fvSFvok5zz35P0Salc60kObaFPNm7cGB07dmy2vXFbW/rPMr1SyprSiteUtAvmzZuXIiI9/vjjadOmTemdd95JDzzwQOrdu3fq1q1bev3111NKKU2ePDlFRLrjjjtKjv/FL36RIiLdf//9JdtXrlyZIiLdeuutKaWU/va3v6WISJdccknJfnfffXeKiDR58uSmbUuXLk0RkZYuXdq0beDAgWngwIFpw4YN253LDTfckCIivfzyy80eGzlyZBo5cmTTv2+77bYUEenee+8t2e+HP/xhioj0hz/8oWlbRKQDDjggvf32203bXn/99dSuXbt03XXXlRzfvn37dPzxx293jI2efvrpdPHFF6ff/va36ZFHHkl33HFHGjJkSGrfvn1asmTJTo/flsbnbcSIESXb33vvvdSzZ880YcKEku1btmxJQ4cOTUcfffR2a27evDlt3LgxDRo0qOR79/LLL6eISPPmzSvZv5zzz0mflKdPDjvssNSpU6fUrVu3NGvWrLR06dJ0/fXXp3333Tcde+yx6aOPPtppja21pT7JMf+c9El5+uR/rV27NkVE+v73v79Lx22tLfVJdXV1OvTQQ9OWLVuatm3atCkdcsghKSLSPffcs9Mae5pesaa09TVlt8LG1l9HHHFEWr58edN+jRf8W2+9VXL8WWedlaqqqtLGjRvTpk2bSr769u2bJk2alFJK6dZbb00RkZ566qmS4zdt2pQqKyt3eMG/+OKLKSLSrFmzdjiXXbngJ02alLp06dLsiV2zZk2KiDR9+vSmbRGRzjjjjGY1+/btmy688MIdjmlXrF+/Ph100EHpyCOP3K3jG5+3m2++uWT7ww8/nCIi3Xfffc2+R9OnT08VFRXp3XffTSl9/P34wQ9+kIYMGZI6dOhQck18+ctfbqq5vQv+k/ik889Jn5SnTwYNGpQiotnCMmfOnBQR6eGHH97lmm2pT3LMPyd9Uv71pOiw0Rb65Oc//3mKiPSNb3wjvfLKK2n16tXp3HPPTe3bt08RkX75y1/u9vOQi16xprT1NWW33o2yYMGCGDJkSFRWVsYBBxwQBx54YLN9OnfuHN27dy/ZtmbNmmhoaIh99tlnm3XfeOONiPj40zIiIvr27VvyeGVlZfTq1WuHY2v8/cODDjqoZZNpgXXr1kXfvn2b/Z5rnz59orKysmm8jbY1xo4dO5bcqvykqqqqYvz48XHbbbfFhg0bYt99992tOlt/7xpvd55++unbPebNN9+MLl26xKWXXhpz586N6dOnx8iRI2O//faLdu3axXnnnVfoXLelqPnnpE8+tqf6pFevXrFq1aoYO3ZsyfYTTzwxpk2bFk8//XSccMIJu1W7LfRJzvnnpE8+Vs71pChtoU++9rWvxdq1a+Paa6+Nn/70pxERccwxx8Tll18eP/zhD6Nfv36FnatoeuVj1pS2t6bsVtgYMmRI0ycibM+23oC2//77R69evWLJkiXbPKZbt24R8d8L5vXXXy9p/M2bNze7uLbW+LuLr7zyyg732xW9evWKJ554IlJKJfP6z3/+E5s3b47999+/sHPtipRSRGz7uW6prY9tnMstt9yy3U+aOOCAAyIi4q677opzzjknZs2aVfL4G2+80ewj7XIoYv456ZOP7ak+OfLII7f5d1car5N27Xb/LWptoU9yzj8nffKxcq8nRWgLfRIRMX369Jg2bVqsWrUqunXrFv37948LLrggunTpEl/4whcKPVeR9MrHrCltb03Zo6vP+PHjY926dbFly5YYNmxYs6/DDjssIqLpjSt33313yfH33ntvbN68eYfnOPTQQ2PgwIFxxx13NPu
|
||
|
"text/plain": [
|
||
|
"<Figure size 1000x300 with 4 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"_, axes = plt.subplots(nrows=1, ncols=4, figsize=(10,3))\n",
|
||
|
"for ax, image, prediction, t in zip(axes, X_test, predicted, y_test):\n",
|
||
|
" ax.set_axis_off()\n",
|
||
|
" image = image.reshape(8,8)\n",
|
||
|
" ax.imshow(image, cmap=plt.cm.gray_r, interpolation=\"nearest\")\n",
|
||
|
" ax.set_title(f\"Prediction: {prediction}, real: {t}\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"id": "1d010c44-2967-44fa-996f-c1fd733d17f1",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Confusion Matrix:\n",
|
||
|
"[[36 0 0 0 0 1 0 0 0 0]\n",
|
||
|
" [ 0 37 1 1 0 0 0 0 2 1]\n",
|
||
|
" [ 0 0 21 0 0 0 0 0 2 0]\n",
|
||
|
" [ 0 0 4 26 0 2 1 0 1 1]\n",
|
||
|
" [ 0 1 0 0 31 0 1 0 0 0]\n",
|
||
|
" [ 0 0 0 0 2 43 0 1 0 0]\n",
|
||
|
" [ 1 1 0 0 4 0 40 0 0 0]\n",
|
||
|
" [ 0 0 0 0 1 1 0 34 1 0]\n",
|
||
|
" [ 0 3 0 0 0 1 0 0 27 2]\n",
|
||
|
" [ 0 2 0 1 1 2 0 0 0 22]]\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfIAAAHgCAYAAABej+9AAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8g+/7EAAAACXBIWXMAAA9hAAAPYQGoP6dpAABo60lEQVR4nO3dd3gU5f7+8fcSkk0hCVISEgm9SAcBIYgCCmjgcPDgERBR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LPPvsshw8fZvjw4Zw8eZKaNWu6nGdfGCtXriQhIYGnn37apWdl9uzZtGrVin79+rFu3ToCAgJK4tcTKVEOw7jg6goiIiLiVXSMXERExIupkIuIiHgxFXIREREvpkIuIiLixVTIRUREvJgKuYiIiBdTIRcREfFiKuQiIiJeTIVcRETEi6mQi4iIeDEVchERES+mQi4iIuLFVMhFRES8mAq5iIiIF1MhFxER8WIq5CIiIl5MhVxERMSLqZCLiIh4MRVyERERL6ZCLiIi4sVUyEVERLyYCrmIiIgXUyEXERHxYirkIiIiXkyFXERExIupkIuIiHgxFXIREREvpkIuIiLixVTIRUREvJgKuYiIiBdTIRcREfFiKuQiIiJeTIVcRETEi5W3OkBx5OTkcOjQIUJDQ3E4HFbHERERNxmGwcmTJ4mOjqZcudJrW549e5bMzMxirycgIIDAwMASSFRyvLqQHzp0iJiYGKtjiIhIMSUlJVG
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 2 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"### confusion matrix\n",
|
||
|
"\n",
|
||
|
"disp = metrics.ConfusionMatrixDisplay.from_predictions(y_test, predicted, cmap=\"summer\")\n",
|
||
|
"disp.figure_.suptitle(\"Confusion Matrix\")\n",
|
||
|
"\n",
|
||
|
"print(f\"Confusion Matrix:\\n{disp.confusion_matrix}\")\n",
|
||
|
"\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"id": "8608e6eb-6dd0-4947-9ee6-dc77f35cecc7",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Classification report for classifier DecisionTreeClassifier():\n",
|
||
|
" precision recall f1-score support\n",
|
||
|
"\n",
|
||
|
" 0 0.97 0.97 0.97 37\n",
|
||
|
" 1 0.84 0.88 0.86 42\n",
|
||
|
" 2 0.81 0.91 0.86 23\n",
|
||
|
" 3 0.93 0.74 0.83 35\n",
|
||
|
" 4 0.79 0.94 0.86 33\n",
|
||
|
" 5 0.86 0.93 0.90 46\n",
|
||
|
" 6 0.95 0.87 0.91 46\n",
|
||
|
" 7 0.97 0.92 0.94 37\n",
|
||
|
" 8 0.82 0.82 0.82 33\n",
|
||
|
" 9 0.85 0.79 0.81 28\n",
|
||
|
"\n",
|
||
|
" accuracy 0.88 360\n",
|
||
|
" macro avg 0.88 0.88 0.88 360\n",
|
||
|
"weighted avg 0.89 0.88 0.88 360\n",
|
||
|
"\n",
|
||
|
"\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(\n",
|
||
|
" f\"Classification report for classifier {clf_dt}:\\n\"\n",
|
||
|
" f\"{metrics.classification_report(y_test, predicted)}\\n\"\n",
|
||
|
")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"id": "42fdd6a5-af00-4155-bc42-44e037d13c2d",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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.11.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|