1716 lines
67 KiB
Plaintext
1716 lines
67 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "dffa60e8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%matplotlib inline"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3340eabf",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"Inclass-Lab\n",
|
|
"\n",
|
|
"Automatically generated by Colab.\n",
|
|
"\n",
|
|
"Original file is located at\n",
|
|
" https://colab.research.google.com/drive/13N7lQHvv4_LxKgcerumJ-18e5GfdBggm"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "34271feb",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9491847c",
|
|
"metadata": {
|
|
"cell_marker": "\"\"\""
|
|
},
|
|
"source": [
|
|
"In-Class Lab: Comparing Classification Models (Breast Cancer Dataset)\n",
|
|
"\n",
|
|
"Learning Objectives\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"\n",
|
|
"By the end of this lab, you should be able to:\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"* Train multiple models (KNN, Logistic Regression, Decision Tree)\n",
|
|
"* Compare performance across various scenarios\n",
|
|
"* Understand the effect of scaling and evaluation methods\n",
|
|
"* Interpret confusion matrix & classification report\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "cceb43dc",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 1:"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "818bfdad",
|
|
"metadata": {
|
|
"cell_marker": "'''",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"Load dataset\n",
|
|
"Explain features/target\n",
|
|
"Do train_test_split"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6de94b5d",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "a8899ca0",
|
|
"metadata": {},
|
|
"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",
|
|
"X = data.data\n",
|
|
"y = data.target\n",
|
|
"\n",
|
|
"print(data.target_names)\n",
|
|
"print(X.shape)\n",
|
|
"#0 - malignant\n",
|
|
"#1 - benigh"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "ae1b0d61",
|
|
"metadata": {
|
|
"cell_marker": "###############################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 2: IMPORT LIBRARIES FOR CLASSIFIERS\n",
|
|
"##############################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"id": "8db6dfd6",
|
|
"metadata": {
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.neighbors import KNeighborsClassifier\n",
|
|
"from sklearn.linear_model import LogisticRegression\n",
|
|
"from sklearn.tree import DecisionTreeClassifier\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a2301b40",
|
|
"metadata": {
|
|
"cell_marker": "#########################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 3: IMPORT MODUL FOR DATA SPLIT\n",
|
|
"########################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"id": "75fe2532",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from sklearn.model_selection import train_test_split"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "fb69921d",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 4: SPLIT THE DATASET\n",
|
|
"###################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "6eb66fc0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "b182e930",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 5\n",
|
|
"TASK 1: Train 3 models (no scaling)\n",
|
|
"###################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"id": "f31f0262",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/html": [
|
|
"<style>#sk-container-id-3 {\n",
|
|
" /* Definition of color scheme common for light and dark mode */\n",
|
|
" --sklearn-color-text: #000;\n",
|
|
" --sklearn-color-text-muted: #666;\n",
|
|
" --sklearn-color-line: gray;\n",
|
|
" /* Definition of color scheme for unfitted estimators */\n",
|
|
" --sklearn-color-unfitted-level-0: #fff5e6;\n",
|
|
" --sklearn-color-unfitted-level-1: #f6e4d2;\n",
|
|
" --sklearn-color-unfitted-level-2: #ffe0b3;\n",
|
|
" --sklearn-color-unfitted-level-3: chocolate;\n",
|
|
" /* Definition of color scheme for fitted estimators */\n",
|
|
" --sklearn-color-fitted-level-0: #f0f8ff;\n",
|
|
" --sklearn-color-fitted-level-1: #d4ebff;\n",
|
|
" --sklearn-color-fitted-level-2: #b3dbfd;\n",
|
|
" --sklearn-color-fitted-level-3: cornflowerblue;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3.light {\n",
|
|
" /* Specific color for light theme */\n",
|
|
" --sklearn-color-text-on-default-background: black;\n",
|
|
" --sklearn-color-background: white;\n",
|
|
" --sklearn-color-border-box: black;\n",
|
|
" --sklearn-color-icon: #696969;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3.dark {\n",
|
|
" --sklearn-color-text-on-default-background: white;\n",
|
|
" --sklearn-color-background: #111;\n",
|
|
" --sklearn-color-border-box: white;\n",
|
|
" --sklearn-color-icon: #878787;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 {\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 pre {\n",
|
|
" padding: 0;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 input.sk-hidden--visually {\n",
|
|
" border: 0;\n",
|
|
" clip: rect(1px 1px 1px 1px);\n",
|
|
" clip: rect(1px, 1px, 1px, 1px);\n",
|
|
" height: 1px;\n",
|
|
" margin: -1px;\n",
|
|
" overflow: hidden;\n",
|
|
" padding: 0;\n",
|
|
" position: absolute;\n",
|
|
" width: 1px;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-dashed-wrapped {\n",
|
|
" border: 1px dashed var(--sklearn-color-line);\n",
|
|
" margin: 0 0.4em 0.5em 0.4em;\n",
|
|
" box-sizing: border-box;\n",
|
|
" padding-bottom: 0.4em;\n",
|
|
" background-color: var(--sklearn-color-background);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-container {\n",
|
|
" /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
|
|
" but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
|
|
" so we also need the `!important` here to be able to override the\n",
|
|
" default hidden behavior on the sphinx rendered scikit-learn.org.\n",
|
|
" See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
|
|
" display: inline-block !important;\n",
|
|
" position: relative;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-text-repr-fallback {\n",
|
|
" display: none;\n",
|
|
"}\n",
|
|
"\n",
|
|
"div.sk-parallel-item,\n",
|
|
"div.sk-serial,\n",
|
|
"div.sk-item {\n",
|
|
" /* draw centered vertical line to link estimators */\n",
|
|
" background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
|
|
" background-size: 2px 100%;\n",
|
|
" background-repeat: no-repeat;\n",
|
|
" background-position: center center;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Parallel-specific style estimator block */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel-item::after {\n",
|
|
" content: \"\";\n",
|
|
" width: 100%;\n",
|
|
" border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
|
|
" flex-grow: 1;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel {\n",
|
|
" display: flex;\n",
|
|
" align-items: stretch;\n",
|
|
" justify-content: center;\n",
|
|
" background-color: var(--sklearn-color-background);\n",
|
|
" position: relative;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel-item {\n",
|
|
" display: flex;\n",
|
|
" flex-direction: column;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel-item:first-child::after {\n",
|
|
" align-self: flex-end;\n",
|
|
" width: 50%;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel-item:last-child::after {\n",
|
|
" align-self: flex-start;\n",
|
|
" width: 50%;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-parallel-item:only-child::after {\n",
|
|
" width: 0;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Serial-specific style estimator block */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-serial {\n",
|
|
" display: flex;\n",
|
|
" flex-direction: column;\n",
|
|
" align-items: center;\n",
|
|
" background-color: var(--sklearn-color-background);\n",
|
|
" padding-right: 1em;\n",
|
|
" padding-left: 1em;\n",
|
|
"}\n",
|
|
"\n",
|
|
"\n",
|
|
"/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
|
|
"clickable and can be expanded/collapsed.\n",
|
|
"- Pipeline and ColumnTransformer use this feature and define the default style\n",
|
|
"- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
|
|
"*/\n",
|
|
"\n",
|
|
"/* Pipeline and ColumnTransformer style (default) */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-toggleable {\n",
|
|
" /* Default theme specific background. It is overwritten whether we have a\n",
|
|
" specific estimator or a Pipeline/ColumnTransformer */\n",
|
|
" background-color: var(--sklearn-color-background);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Toggleable label */\n",
|
|
"#sk-container-id-3 label.sk-toggleable__label {\n",
|
|
" cursor: pointer;\n",
|
|
" display: flex;\n",
|
|
" width: 100%;\n",
|
|
" margin-bottom: 0;\n",
|
|
" padding: 0.5em;\n",
|
|
" box-sizing: border-box;\n",
|
|
" text-align: center;\n",
|
|
" align-items: center;\n",
|
|
" justify-content: center;\n",
|
|
" gap: 0.5em;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 label.sk-toggleable__label .caption {\n",
|
|
" font-size: 0.6rem;\n",
|
|
" font-weight: lighter;\n",
|
|
" color: var(--sklearn-color-text-muted);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 label.sk-toggleable__label-arrow:before {\n",
|
|
" /* Arrow on the left of the label */\n",
|
|
" content: \"▸\";\n",
|
|
" float: left;\n",
|
|
" margin-right: 0.25em;\n",
|
|
" color: var(--sklearn-color-icon);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 label.sk-toggleable__label-arrow:hover:before {\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Toggleable content - dropdown */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-toggleable__content {\n",
|
|
" display: none;\n",
|
|
" text-align: left;\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-toggleable__content.fitted {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-toggleable__content pre {\n",
|
|
" margin: 0.2em;\n",
|
|
" border-radius: 0.25em;\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-toggleable__content.fitted pre {\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
|
|
" /* Expand drop-down */\n",
|
|
" display: block;\n",
|
|
" width: 100%;\n",
|
|
" overflow: visible;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
|
|
" content: \"▾\";\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Pipeline/ColumnTransformer-specific style */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
|
|
" background-color: var(--sklearn-color-fitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Estimator-specific style */\n",
|
|
"\n",
|
|
"/* Colorize estimator box */\n",
|
|
"#sk-container-id-3 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-label label.sk-toggleable__label,\n",
|
|
"#sk-container-id-3 div.sk-label label {\n",
|
|
" /* The background is the default theme color */\n",
|
|
" color: var(--sklearn-color-text-on-default-background);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* On hover, darken the color of the background */\n",
|
|
"#sk-container-id-3 div.sk-label:hover label.sk-toggleable__label {\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Label box, darken color on hover, fitted */\n",
|
|
"#sk-container-id-3 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" background-color: var(--sklearn-color-fitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Estimator label */\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-label label {\n",
|
|
" font-family: monospace;\n",
|
|
" font-weight: bold;\n",
|
|
" line-height: 1.2em;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-label-container {\n",
|
|
" text-align: center;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Estimator-specific */\n",
|
|
"#sk-container-id-3 div.sk-estimator {\n",
|
|
" font-family: monospace;\n",
|
|
" border: 1px dotted var(--sklearn-color-border-box);\n",
|
|
" border-radius: 0.25em;\n",
|
|
" box-sizing: border-box;\n",
|
|
" margin-bottom: 0.5em;\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-estimator.fitted {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-0);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* on hover */\n",
|
|
"#sk-container-id-3 div.sk-estimator:hover {\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 div.sk-estimator.fitted:hover {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-2);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
|
|
"\n",
|
|
"/* Common style for \"i\" and \"?\" */\n",
|
|
"\n",
|
|
".sk-estimator-doc-link,\n",
|
|
"a:link.sk-estimator-doc-link,\n",
|
|
"a:visited.sk-estimator-doc-link {\n",
|
|
" float: right;\n",
|
|
" font-size: smaller;\n",
|
|
" line-height: 1em;\n",
|
|
" font-family: monospace;\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-0);\n",
|
|
" border-radius: 1em;\n",
|
|
" height: 1em;\n",
|
|
" width: 1em;\n",
|
|
" text-decoration: none !important;\n",
|
|
" margin-left: 0.5em;\n",
|
|
" text-align: center;\n",
|
|
" /* unfitted */\n",
|
|
" border: var(--sklearn-color-unfitted-level-3) 1pt solid;\n",
|
|
" color: var(--sklearn-color-unfitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
".sk-estimator-doc-link.fitted,\n",
|
|
"a:link.sk-estimator-doc-link.fitted,\n",
|
|
"a:visited.sk-estimator-doc-link.fitted {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-0);\n",
|
|
" border: var(--sklearn-color-fitted-level-3) 1pt solid;\n",
|
|
" color: var(--sklearn-color-fitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* On hover */\n",
|
|
"div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
|
|
".sk-estimator-doc-link:hover,\n",
|
|
"div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
|
|
".sk-estimator-doc-link:hover {\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-3);\n",
|
|
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
|
|
" color: var(--sklearn-color-unfitted-level-0);\n",
|
|
" text-decoration: none;\n",
|
|
"}\n",
|
|
"\n",
|
|
"div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
|
|
".sk-estimator-doc-link.fitted:hover,\n",
|
|
"div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
|
|
".sk-estimator-doc-link.fitted:hover {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-3);\n",
|
|
" border: var(--sklearn-color-fitted-level-0) 1pt solid;\n",
|
|
" color: var(--sklearn-color-fitted-level-0);\n",
|
|
" text-decoration: none;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Span, style for the box shown on hovering the info icon */\n",
|
|
".sk-estimator-doc-link span {\n",
|
|
" display: none;\n",
|
|
" z-index: 9999;\n",
|
|
" position: relative;\n",
|
|
" font-weight: normal;\n",
|
|
" right: .2ex;\n",
|
|
" padding: .5ex;\n",
|
|
" margin: .5ex;\n",
|
|
" width: min-content;\n",
|
|
" min-width: 20ex;\n",
|
|
" max-width: 50ex;\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" box-shadow: 2pt 2pt 4pt #999;\n",
|
|
" /* unfitted */\n",
|
|
" background: var(--sklearn-color-unfitted-level-0);\n",
|
|
" border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
".sk-estimator-doc-link.fitted span {\n",
|
|
" /* fitted */\n",
|
|
" background: var(--sklearn-color-fitted-level-0);\n",
|
|
" border: var(--sklearn-color-fitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
".sk-estimator-doc-link:hover span {\n",
|
|
" display: block;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* \"?\"-specific style due to the `<a>` HTML tag */\n",
|
|
"\n",
|
|
"#sk-container-id-3 a.estimator_doc_link {\n",
|
|
" float: right;\n",
|
|
" font-size: 1rem;\n",
|
|
" line-height: 1em;\n",
|
|
" font-family: monospace;\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-0);\n",
|
|
" border-radius: 1rem;\n",
|
|
" height: 1rem;\n",
|
|
" width: 1rem;\n",
|
|
" text-decoration: none;\n",
|
|
" /* unfitted */\n",
|
|
" color: var(--sklearn-color-unfitted-level-1);\n",
|
|
" border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 a.estimator_doc_link.fitted {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-0);\n",
|
|
" border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
|
|
" color: var(--sklearn-color-fitted-level-1);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* On hover */\n",
|
|
"#sk-container-id-3 a.estimator_doc_link:hover {\n",
|
|
" /* unfitted */\n",
|
|
" background-color: var(--sklearn-color-unfitted-level-3);\n",
|
|
" color: var(--sklearn-color-background);\n",
|
|
" text-decoration: none;\n",
|
|
"}\n",
|
|
"\n",
|
|
"#sk-container-id-3 a.estimator_doc_link.fitted:hover {\n",
|
|
" /* fitted */\n",
|
|
" background-color: var(--sklearn-color-fitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table {\n",
|
|
" font-family: monospace;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table summary {\n",
|
|
" padding: .5rem;\n",
|
|
" cursor: pointer;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table summary::marker {\n",
|
|
" font-size: 0.7rem;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table details[open] {\n",
|
|
" padding-left: 0.1rem;\n",
|
|
" padding-right: 0.1rem;\n",
|
|
" padding-bottom: 0.3rem;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table .parameters-table {\n",
|
|
" margin-left: auto !important;\n",
|
|
" margin-right: auto !important;\n",
|
|
" margin-top: 0;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table .parameters-table tr:nth-child(odd) {\n",
|
|
" background-color: #fff;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table .parameters-table tr:nth-child(even) {\n",
|
|
" background-color: #f6f6f6;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table .parameters-table tr:hover {\n",
|
|
" background-color: #e0e0e0;\n",
|
|
"}\n",
|
|
"\n",
|
|
".estimator-table table td {\n",
|
|
" border: 1px solid rgba(106, 105, 104, 0.232);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/*\n",
|
|
" `table td`is set in notebook with right text-align.\n",
|
|
" We need to overwrite it.\n",
|
|
"*/\n",
|
|
".estimator-table table td.param {\n",
|
|
" text-align: left;\n",
|
|
" position: relative;\n",
|
|
" padding: 0;\n",
|
|
"}\n",
|
|
"\n",
|
|
".user-set td {\n",
|
|
" color:rgb(255, 94, 0);\n",
|
|
" text-align: left !important;\n",
|
|
"}\n",
|
|
"\n",
|
|
".user-set td.value {\n",
|
|
" color:rgb(255, 94, 0);\n",
|
|
" background-color: transparent;\n",
|
|
"}\n",
|
|
"\n",
|
|
".default td {\n",
|
|
" color: black;\n",
|
|
" text-align: left !important;\n",
|
|
"}\n",
|
|
"\n",
|
|
".user-set td i,\n",
|
|
".default td i {\n",
|
|
" color: black;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/*\n",
|
|
" Styles for parameter documentation links\n",
|
|
" We need styling for visited so jupyter doesn't overwrite it\n",
|
|
"*/\n",
|
|
"a.param-doc-link,\n",
|
|
"a.param-doc-link:link,\n",
|
|
"a.param-doc-link:visited {\n",
|
|
" text-decoration: underline dashed;\n",
|
|
" text-underline-offset: .3em;\n",
|
|
" color: inherit;\n",
|
|
" display: block;\n",
|
|
" padding: .5em;\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* \"hack\" to make the entire area of the cell containing the link clickable */\n",
|
|
"a.param-doc-link::before {\n",
|
|
" position: absolute;\n",
|
|
" content: \"\";\n",
|
|
" inset: 0;\n",
|
|
"}\n",
|
|
"\n",
|
|
".param-doc-description {\n",
|
|
" display: none;\n",
|
|
" position: absolute;\n",
|
|
" z-index: 9999;\n",
|
|
" left: 0;\n",
|
|
" padding: .5ex;\n",
|
|
" margin-left: 1.5em;\n",
|
|
" color: var(--sklearn-color-text);\n",
|
|
" box-shadow: .3em .3em .4em #999;\n",
|
|
" width: max-content;\n",
|
|
" text-align: left;\n",
|
|
" max-height: 10em;\n",
|
|
" overflow-y: auto;\n",
|
|
"\n",
|
|
" /* unfitted */\n",
|
|
" background: var(--sklearn-color-unfitted-level-0);\n",
|
|
" border: thin solid var(--sklearn-color-unfitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
"/* Fitted state for parameter tooltips */\n",
|
|
".fitted .param-doc-description {\n",
|
|
" /* fitted */\n",
|
|
" background: var(--sklearn-color-fitted-level-0);\n",
|
|
" border: thin solid var(--sklearn-color-fitted-level-3);\n",
|
|
"}\n",
|
|
"\n",
|
|
".param-doc-link:hover .param-doc-description {\n",
|
|
" display: block;\n",
|
|
"}\n",
|
|
"\n",
|
|
".copy-paste-icon {\n",
|
|
" background-image: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0NDggNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjUgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIwOCAwTDMzMi4xIDBjMTIuNyAwIDI0LjkgNS4xIDMzLjkgMTQuMWw2Ny45IDY3LjljOSA5IDE0LjEgMjEuMiAxNC4xIDMzLjlMNDQ4IDMzNmMwIDI2LjUtMjEuNSA0OC00OCA0OGwtMTkyIDBjLTI2LjUgMC00OC0yMS41LTQ4LTQ4bDAtMjg4YzAtMjYuNSAyMS41LTQ4IDQ4LTQ4ek00OCAxMjhsODAgMCAwIDY0LTY0IDAgMCAyNTYgMTkyIDAgMC0zMiA2NCAwIDAgNDhjMCAyNi41LTIxLjUgNDgtNDggNDhMNDggNTEyYy0yNi41IDAtNDgtMjEuNS00OC00OEwwIDE3NmMwLTI2LjUgMjEuNS00OCA0OC00OHoiLz48L3N2Zz4=);\n",
|
|
" background-repeat: no-repeat;\n",
|
|
" background-size: 14px 14px;\n",
|
|
" background-position: 0;\n",
|
|
" display: inline-block;\n",
|
|
" width: 14px;\n",
|
|
" height: 14px;\n",
|
|
" cursor: pointer;\n",
|
|
"}\n",
|
|
"</style><body><div id=\"sk-container-id-3\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>DecisionTreeClassifier()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-3\" type=\"checkbox\" checked><label for=\"sk-estimator-id-3\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>DecisionTreeClassifier</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html\">?<span>Documentation for DecisionTreeClassifier</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\" data-param-prefix=\"\">\n",
|
|
" <div class=\"estimator-table\">\n",
|
|
" <details>\n",
|
|
" <summary>Parameters</summary>\n",
|
|
" <table class=\"parameters-table\">\n",
|
|
" <tbody>\n",
|
|
" \n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('criterion',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=criterion,-%7B%22gini%22%2C%20%22entropy%22%2C%20%22log_loss%22%7D%2C%20default%3D%22gini%22\">\n",
|
|
" criterion\n",
|
|
" <span class=\"param-doc-description\">criterion: {\"gini\", \"entropy\", \"log_loss\"}, default=\"gini\"<br><br>The function to measure the quality of a split. Supported criteria are<br>\"gini\" for the Gini impurity and \"log_loss\" and \"entropy\" both for the<br>Shannon information gain, see :ref:`tree_mathematical_formulation`.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">'gini'</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('splitter',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=splitter,-%7B%22best%22%2C%20%22random%22%7D%2C%20default%3D%22best%22\">\n",
|
|
" splitter\n",
|
|
" <span class=\"param-doc-description\">splitter: {\"best\", \"random\"}, default=\"best\"<br><br>The strategy used to choose the split at each node. Supported<br>strategies are \"best\" to choose the best split and \"random\" to choose<br>the best random split.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">'best'</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('max_depth',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=max_depth,-int%2C%20default%3DNone\">\n",
|
|
" max_depth\n",
|
|
" <span class=\"param-doc-description\">max_depth: int, default=None<br><br>The maximum depth of the tree. If None, then nodes are expanded until<br>all leaves are pure or until all leaves contain less than<br>min_samples_split samples.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('min_samples_split',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=min_samples_split,-int%20or%20float%2C%20default%3D2\">\n",
|
|
" min_samples_split\n",
|
|
" <span class=\"param-doc-description\">min_samples_split: int or float, default=2<br><br>The minimum number of samples required to split an internal node:<br><br>- If int, then consider `min_samples_split` as the minimum number.<br>- If float, then `min_samples_split` is a fraction and<br> `ceil(min_samples_split * n_samples)` are the minimum<br> number of samples for each split.<br><br>.. versionchanged:: 0.18<br> Added float values for fractions.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">2</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('min_samples_leaf',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=min_samples_leaf,-int%20or%20float%2C%20default%3D1\">\n",
|
|
" min_samples_leaf\n",
|
|
" <span class=\"param-doc-description\">min_samples_leaf: int or float, default=1<br><br>The minimum number of samples required to be at a leaf node.<br>A split point at any depth will only be considered if it leaves at<br>least ``min_samples_leaf`` training samples in each of the left and<br>right branches. This may have the effect of smoothing the model,<br>especially in regression.<br><br>- If int, then consider `min_samples_leaf` as the minimum number.<br>- If float, then `min_samples_leaf` is a fraction and<br> `ceil(min_samples_leaf * n_samples)` are the minimum<br> number of samples for each node.<br><br>.. versionchanged:: 0.18<br> Added float values for fractions.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">1</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('min_weight_fraction_leaf',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=min_weight_fraction_leaf,-float%2C%20default%3D0.0\">\n",
|
|
" min_weight_fraction_leaf\n",
|
|
" <span class=\"param-doc-description\">min_weight_fraction_leaf: float, default=0.0<br><br>The minimum weighted fraction of the sum total of weights (of all<br>the input samples) required to be at a leaf node. Samples have<br>equal weight when sample_weight is not provided.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">0.0</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('max_features',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=max_features,-int%2C%20float%20or%20%7B%22sqrt%22%2C%20%22log2%22%7D%2C%20default%3DNone\">\n",
|
|
" max_features\n",
|
|
" <span class=\"param-doc-description\">max_features: int, float or {\"sqrt\", \"log2\"}, default=None<br><br>The number of features to consider when looking for the best split:<br><br>- If int, then consider `max_features` features at each split.<br>- If float, then `max_features` is a fraction and<br> `max(1, int(max_features * n_features_in_))` features are considered at<br> each split.<br>- If \"sqrt\", then `max_features=sqrt(n_features)`.<br>- If \"log2\", then `max_features=log2(n_features)`.<br>- If None, then `max_features=n_features`.<br><br>.. note::<br><br> The search for a split does not stop until at least one<br> valid partition of the node samples is found, even if it requires to<br> effectively inspect more than ``max_features`` features.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('random_state',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=random_state,-int%2C%20RandomState%20instance%20or%20None%2C%20default%3DNone\">\n",
|
|
" random_state\n",
|
|
" <span class=\"param-doc-description\">random_state: int, RandomState instance or None, default=None<br><br>Controls the randomness of the estimator. The features are always<br>randomly permuted at each split, even if ``splitter`` is set to<br>``\"best\"``. When ``max_features < n_features``, the algorithm will<br>select ``max_features`` at random at each split before finding the best<br>split among them. But the best found split may vary across different<br>runs, even if ``max_features=n_features``. That is the case, if the<br>improvement of the criterion is identical for several splits and one<br>split has to be selected at random. To obtain a deterministic behaviour<br>during fitting, ``random_state`` has to be fixed to an integer.<br>See :term:`Glossary <random_state>` for details.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('max_leaf_nodes',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=max_leaf_nodes,-int%2C%20default%3DNone\">\n",
|
|
" max_leaf_nodes\n",
|
|
" <span class=\"param-doc-description\">max_leaf_nodes: int, default=None<br><br>Grow a tree with ``max_leaf_nodes`` in best-first fashion.<br>Best nodes are defined as relative reduction in impurity.<br>If None then unlimited number of leaf nodes.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('min_impurity_decrease',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=min_impurity_decrease,-float%2C%20default%3D0.0\">\n",
|
|
" min_impurity_decrease\n",
|
|
" <span class=\"param-doc-description\">min_impurity_decrease: float, default=0.0<br><br>A node will be split if this split induces a decrease of the impurity<br>greater than or equal to this value.<br><br>The weighted impurity decrease equation is the following::<br><br> N_t / N * (impurity - N_t_R / N_t * right_impurity<br> - N_t_L / N_t * left_impurity)<br><br>where ``N`` is the total number of samples, ``N_t`` is the number of<br>samples at the current node, ``N_t_L`` is the number of samples in the<br>left child, and ``N_t_R`` is the number of samples in the right child.<br><br>``N``, ``N_t``, ``N_t_R`` and ``N_t_L`` all refer to the weighted sum,<br>if ``sample_weight`` is passed.<br><br>.. versionadded:: 0.19</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">0.0</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('class_weight',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=class_weight,-dict%2C%20list%20of%20dict%20or%20%22balanced%22%2C%20default%3DNone\">\n",
|
|
" class_weight\n",
|
|
" <span class=\"param-doc-description\">class_weight: dict, list of dict or \"balanced\", default=None<br><br>Weights associated with classes in the form ``{class_label: weight}``.<br>If None, all classes are supposed to have weight one. For<br>multi-output problems, a list of dicts can be provided in the same<br>order as the columns of y.<br><br>Note that for multioutput (including multilabel) weights should be<br>defined for each class of every column in its own dict. For example,<br>for four-class multilabel classification weights should be<br>[{0: 1, 1: 1}, {0: 1, 1: 5}, {0: 1, 1: 1}, {0: 1, 1: 1}] instead of<br>[{1:1}, {2:5}, {3:1}, {4:1}].<br><br>The \"balanced\" mode uses the values of y to automatically adjust<br>weights inversely proportional to class frequencies in the input data<br>as ``n_samples / (n_classes * np.bincount(y))``<br><br>For multi-output, the weights of each column of y will be multiplied.<br><br>Note that these weights will be multiplied with sample_weight (passed<br>through the fit method) if sample_weight is specified.</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('ccp_alpha',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=ccp_alpha,-non-negative%20float%2C%20default%3D0.0\">\n",
|
|
" ccp_alpha\n",
|
|
" <span class=\"param-doc-description\">ccp_alpha: non-negative float, default=0.0<br><br>Complexity parameter used for Minimal Cost-Complexity Pruning. The<br>subtree with the largest cost complexity that is smaller than<br>``ccp_alpha`` will be chosen. By default, no pruning is performed. See<br>:ref:`minimal_cost_complexity_pruning` for details. See<br>:ref:`sphx_glr_auto_examples_tree_plot_cost_complexity_pruning.py`<br>for an example of such pruning.<br><br>.. versionadded:: 0.22</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">0.0</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
"\n",
|
|
" <tr class=\"default\">\n",
|
|
" <td><i class=\"copy-paste-icon\"\n",
|
|
" onclick=\"copyToClipboard('monotonic_cst',\n",
|
|
" this.parentElement.nextElementSibling)\"\n",
|
|
" ></i></td>\n",
|
|
" <td class=\"param\">\n",
|
|
" <a class=\"param-doc-link\"\n",
|
|
" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.8/modules/generated/sklearn.tree.DecisionTreeClassifier.html#:~:text=monotonic_cst,-array-like%20of%20int%20of%20shape%20%28n_features%29%2C%20default%3DNone\">\n",
|
|
" monotonic_cst\n",
|
|
" <span class=\"param-doc-description\">monotonic_cst: array-like of int of shape (n_features), default=None<br><br>Indicates the monotonicity constraint to enforce on each feature.<br> - 1: monotonic increase<br> - 0: no constraint<br> - -1: monotonic decrease<br><br>If monotonic_cst is None, no constraints are applied.<br><br>Monotonicity constraints are not supported for:<br> - multiclass classifications (i.e. when `n_classes > 2`),<br> - multioutput classifications (i.e. when `n_outputs_ > 1`),<br> - classifications trained on data with missing values.<br><br>The constraints hold over the probability of the positive class.<br><br>Read more in the :ref:`User Guide <monotonic_cst_gbdt>`.<br><br>.. versionadded:: 1.4</span>\n",
|
|
" </a>\n",
|
|
" </td>\n",
|
|
" <td class=\"value\">None</td>\n",
|
|
" </tr>\n",
|
|
" \n",
|
|
" </tbody>\n",
|
|
" </table>\n",
|
|
" </details>\n",
|
|
" </div>\n",
|
|
" </div></div></div></div></div><script>function copyToClipboard(text, element) {\n",
|
|
" // Get the parameter prefix from the closest toggleable content\n",
|
|
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
|
|
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
|
|
" const fullParamName = paramPrefix ? `${paramPrefix}${text}` : text;\n",
|
|
"\n",
|
|
" const originalStyle = element.style;\n",
|
|
" const computedStyle = window.getComputedStyle(element);\n",
|
|
" const originalWidth = computedStyle.width;\n",
|
|
" const originalHTML = element.innerHTML.replace('Copied!', '');\n",
|
|
"\n",
|
|
" navigator.clipboard.writeText(fullParamName)\n",
|
|
" .then(() => {\n",
|
|
" element.style.width = originalWidth;\n",
|
|
" element.style.color = 'green';\n",
|
|
" element.innerHTML = \"Copied!\";\n",
|
|
"\n",
|
|
" setTimeout(() => {\n",
|
|
" element.innerHTML = originalHTML;\n",
|
|
" element.style = originalStyle;\n",
|
|
" }, 2000);\n",
|
|
" })\n",
|
|
" .catch(err => {\n",
|
|
" console.error('Failed to copy:', err);\n",
|
|
" element.style.color = 'red';\n",
|
|
" element.innerHTML = \"Failed!\";\n",
|
|
" setTimeout(() => {\n",
|
|
" element.innerHTML = originalHTML;\n",
|
|
" element.style = originalStyle;\n",
|
|
" }, 2000);\n",
|
|
" });\n",
|
|
" return false;\n",
|
|
"}\n",
|
|
"\n",
|
|
"document.querySelectorAll('.copy-paste-icon').forEach(function(element) {\n",
|
|
" const toggleableContent = element.closest('.sk-toggleable__content');\n",
|
|
" const paramPrefix = toggleableContent ? toggleableContent.dataset.paramPrefix : '';\n",
|
|
" const paramName = element.parentElement.nextElementSibling\n",
|
|
" .textContent.trim().split(' ')[0];\n",
|
|
" const fullParamName = paramPrefix ? `${paramPrefix}${paramName}` : paramName;\n",
|
|
"\n",
|
|
" element.setAttribute('title', fullParamName);\n",
|
|
"});\n",
|
|
"\n",
|
|
"\n",
|
|
"/**\n",
|
|
" * Adapted from Skrub\n",
|
|
" * https://github.com/skrub-data/skrub/blob/403466d1d5d4dc76a7ef569b3f8228db59a31dc3/skrub/_reporting/_data/templates/report.js#L789\n",
|
|
" * @returns \"light\" or \"dark\"\n",
|
|
" */\n",
|
|
"function detectTheme(element) {\n",
|
|
" const body = document.querySelector('body');\n",
|
|
"\n",
|
|
" // Check VSCode theme\n",
|
|
" const themeKindAttr = body.getAttribute('data-vscode-theme-kind');\n",
|
|
" const themeNameAttr = body.getAttribute('data-vscode-theme-name');\n",
|
|
"\n",
|
|
" if (themeKindAttr && themeNameAttr) {\n",
|
|
" const themeKind = themeKindAttr.toLowerCase();\n",
|
|
" const themeName = themeNameAttr.toLowerCase();\n",
|
|
"\n",
|
|
" if (themeKind.includes(\"dark\") || themeName.includes(\"dark\")) {\n",
|
|
" return \"dark\";\n",
|
|
" }\n",
|
|
" if (themeKind.includes(\"light\") || themeName.includes(\"light\")) {\n",
|
|
" return \"light\";\n",
|
|
" }\n",
|
|
" }\n",
|
|
"\n",
|
|
" // Check Jupyter theme\n",
|
|
" if (body.getAttribute('data-jp-theme-light') === 'false') {\n",
|
|
" return 'dark';\n",
|
|
" } else if (body.getAttribute('data-jp-theme-light') === 'true') {\n",
|
|
" return 'light';\n",
|
|
" }\n",
|
|
"\n",
|
|
" // Guess based on a parent element's color\n",
|
|
" const color = window.getComputedStyle(element.parentNode, null).getPropertyValue('color');\n",
|
|
" const match = color.match(/^rgb\\s*\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)\\s*$/i);\n",
|
|
" if (match) {\n",
|
|
" const [r, g, b] = [\n",
|
|
" parseFloat(match[1]),\n",
|
|
" parseFloat(match[2]),\n",
|
|
" parseFloat(match[3])\n",
|
|
" ];\n",
|
|
"\n",
|
|
" // https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness\n",
|
|
" const luma = 0.299 * r + 0.587 * g + 0.114 * b;\n",
|
|
"\n",
|
|
" if (luma > 180) {\n",
|
|
" // If the text is very bright we have a dark theme\n",
|
|
" return 'dark';\n",
|
|
" }\n",
|
|
" if (luma < 75) {\n",
|
|
" // If the text is very dark we have a light theme\n",
|
|
" return 'light';\n",
|
|
" }\n",
|
|
" // Otherwise fall back to the next heuristic.\n",
|
|
" }\n",
|
|
"\n",
|
|
" // Fallback to system preference\n",
|
|
" return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\n",
|
|
"}\n",
|
|
"\n",
|
|
"\n",
|
|
"function forceTheme(elementId) {\n",
|
|
" const estimatorElement = document.querySelector(`#${elementId}`);\n",
|
|
" if (estimatorElement === null) {\n",
|
|
" console.error(`Element with id ${elementId} not found.`);\n",
|
|
" } else {\n",
|
|
" const theme = detectTheme(estimatorElement);\n",
|
|
" estimatorElement.classList.add(theme);\n",
|
|
" }\n",
|
|
"}\n",
|
|
"\n",
|
|
"forceTheme('sk-container-id-3');</script></body>"
|
|
],
|
|
"text/plain": [
|
|
"DecisionTreeClassifier()"
|
|
]
|
|
},
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"knn = KNeighborsClassifier(n_neighbors=5)\n",
|
|
"logreg = LogisticRegression(max_iter=5000)\n",
|
|
"tree = DecisionTreeClassifier()\n",
|
|
"\n",
|
|
"knn.fit(X_train, y_train)\n",
|
|
"logreg.fit(X_train, y_train)\n",
|
|
"tree.fit(X_train, y_train)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d3e0088e",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 6\n",
|
|
"Task 2: Evaluate accuracy"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "3a8845a1",
|
|
"metadata": {
|
|
"cell_marker": "'''",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"* Which model performs best?\n",
|
|
"* Are the results very different?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d49b62b3",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "395d887c",
|
|
"metadata": {},
|
|
"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",
|
|
"print(\"Tree:\", tree.score(X_test, y_test))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "9de74c34",
|
|
"metadata": {
|
|
"cell_marker": "####################################"
|
|
},
|
|
"source": [
|
|
"BLOCK 7\n",
|
|
"Part 2: Evaluation Metrics\n",
|
|
"Task 3: Confusion Matrix"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"id": "deec64c6",
|
|
"metadata": {},
|
|
"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)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "18bc0c0e",
|
|
"metadata": {
|
|
"cell_marker": "#####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 8: CLASSIFICATION FREPORT"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "a2e8747f",
|
|
"metadata": {
|
|
"cell_marker": "'''",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"* Which model has better recall?\n",
|
|
"* Which is better for detecting cancer?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "88303af7",
|
|
"metadata": {
|
|
"cell_marker": "#####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"id": "e9ef7bea",
|
|
"metadata": {},
|
|
"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)))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c1d8978a",
|
|
"metadata": {
|
|
"cell_marker": "########################################"
|
|
},
|
|
"source": [
|
|
"BLOCK 9:\n",
|
|
"Part 3: Scaling Effect\n",
|
|
"Task 5: Apply scaling using pipeline\n",
|
|
"#######################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"id": "84e1f5d4",
|
|
"metadata": {},
|
|
"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",
|
|
"from sklearn.metrics import accuracy_score, confusion_matrix, classification_report\n",
|
|
"\n",
|
|
"knn_scaled = Pipeline([\n",
|
|
" ('scaler', StandardScaler()),\n",
|
|
" ('knn', KNeighborsClassifier(n_neighbors=5))\n",
|
|
"])\n",
|
|
"\n",
|
|
"logreg_scaled = Pipeline([\n",
|
|
" ('scaler', StandardScaler()),\n",
|
|
" ('logreg', LogisticRegression(max_iter=5000))\n",
|
|
"])\n",
|
|
"\n",
|
|
"tree_scaled = Pipeline([\n",
|
|
" ('scaler', StandardScaler()),\n",
|
|
" ('tree', DecisionTreeClassifier(random_state=42))\n",
|
|
"])\n",
|
|
"\n",
|
|
"knn_scaled.fit(X_train, y_train)\n",
|
|
"logreg_scaled.fit(X_train, y_train)\n",
|
|
"tree_scaled.fit(X_train, y_train)\n",
|
|
"\n",
|
|
"knn_pred_scaled = knn_scaled.predict(X_test)\n",
|
|
"logreg_pred_scaled = logreg_scaled.predict(X_test)\n",
|
|
"tree_pred_scaled = tree_scaled.predict(X_test)\n",
|
|
"\n",
|
|
"print(\"\\n=== RESULTS WITH SCALING ===\")\n",
|
|
"print(\"KNN Accuracy:\", accuracy_score(y_test, knn_pred_scaled))\n",
|
|
"print(\"Logistic Regression Accuracy:\", accuracy_score(y_test, logreg_pred_scaled))\n",
|
|
"print(\"Decision Tree Accuracy:\", accuracy_score(y_test, tree_pred_scaled))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d5caa8ec",
|
|
"metadata": {
|
|
"cell_marker": "#####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 10: CLASSIFICATION REPORT\n",
|
|
"####################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"id": "6643f0af",
|
|
"metadata": {},
|
|
"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",
|
|
"print(\"\\nKNN Report:\")\n",
|
|
"print(classification_report(y_test, knn_pred_scaled))\n",
|
|
"\n",
|
|
"print(\"\\nLogistic Regression Report:\")\n",
|
|
"print(classification_report(y_test, logreg_pred_scaled))\n",
|
|
"\n",
|
|
"print(\"\\nDecision Tree Report:\")\n",
|
|
"print(classification_report(y_test, tree_pred_scaled))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c38c51d0",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 11: CONFUSION MATRIX\n",
|
|
"###################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"id": "444fbb13",
|
|
"metadata": {},
|
|
"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",
|
|
"print(\"\\nKNN Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(y_test, knn_pred_scaled))\n",
|
|
"\n",
|
|
"print(\"\\nLogistic Regression Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(y_test, logreg_pred_scaled))\n",
|
|
"\n",
|
|
"print(\"\\nDecision Tree Confusion Matrix:\")\n",
|
|
"print(confusion_matrix(y_test, tree_pred_scaled))\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c5d82a08",
|
|
"metadata": {
|
|
"cell_marker": "###############################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 12: MODWLS WITH CROSS-VALIDATION\n",
|
|
"##############################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"id": "17493cc3",
|
|
"metadata": {},
|
|
"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",
|
|
"\n",
|
|
"knn_pipeline = Pipeline([\n",
|
|
" ('scaler', StandardScaler()),\n",
|
|
" ('knn', KNeighborsClassifier(n_neighbors=5))\n",
|
|
"])\n",
|
|
"\n",
|
|
"logreg_pipeline = Pipeline([\n",
|
|
" ('scaler', StandardScaler()),\n",
|
|
" ('logreg', LogisticRegression(max_iter=5000))\n",
|
|
"])\n",
|
|
"\n",
|
|
"tree_model = DecisionTreeClassifier(random_state=42)\n",
|
|
"\n",
|
|
"knn_cv_scores = cross_val_score(knn_pipeline, X, y, cv=5, scoring='accuracy')\n",
|
|
"logreg_cv_scores = cross_val_score(logreg_pipeline, X, y, cv=5, scoring='accuracy')\n",
|
|
"tree_cv_scores = cross_val_score(tree_model, X, y, cv=5, scoring='accuracy')\n",
|
|
"\n",
|
|
"print(\"=== Cross-Validation Results ===\")\n",
|
|
"\n",
|
|
"print(\"\\nKNN\")\n",
|
|
"print(\"CV Scores:\", knn_cv_scores)\n",
|
|
"print(\"Mean Accuracy:\", np.mean(knn_cv_scores))\n",
|
|
"print(\"Standard Deviation:\", np.std(knn_cv_scores))\n",
|
|
"\n",
|
|
"print(\"\\nLogistic Regression\")\n",
|
|
"print(\"CV Scores:\", logreg_cv_scores)\n",
|
|
"print(\"Mean Accuracy:\", np.mean(logreg_cv_scores))\n",
|
|
"print(\"Standard Deviation:\", np.std(logreg_cv_scores))\n",
|
|
"\n",
|
|
"print(\"\\nDecision Tree\")\n",
|
|
"print(\"CV Scores:\", tree_cv_scores)\n",
|
|
"print(\"Mean Accuracy:\", np.mean(tree_cv_scores))\n",
|
|
"print(\"Standard Deviation:\", np.std(tree_cv_scores))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "94d94dd2",
|
|
"metadata": {
|
|
"cell_marker": "####################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 13: CONFUSION MATRIX\n",
|
|
"###################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"id": "c706abc3",
|
|
"metadata": {},
|
|
"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",
|
|
"\n",
|
|
"knn_pred = cross_val_predict(knn_pipeline, X, y, cv=5)\n",
|
|
"logreg_pred = cross_val_predict(logreg_pipeline, X, y, cv=5)\n",
|
|
"tree_pred = cross_val_predict(tree_model, X, y, cv=5)\n",
|
|
"\n",
|
|
"print(\"\\n=== Confusion Matrices ===\")\n",
|
|
"print(\"\\nKNN\")\n",
|
|
"print(confusion_matrix(y, knn_pred))\n",
|
|
"\n",
|
|
"print(\"\\nLogistic Regression\")\n",
|
|
"print(confusion_matrix(y, logreg_pred))\n",
|
|
"\n",
|
|
"print(\"\\nDecision Tree\")\n",
|
|
"print(confusion_matrix(y, tree_pred))\n",
|
|
"\n",
|
|
"print(\"\\n=== Classification Reports ===\")\n",
|
|
"print(\"\\nKNN\")\n",
|
|
"print(classification_report(y, knn_pred))\n",
|
|
"\n",
|
|
"print(\"\\nLogistic Regression\")\n",
|
|
"print(classification_report(y, logreg_pred))\n",
|
|
"\n",
|
|
"print(\"\\nDecision Tree\")\n",
|
|
"print(classification_report(y, tree_pred))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "34354ec9",
|
|
"metadata": {
|
|
"cell_marker": "###########################################",
|
|
"lines_to_next_cell": 0
|
|
},
|
|
"source": [
|
|
"BLOCK 14: LETS GET THE BEST VALUE FOR K\n",
|
|
"task: use the k value to recompute the previous KNN model and evaluate the performance\n",
|
|
"#########################################"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"id": "93fa45e5",
|
|
"metadata": {},
|
|
"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",
|
|
"\n",
|
|
"k_values = range(1, 21)\n",
|
|
"scores = []\n",
|
|
"\n",
|
|
"for k in k_values:\n",
|
|
" knn = KNeighborsClassifier(n_neighbors=k)\n",
|
|
" cv_scores = cross_val_score(knn, X, y, cv=5)\n",
|
|
" scores.append(np.mean(cv_scores))\n",
|
|
"\n",
|
|
"best_k = k_values[np.argmax(scores)]\n",
|
|
"\n",
|
|
"print(\"Best k:\", best_k)\n",
|
|
"print(\"Best CV Accuracy:\", max(scores))\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"jupytext": {
|
|
"cell_metadata_filter": "-all",
|
|
"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,
|
|
"nbformat_minor": 5
|
|
}
|