Compare commits
8 Commits
6b8a100ad0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 93d8bf4a5b | |||
| 4887c03420 | |||
| 5d4ec549c0 | |||
| b12bfa4287 | |||
| d06c904f4b | |||
| 31874b3159 | |||
| 2f5ba2c829 | |||
| e4aae5bb86 |
594
Classification Lab/inclass_lab.ipynb
Normal file
594
Classification Lab/inclass_lab.ipynb
Normal file
@@ -0,0 +1,594 @@
|
||||
{
|
||||
"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": null,
|
||||
"id": "a8899ca0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"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": null,
|
||||
"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": null,
|
||||
"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": null,
|
||||
"id": "f31f0262",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "395d887c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "deec64c6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "e9ef7bea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "84e1f5d4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "6643f0af",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "444fbb13",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "17493cc3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "c706abc3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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": null,
|
||||
"id": "93fa45e5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
284
Classification Lab/inclass_lab.py
Normal file
284
Classification Lab/inclass_lab.py
Normal file
@@ -0,0 +1,284 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Inclass-Lab
|
||||
|
||||
Automatically generated by Colab.
|
||||
|
||||
Original file is located at
|
||||
https://colab.research.google.com/drive/13N7lQHvv4_LxKgcerumJ-18e5GfdBggm
|
||||
"""
|
||||
|
||||
|
||||
|
||||
"""In-Class Lab: Comparing Classification Models (Breast Cancer Dataset)
|
||||
|
||||
Learning Objectives
|
||||
|
||||
---
|
||||
|
||||
|
||||
By the end of this lab, you should be able to:
|
||||
|
||||
|
||||
|
||||
* Train multiple models (KNN, Logistic Regression, Decision Tree)
|
||||
* Compare performance across various scenarios
|
||||
* Understand the effect of scaling and evaluation methods
|
||||
* Interpret confusion matrix & classification report
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
####################################
|
||||
# BLOCK 1:
|
||||
'''
|
||||
Load dataset
|
||||
Explain features/target
|
||||
Do train_test_split
|
||||
'''
|
||||
####################################
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
data = load_breast_cancer()
|
||||
X = data.data
|
||||
y = data.target
|
||||
|
||||
print(data.target_names)
|
||||
print(X.shape)
|
||||
#0 - malignant
|
||||
#1 - benigh
|
||||
|
||||
'''
|
||||
Answer: The dataset contains 569 samples with 30 features each. The target variable has two classes: 0 (malignant) and 1 (benign). The goal is to classify tumors as either malignant or benign based on the features provided.
|
||||
'''
|
||||
###############################
|
||||
# BLOCK 2: IMPORT LIBRARIES FOR CLASSIFIERS
|
||||
###############################
|
||||
from sklearn.neighbors import KNeighborsClassifier
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.tree import DecisionTreeClassifier
|
||||
import numpy as np
|
||||
#########################################
|
||||
# BLOCK 3: IMPORT MODUL FOR DATA SPLIT
|
||||
#########################################
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
####################################
|
||||
# BLOCK 4: SPLIT THE DATASET
|
||||
####################################
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||||
|
||||
####################################
|
||||
# BLOCK 5
|
||||
#TASK 1: Train 3 models (no scaling)
|
||||
####################################
|
||||
knn = KNeighborsClassifier(n_neighbors=5)
|
||||
logreg = LogisticRegression(max_iter=5000)
|
||||
tree = DecisionTreeClassifier()
|
||||
|
||||
knn.fit(X_train, y_train)
|
||||
logreg.fit(X_train, y_train)
|
||||
tree.fit(X_train, y_train)
|
||||
|
||||
####################################
|
||||
# BLOCK 6
|
||||
# Task 2: Evaluate accuracy
|
||||
'''
|
||||
* Which model performs best?
|
||||
* Are the results very different?
|
||||
'''
|
||||
####################################
|
||||
print("KNN:", knn.score(X_test, y_test))
|
||||
print("LogReg:", logreg.score(X_test, y_test))
|
||||
print("Tree:", tree.score(X_test, y_test))
|
||||
|
||||
'''
|
||||
Answer: The results are as follows:
|
||||
KNN: 0.956140350877193
|
||||
LogReg: 0.956140350877193
|
||||
Tree: 0.9298245614035088
|
||||
|
||||
They are all very similar, however LogReg and KNN perform slightly better than the Decision Tree in terms of accuracy on the test set. The difference is not very large, but it suggests that KNN and Logistic Regression may be more effective for this particular dataset.
|
||||
'''
|
||||
|
||||
####################################
|
||||
#BLOCK 7
|
||||
# Part 2: Evaluation Metrics
|
||||
# Task 3: Confusion Matrix
|
||||
|
||||
from sklearn.metrics import confusion_matrix
|
||||
print(confusion_matrix(y_test, knn.predict(X_test)))
|
||||
|
||||
#####################################
|
||||
# BLOCK 8: CLASSIFICATION FREPORT
|
||||
'''
|
||||
* Which model has better recall?
|
||||
* Which is better for detecting cancer?
|
||||
'''
|
||||
|
||||
'''
|
||||
Answer: The recall for the model in Block 8 is 0.88 for malignant, so it correctly identifies 88% of cancer cases.
|
||||
'''
|
||||
#####################################
|
||||
from sklearn.metrics import classification_report
|
||||
print(classification_report(y_test, knn.predict(X_test)))
|
||||
|
||||
########################################
|
||||
# BLOCK 9:
|
||||
# Part 3: Scaling Effect
|
||||
# Task 5: Apply scaling using pipeline
|
||||
########################################
|
||||
|
||||
from sklearn.pipeline import Pipeline
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
|
||||
|
||||
knn_scaled = Pipeline([
|
||||
('scaler', StandardScaler()),
|
||||
('knn', KNeighborsClassifier(n_neighbors=5))
|
||||
])
|
||||
|
||||
logreg_scaled = Pipeline([
|
||||
('scaler', StandardScaler()),
|
||||
('logreg', LogisticRegression(max_iter=5000))
|
||||
])
|
||||
|
||||
tree_scaled = Pipeline([
|
||||
('scaler', StandardScaler()),
|
||||
('tree', DecisionTreeClassifier(random_state=42))
|
||||
])
|
||||
|
||||
knn_scaled.fit(X_train, y_train)
|
||||
logreg_scaled.fit(X_train, y_train)
|
||||
tree_scaled.fit(X_train, y_train)
|
||||
|
||||
knn_pred_scaled = knn_scaled.predict(X_test)
|
||||
logreg_pred_scaled = logreg_scaled.predict(X_test)
|
||||
tree_pred_scaled = tree_scaled.predict(X_test)
|
||||
|
||||
print("\n=== RESULTS WITH SCALING ===")
|
||||
print("KNN Accuracy:", accuracy_score(y_test, knn_pred_scaled))
|
||||
print("Logistic Regression Accuracy:", accuracy_score(y_test, logreg_pred_scaled))
|
||||
print("Decision Tree Accuracy:", accuracy_score(y_test, tree_pred_scaled))
|
||||
|
||||
#####################################
|
||||
# BLOCK 10: CLASSIFICATION REPORT
|
||||
#####################################
|
||||
print("\n=== CLASSIFICATION REPORTS (WITH SCALING) ===")
|
||||
|
||||
print("\nKNN Report:")
|
||||
print(classification_report(y_test, knn_pred_scaled))
|
||||
|
||||
print("\nLogistic Regression Report:")
|
||||
print(classification_report(y_test, logreg_pred_scaled))
|
||||
|
||||
print("\nDecision Tree Report:")
|
||||
print(classification_report(y_test, tree_pred_scaled))
|
||||
|
||||
####################################
|
||||
# BLOCK 11: CONFUSION MATRIX
|
||||
####################################
|
||||
print("\n=== CONFUSION MATRICES (WITH SCALING) ===")
|
||||
|
||||
print("\nKNN Confusion Matrix:")
|
||||
print(confusion_matrix(y_test, knn_pred_scaled))
|
||||
|
||||
print("\nLogistic Regression Confusion Matrix:")
|
||||
print(confusion_matrix(y_test, logreg_pred_scaled))
|
||||
|
||||
print("\nDecision Tree Confusion Matrix:")
|
||||
print(confusion_matrix(y_test, tree_pred_scaled))
|
||||
|
||||
|
||||
|
||||
###############################################
|
||||
# BLOCK 12: MODWLS WITH CROSS-VALIDATION
|
||||
###############################################
|
||||
from sklearn.model_selection import cross_val_score
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
knn_pipeline = Pipeline([
|
||||
('scaler', StandardScaler()),
|
||||
('knn', KNeighborsClassifier(n_neighbors=5))
|
||||
])
|
||||
|
||||
logreg_pipeline = Pipeline([
|
||||
('scaler', StandardScaler()),
|
||||
('logreg', LogisticRegression(max_iter=5000))
|
||||
])
|
||||
|
||||
tree_model = DecisionTreeClassifier(random_state=42)
|
||||
|
||||
knn_cv_scores = cross_val_score(knn_pipeline, X, y, cv=5, scoring='accuracy')
|
||||
logreg_cv_scores = cross_val_score(logreg_pipeline, X, y, cv=5, scoring='accuracy')
|
||||
tree_cv_scores = cross_val_score(tree_model, X, y, cv=5, scoring='accuracy')
|
||||
|
||||
print("=== Cross-Validation Results ===")
|
||||
|
||||
print("\nKNN")
|
||||
print("CV Scores:", knn_cv_scores)
|
||||
print("Mean Accuracy:", np.mean(knn_cv_scores))
|
||||
print("Standard Deviation:", np.std(knn_cv_scores))
|
||||
|
||||
print("\nLogistic Regression")
|
||||
print("CV Scores:", logreg_cv_scores)
|
||||
print("Mean Accuracy:", np.mean(logreg_cv_scores))
|
||||
print("Standard Deviation:", np.std(logreg_cv_scores))
|
||||
|
||||
print("\nDecision Tree")
|
||||
print("CV Scores:", tree_cv_scores)
|
||||
print("Mean Accuracy:", np.mean(tree_cv_scores))
|
||||
print("Standard Deviation:", np.std(tree_cv_scores))
|
||||
|
||||
####################################
|
||||
# BLOCK 13: CONFUSION MATRIX
|
||||
####################################
|
||||
from sklearn.model_selection import cross_val_predict
|
||||
from sklearn.metrics import confusion_matrix, classification_report
|
||||
|
||||
knn_pred = cross_val_predict(knn_pipeline, X, y, cv=5)
|
||||
logreg_pred = cross_val_predict(logreg_pipeline, X, y, cv=5)
|
||||
tree_pred = cross_val_predict(tree_model, X, y, cv=5)
|
||||
|
||||
print("\n=== Confusion Matrices ===")
|
||||
print("\nKNN")
|
||||
print(confusion_matrix(y, knn_pred))
|
||||
|
||||
print("\nLogistic Regression")
|
||||
print(confusion_matrix(y, logreg_pred))
|
||||
|
||||
print("\nDecision Tree")
|
||||
print(confusion_matrix(y, tree_pred))
|
||||
|
||||
print("\n=== Classification Reports ===")
|
||||
print("\nKNN")
|
||||
print(classification_report(y, knn_pred))
|
||||
|
||||
print("\nLogistic Regression")
|
||||
print(classification_report(y, logreg_pred))
|
||||
|
||||
print("\nDecision Tree")
|
||||
print(classification_report(y, tree_pred))
|
||||
|
||||
###########################################
|
||||
# BLOCK 14: LETS GET THE BEST VALUE FOR K
|
||||
# task: use the k value to recompute the previous KNN model and evaluate the performance
|
||||
##########################################
|
||||
from sklearn.model_selection import cross_val_score
|
||||
import numpy as np
|
||||
|
||||
k_values = range(1, 21)
|
||||
scores = []
|
||||
|
||||
for k in k_values:
|
||||
knn = KNeighborsClassifier(n_neighbors=k)
|
||||
cv_scores = cross_val_score(knn, X, y, cv=5)
|
||||
scores.append(np.mean(cv_scores))
|
||||
|
||||
best_k = k_values[np.argmax(scores)]
|
||||
|
||||
print("Best k:", best_k)
|
||||
print("Best CV Accuracy:", max(scores))
|
||||
|
||||
4
Classwork Assignment/phrase.txt
Normal file
4
Classwork Assignment/phrase.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
When IN the course
|
||||
of human events IN
|
||||
the space of one
|
||||
place IN time IN memory
|
||||
4
Classwork Assignment/phrase.txt.bak
Normal file
4
Classwork Assignment/phrase.txt.bak
Normal file
@@ -0,0 +1,4 @@
|
||||
When in the course
|
||||
of human events in
|
||||
the space of one
|
||||
place in time in memory
|
||||
6
TextBlob/test.json
Normal file
6
TextBlob/test.json
Normal file
@@ -0,0 +1,6 @@
|
||||
[
|
||||
{"text": "The beer was good.", "label": "pos"},
|
||||
{"text": "I do not enjoy my job", "label": "neg"},
|
||||
{"text": "I feel amazing!", "label": "pos"},
|
||||
{"text": "I am very tired", "label": "neg"}
|
||||
]
|
||||
31
TextBlob/text_classifier.py
Normal file
31
TextBlob/text_classifier.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from textblob.classifiers import NaiveBayesClassifier
|
||||
from pathlib import Path
|
||||
|
||||
def main():
|
||||
# Get directory where this script is located
|
||||
base_dir = Path(__file__).parent
|
||||
|
||||
# Build paths to data files
|
||||
train_path = base_dir / "train.json"
|
||||
test_path = base_dir / "test.json"
|
||||
|
||||
# Load training data
|
||||
with open(train_path, "r") as train_file:
|
||||
classifier = NaiveBayesClassifier(train_file, format="json")
|
||||
|
||||
# Load test data
|
||||
with open(test_path, "r") as test_file:
|
||||
accuracy = classifier.accuracy(test_file)
|
||||
|
||||
print("Accuracy:", accuracy)
|
||||
|
||||
print("Classifying sentences:")
|
||||
print("I love this place ->", classifier.classify("I love this place"))
|
||||
print("I hate this job ->", classifier.classify("I hate this job"))
|
||||
|
||||
print("\nMost Informative Features:")
|
||||
classifier.show_informative_features(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
12
TextBlob/train.json
Normal file
12
TextBlob/train.json
Normal file
@@ -0,0 +1,12 @@
|
||||
[
|
||||
{"text": "I love this sandwich.", "label": "pos"},
|
||||
{"text": "This is an amazing place!", "label": "pos"},
|
||||
{"text": "I feel very good about these beers.", "label": "pos"},
|
||||
{"text": "This is my best work.", "label": "pos"},
|
||||
{"text": "What an awesome view", "label": "pos"},
|
||||
{"text": "I do not like this restaurant", "label": "neg"},
|
||||
{"text": "I am tired of this stuff.", "label": "neg"},
|
||||
{"text": "I can't deal with this", "label": "neg"},
|
||||
{"text": "He is my sworn enemy!", "label": "neg"},
|
||||
{"text": "My boss is horrible.", "label": "neg"}
|
||||
]
|
||||
Reference in New Issue
Block a user