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. \n",
+ "
\n",
+ "
\n",
+ " Parameters \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " criterion\n",
+ " criterion: {\"gini\", \"entropy\", \"log_loss\"}, default=\"gini\" The function to measure the quality of a split. Supported criteria are \"gini\" for the Gini impurity and \"log_loss\" and \"entropy\" both for the Shannon information gain, see :ref:`tree_mathematical_formulation`. \n",
+ " \n",
+ " \n",
+ " 'gini' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " splitter\n",
+ " splitter: {\"best\", \"random\"}, default=\"best\" The strategy used to choose the split at each node. Supported strategies are \"best\" to choose the best split and \"random\" to choose the best random split. \n",
+ " \n",
+ " \n",
+ " 'best' \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_depth\n",
+ " max_depth: int, default=None The maximum depth of the tree. If None, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_split\n",
+ " min_samples_split: int or float, default=2 The minimum number of samples required to split an internal node: - If int, then consider `min_samples_split` as the minimum number. - If float, then `min_samples_split` is a fraction and `ceil(min_samples_split * n_samples)` are the minimum number of samples for each split. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 2 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_samples_leaf\n",
+ " min_samples_leaf: int or float, default=1 The minimum number of samples required to be at a leaf node. A split point at any depth will only be considered if it leaves at least ``min_samples_leaf`` training samples in each of the left and right branches. This may have the effect of smoothing the model, especially in regression. - If int, then consider `min_samples_leaf` as the minimum number. - If float, then `min_samples_leaf` is a fraction and `ceil(min_samples_leaf * n_samples)` are the minimum number of samples for each node. .. versionchanged:: 0.18 Added float values for fractions. \n",
+ " \n",
+ " \n",
+ " 1 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_weight_fraction_leaf\n",
+ " min_weight_fraction_leaf: float, default=0.0 The minimum weighted fraction of the sum total of weights (of all the input samples) required to be at a leaf node. Samples have equal weight when sample_weight is not provided. \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_features\n",
+ " max_features: int, float or {\"sqrt\", \"log2\"}, default=None The number of features to consider when looking for the best split: - If int, then consider `max_features` features at each split. - If float, then `max_features` is a fraction and `max(1, int(max_features * n_features_in_))` features are considered at each split. - If \"sqrt\", then `max_features=sqrt(n_features)`. - If \"log2\", then `max_features=log2(n_features)`. - If None, then `max_features=n_features`. .. note:: The search for a split does not stop until at least one valid partition of the node samples is found, even if it requires to effectively inspect more than ``max_features`` features. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " random_state\n",
+ " random_state: int, RandomState instance or None, default=None Controls the randomness of the estimator. The features are always randomly permuted at each split, even if ``splitter`` is set to ``\"best\"``. When ``max_features < n_features``, the algorithm will select ``max_features`` at random at each split before finding the best split among them. But the best found split may vary across different runs, even if ``max_features=n_features``. That is the case, if the improvement of the criterion is identical for several splits and one split has to be selected at random. To obtain a deterministic behaviour during fitting, ``random_state`` has to be fixed to an integer. See :term:`Glossary ` for details. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " max_leaf_nodes\n",
+ " max_leaf_nodes: int, default=None Grow a tree with ``max_leaf_nodes`` in best-first fashion. Best nodes are defined as relative reduction in impurity. If None then unlimited number of leaf nodes. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " min_impurity_decrease\n",
+ " min_impurity_decrease: float, default=0.0 A node will be split if this split induces a decrease of the impurity greater than or equal to this value. The weighted impurity decrease equation is the following:: N_t / N * (impurity - N_t_R / N_t * right_impurity - N_t_L / N_t * left_impurity) where ``N`` is the total number of samples, ``N_t`` is the number of samples at the current node, ``N_t_L`` is the number of samples in the left child, and ``N_t_R`` is the number of samples in the right child. ``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum, if ``sample_weight`` is passed. .. versionadded:: 0.19 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " class_weight\n",
+ " class_weight: dict, list of dict or \"balanced\", default=None Weights associated with classes in the form ``{class_label: weight}``. If None, all classes are supposed to have weight one. For multi-output problems, a list of dicts can be provided in the same order as the columns of y. Note that for multioutput (including multilabel) weights should be defined for each class of every column in its own dict. For example, for four-class multilabel classification weights should be [{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of [{1:1}, {2:5}, {3:1}, {4:1}]. The \"balanced\" mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))`` For multi-output, the weights of each column of y will be multiplied. Note that these weights will be multiplied with sample_weight (passed through the fit method) if sample_weight is specified. \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " ccp_alpha\n",
+ " ccp_alpha: non-negative float, default=0.0 Complexity parameter used for Minimal Cost-Complexity Pruning. The subtree with the largest cost complexity that is smaller than ``ccp_alpha`` will be chosen. By default, no pruning is performed. See :ref:`minimal_cost_complexity_pruning` for details. See :ref:`sphx_glr_auto_examples_tree_plot_cost_complexity_pruning.py` for an example of such pruning. .. versionadded:: 0.22 \n",
+ " \n",
+ " \n",
+ " 0.0 \n",
+ " \n",
+ " \n",
+ "\n",
+ " \n",
+ " \n",
+ " \n",
+ " \n",
+ " monotonic_cst\n",
+ " monotonic_cst: array-like of int of shape (n_features), default=None Indicates the monotonicity constraint to enforce on each feature. - 1: monotonic increase - 0: no constraint - -1: monotonic decrease If monotonic_cst is None, no constraints are applied. Monotonicity constraints are not supported for: - multiclass classifications (i.e. when `n_classes > 2`), - multioutput classifications (i.e. when `n_outputs_ > 1`), - classifications trained on data with missing values. The constraints hold over the probability of the positive class. Read more in the :ref:`User Guide `. .. versionadded:: 1.4 \n",
+ " \n",
+ " \n",
+ " None \n",
+ " \n",
+ " \n",
+ " \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
"
+ ],
+ "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,