From af2862cec7c8180bfbfc60fe19d9999f88d618df Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 29 Jul 2026 23:47:15 -0400 Subject: [PATCH] Final commit --- Classification Lab/inclass_lab.ipynb | 1171 +++++++++++++++++++++++++- HW 5/Iris_knn_W2025_template.ipynb | 2 +- 2 files changed, 1147 insertions(+), 26 deletions(-) diff --git a/Classification Lab/inclass_lab.ipynb b/Classification Lab/inclass_lab.ipynb index b588c82..809a900 100644 --- a/Classification Lab/inclass_lab.ipynb +++ b/Classification Lab/inclass_lab.ipynb @@ -97,10 +97,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "a8899ca0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['malignant' 'benign']\n", + "(569, 30)\n" + ] + } + ], "source": [ "from sklearn.datasets import load_breast_cancer\n", "data = load_breast_cancer()\n", @@ -127,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "8db6dfd6", "metadata": { "lines_to_next_cell": 0 @@ -154,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "75fe2532", "metadata": {}, "outputs": [], @@ -176,7 +185,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "6eb66fc0", "metadata": {}, "outputs": [], @@ -199,10 +208,907 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "f31f0262", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
DecisionTreeClassifier()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "DecisionTreeClassifier()" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "knn = KNeighborsClassifier(n_neighbors=5)\n", "logreg = LogisticRegression(max_iter=5000)\n", @@ -248,10 +1154,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "395d887c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "KNN: 0.956140350877193\n", + "LogReg: 0.956140350877193\n", + "Tree: 0.9298245614035088\n" + ] + } + ], "source": [ "print(\"KNN:\", knn.score(X_test, y_test))\n", "print(\"LogReg:\", logreg.score(X_test, y_test))\n", @@ -272,10 +1188,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "deec64c6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[38 5]\n", + " [ 0 71]]\n" + ] + } + ], "source": [ "from sklearn.metrics import confusion_matrix\n", "print(confusion_matrix(y_test, knn.predict(X_test)))" @@ -315,10 +1240,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "e9ef7bea", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " precision recall f1-score support\n", + "\n", + " 0 1.00 0.88 0.94 43\n", + " 1 0.93 1.00 0.97 71\n", + "\n", + " accuracy 0.96 114\n", + " macro avg 0.97 0.94 0.95 114\n", + "weighted avg 0.96 0.96 0.96 114\n", + "\n" + ] + } + ], "source": [ "from sklearn.metrics import classification_report\n", "print(classification_report(y_test, knn.predict(X_test)))" @@ -339,10 +1280,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "84e1f5d4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== RESULTS WITH SCALING ===\n", + "KNN Accuracy: 0.9473684210526315\n", + "Logistic Regression Accuracy: 0.9736842105263158\n", + "Decision Tree Accuracy: 0.9473684210526315\n" + ] + } + ], "source": [ "from sklearn.pipeline import Pipeline\n", "from sklearn.preprocessing import StandardScaler\n", @@ -391,10 +1344,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "6643f0af", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== CLASSIFICATION REPORTS (WITH SCALING) ===\n", + "\n", + "KNN Report:\n", + " precision recall f1-score support\n", + "\n", + " 0 0.93 0.93 0.93 43\n", + " 1 0.96 0.96 0.96 71\n", + "\n", + " accuracy 0.95 114\n", + " macro avg 0.94 0.94 0.94 114\n", + "weighted avg 0.95 0.95 0.95 114\n", + "\n", + "\n", + "Logistic Regression Report:\n", + " precision recall f1-score support\n", + "\n", + " 0 0.98 0.95 0.96 43\n", + " 1 0.97 0.99 0.98 71\n", + "\n", + " accuracy 0.97 114\n", + " macro avg 0.97 0.97 0.97 114\n", + "weighted avg 0.97 0.97 0.97 114\n", + "\n", + "\n", + "Decision Tree Report:\n", + " precision recall f1-score support\n", + "\n", + " 0 0.93 0.93 0.93 43\n", + " 1 0.96 0.96 0.96 71\n", + "\n", + " accuracy 0.95 114\n", + " macro avg 0.94 0.94 0.94 114\n", + "weighted avg 0.95 0.95 0.95 114\n", + "\n" + ] + } + ], "source": [ "print(\"\\n=== CLASSIFICATION REPORTS (WITH SCALING) ===\")\n", "\n", @@ -422,10 +1417,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "444fbb13", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== CONFUSION MATRICES (WITH SCALING) ===\n", + "\n", + "KNN Confusion Matrix:\n", + "[[40 3]\n", + " [ 3 68]]\n", + "\n", + "Logistic Regression Confusion Matrix:\n", + "[[41 2]\n", + " [ 1 70]]\n", + "\n", + "Decision Tree Confusion Matrix:\n", + "[[40 3]\n", + " [ 3 68]]\n" + ] + } + ], "source": [ "print(\"\\n=== CONFUSION MATRICES (WITH SCALING) ===\")\n", "\n", @@ -454,10 +1470,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "17493cc3", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=== Cross-Validation Results ===\n", + "\n", + "KNN\n", + "CV Scores: [0.96491228 0.95614035 0.98245614 0.95614035 0.96460177]\n", + "Mean Accuracy: 0.9648501785437045\n", + "Standard Deviation: 0.009609970350036127\n", + "\n", + "Logistic Regression\n", + "CV Scores: [0.98245614 0.98245614 0.97368421 0.97368421 0.99115044]\n", + "Mean Accuracy: 0.9806862288464524\n", + "Standard Deviation: 0.006539441283506109\n", + "\n", + "Decision Tree\n", + "CV Scores: [0.9122807 0.90350877 0.92982456 0.95614035 0.88495575]\n", + "Mean Accuracy: 0.9173420276354604\n", + "Standard Deviation: 0.02419491828674519\n" + ] + } + ], "source": [ "from sklearn.model_selection import cross_val_score\n", "from sklearn.preprocessing import StandardScaler\n", @@ -510,10 +1549,66 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "c706abc3", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "=== Confusion Matrices ===\n", + "\n", + "KNN\n", + "[[198 14]\n", + " [ 6 351]]\n", + "\n", + "Logistic Regression\n", + "[[204 8]\n", + " [ 3 354]]\n", + "\n", + "Decision Tree\n", + "[[193 19]\n", + " [ 28 329]]\n", + "\n", + "=== Classification Reports ===\n", + "\n", + "KNN\n", + " precision recall f1-score support\n", + "\n", + " 0 0.97 0.93 0.95 212\n", + " 1 0.96 0.98 0.97 357\n", + "\n", + " accuracy 0.96 569\n", + " macro avg 0.97 0.96 0.96 569\n", + "weighted avg 0.96 0.96 0.96 569\n", + "\n", + "\n", + "Logistic Regression\n", + " precision recall f1-score support\n", + "\n", + " 0 0.99 0.96 0.97 212\n", + " 1 0.98 0.99 0.98 357\n", + "\n", + " accuracy 0.98 569\n", + " macro avg 0.98 0.98 0.98 569\n", + "weighted avg 0.98 0.98 0.98 569\n", + "\n", + "\n", + "Decision Tree\n", + " precision recall f1-score support\n", + "\n", + " 0 0.87 0.91 0.89 212\n", + " 1 0.95 0.92 0.93 357\n", + "\n", + " accuracy 0.92 569\n", + " macro avg 0.91 0.92 0.91 569\n", + "weighted avg 0.92 0.92 0.92 569\n", + "\n" + ] + } + ], "source": [ "from sklearn.model_selection import cross_val_predict\n", "from sklearn.metrics import confusion_matrix, classification_report\n", @@ -558,10 +1653,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "93fa45e5", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Best k: 13\n", + "Best CV Accuracy: 0.9332401800962584\n" + ] + } + ], "source": [ "from sklearn.model_selection import cross_val_score\n", "import numpy as np\n", @@ -587,6 +1691,23 @@ "encoding": "# -*- coding: utf-8 -*-", "main_language": "python", "notebook_metadata_filter": "-all" + }, + "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.14.3" } }, "nbformat": 4, diff --git a/HW 5/Iris_knn_W2025_template.ipynb b/HW 5/Iris_knn_W2025_template.ipynb index 44236f5..ab90b8c 100644 --- a/HW 5/Iris_knn_W2025_template.ipynb +++ b/HW 5/Iris_knn_W2025_template.ipynb @@ -2909,7 +2909,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.14.3" + "version": "3.14.4" } }, "nbformat": 4,