Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,605 changes: 8,605 additions & 0 deletions 01_materials/notebooks/ClassW_Jan_7_Classfication-1.ipynb

Large diffs are not rendered by default.

1,179 changes: 994 additions & 185 deletions 01_materials/notebooks/Classification-2.ipynb

Large diffs are not rendered by default.

2,659 changes: 2,623 additions & 36 deletions 02_activities/assignments/assignment_1.ipynb

Large diffs are not rendered by default.

455 changes: 455 additions & 0 deletions 02_activities/assignments/assignment_1_cohort 4.ipynb

Large diffs are not rendered by default.

385 changes: 385 additions & 0 deletions 02_activities/assignments/assignment_1_ori.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,385 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7b0bcac6-5086-4f4e-928a-570a9ff7ae58",
"metadata": {},
"source": [
"# Assignment 1"
]
},
{
"cell_type": "markdown",
"id": "5fce0350-2a17-4e93-8d4c-0b8748fdfc32",
"metadata": {},
"source": [
"You only need to write one line of code for each question. When answering questions that ask you to identify or interpret something, the length of your response doesn’t matter. For example, if the answer is just ‘yes,’ ‘no,’ or a number, you can just give that answer without adding anything else.\n",
"\n",
"We will go through comparable code and concepts in the live learning session. If you run into trouble, start by using the help `help()` function in Python, to get information about the datasets and function in question. The internet is also a great resource when coding (though note that **no outside searches are required by the assignment!**). If you do incorporate code from the internet, please cite the source within your code (providing a URL is sufficient).\n",
"\n",
"Please bring questions that you cannot work out on your own to office hours, work periods or share with your peers on Slack. We will work with you through the issue."
]
},
{
"cell_type": "markdown",
"id": "5fc5001c-7715-4ebe-b0f7-e4bd04349629",
"metadata": {},
"source": [
"### Classification using KNN\n",
"\n",
"Let's set up our workspace and use the **Wine dataset** from `scikit-learn`. This dataset contains 178 wine samples with 13 chemical features, used to classify wines into different classes based on their origin.\n",
"\n",
"The **response variable** is `class`, which indicates the type of wine. We'll use all of the chemical features to predict this response variable."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4a3485d6-ba58-4660-a983-5680821c5719",
"metadata": {},
"outputs": [],
"source": [
"# Import standard libraries\n",
"import pandas as pd\n",
"import numpy as np\n",
"import random\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.colors as mcolors\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.neighbors import KNeighborsClassifier\n",
"from sklearn.metrics import recall_score, precision_score\n",
"from sklearn.model_selection import cross_validate\n",
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.metrics import accuracy_score"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a431d282-f9ca-4d5d-8912-71ffc9d8ea19",
"metadata": {},
"outputs": [],
"source": [
"from sklearn.datasets import load_wine\n",
"\n",
"# Load the Wine dataset\n",
"wine_data = load_wine()\n",
"\n",
"# Convert to DataFrame\n",
"wine_df = pd.DataFrame(wine_data.data, columns=wine_data.feature_names)\n",
"\n",
"# Bind the 'class' (wine target) to the DataFrame\n",
"wine_df['class'] = wine_data.target\n",
"\n",
"# Display the DataFrame\n",
"wine_df\n"
]
},
{
"cell_type": "markdown",
"id": "721b2b17",
"metadata": {},
"source": [
"#### **Question 1:** \n",
"#### Data inspection\n",
"\n",
"Before fitting any model, it is essential to understand our data. **Use Python code** to answer the following questions about the **Wine dataset**:\n",
"\n",
"_(i)_ How many observations (rows) does the dataset contain?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "56916892",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
},
{
"cell_type": "markdown",
"id": "f7573b59",
"metadata": {},
"source": [
"_(ii)_ How many variables (columns) does the dataset contain?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df0ef103",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
},
{
"cell_type": "markdown",
"id": "cb5180c7",
"metadata": {},
"source": [
"_(iii)_ What is the 'variable type' of the response variable `class` (e.g., 'integer', 'category', etc.)? What are the 'levels' (unique values) of the variable?"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47989426",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
},
{
"cell_type": "markdown",
"id": "a25f5e1b",
"metadata": {},
"source": [
"\n",
"_(iv)_ How many predictor variables do we have (Hint: all variables other than `class`)? "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bd7b0910",
"metadata": {},
"outputs": [],
"source": [
"# Your answer here"
]
},
{
"cell_type": "markdown",
"id": "d631e8e3",
"metadata": {},
"source": [
"You can use `print()` and `describe()` to help answer these questions."
]
},
{
"cell_type": "markdown",
"id": "fa3832d7",
"metadata": {},
"source": [
"#### **Question 2:** \n",
"#### Standardization and data-splitting\n",
"\n",
"Next, we must preform 'pre-processing' or 'data munging', to prepare our data for classification/prediction. For KNN, there are three essential steps. A first essential step is to 'standardize' the predictor variables. We can achieve this using the scaler method, provided as follows:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc899b59",
"metadata": {},
"outputs": [],
"source": [
"# Select predictors (excluding the last column)\n",
"predictors = wine_df.iloc[:, :-1]\n",
"\n",
"# Standardize the predictors\n",
"scaler = StandardScaler()\n",
"predictors_standardized = pd.DataFrame(scaler.fit_transform(predictors), columns=predictors.columns)\n",
"\n",
"# Display the head of the standardized predictors\n",
"print(predictors_standardized.head())"
]
},
{
"cell_type": "markdown",
"id": "9981ca48",
"metadata": {},
"source": [
"(i) Why is it important to standardize the predictor variables?"
]
},
{
"cell_type": "markdown",
"id": "403ef0bb",
"metadata": {},
"source": [
"> Your answer here..."
]
},
{
"cell_type": "markdown",
"id": "8e2e1bea",
"metadata": {},
"source": [
"(ii) Why did we elect not to standard our response variable `Class`?"
]
},
{
"cell_type": "markdown",
"id": "fdee5a15",
"metadata": {},
"source": [
"> Your answer here..."
]
},
{
"cell_type": "markdown",
"id": "8077ec21",
"metadata": {},
"source": [
"(iii) A second essential step is to set a random seed. Do so below (Hint: use the random.seed function). Why is setting a seed important? Is the particular seed value important? Why or why not?"
]
},
{
"cell_type": "markdown",
"id": "f0676c21",
"metadata": {},
"source": [
"> Your answer here..."
]
},
{
"cell_type": "markdown",
"id": "36ab9229",
"metadata": {},
"source": [
"(iv) A third essential step is to split our standardized data into separate training and testing sets. We will split into 75% training and 25% testing. The provided code randomly partitions our data, and creates linked training sets for the predictors and response variables. \n",
"\n",
"Extend the code to create a non-overlapping test set for the predictors and response variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72c101f2",
"metadata": {},
"outputs": [],
"source": [
"# Do not touch\n",
"np.random.seed(123)\n",
"# Create a random vector of True and False values to split the data\n",
"split = np.random.choice([True, False], size=len(predictors_standardized), replace=True, p=[0.75, 0.25])"
]
},
{
"cell_type": "markdown",
"id": "4604ee03",
"metadata": {},
"source": [
"#### **Question 3:**\n",
"#### Model initialization and cross-validation\n",
"We are finally set to fit the KNN model. \n",
"\n",
"\n",
"Perform a grid search to tune the `n_neighbors` hyperparameter using 10-fold cross-validation. Follow these steps:\n",
"\n",
"1. Initialize the KNN classifier using `KNeighborsClassifier()`.\n",
"2. Define a parameter grid for `n_neighbors` ranging from 1 to 50.\n",
"3. Implement a grid search using `GridSearchCV` with 10-fold cross-validation to find the optimal number of neighbors.\n",
"4. After fitting the model on the training data, identify and return the best value for `n_neighbors` based on the grid search results."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08818c64",
"metadata": {},
"outputs": [],
"source": [
"# Your code here..."
]
},
{
"cell_type": "markdown",
"id": "3f76bf62",
"metadata": {},
"source": [
"#### **Question 4:**\n",
"#### Model evaluation\n",
"\n",
"Using the best value for `n_neighbors`, fit a KNN model on the training data and evaluate its performance on the test set using `accuracy_score`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffefa9f2",
"metadata": {},
"outputs": [],
"source": [
"# Your code here..."
]
},
{
"cell_type": "markdown",
"id": "6f8a69db",
"metadata": {},
"source": [
"# Criteria\n",
"\n",
"\n",
"| **Criteria** | **Complete** | **Incomplete** |\n",
"|--------------------------------------------------------|---------------------------------------------------|--------------------------------------------------|\n",
"| **Data Inspection** | Data is inspected for number of variables, observations and data types. | Data inspection is missing or incomplete. |\n",
"| **Data Scaling** | Data scaling or normalization is applied where necessary (e.g., using `StandardScaler`). | Data scaling or normalization is missing or incorrectly applied. |\n",
"| **Model Initialization** | The KNN model is correctly initialized and a random seed is set for reproducibility. | The KNN model is not initialized, is incorrect, or lacks a random seed for reproducibility. |\n",
"| **Parameter Grid for `n_neighbors`** | The parameter grid for `n_neighbors` is correctly defined. | The parameter grid is missing or incorrectly defined. |\n",
"| **Cross-Validation Setup** | Cross-validation is set up correctly with 10 folds. | Cross-validation is missing or incorrectly set up. |\n",
"| **Best Hyperparameter (`n_neighbors`) Selection** | The best value for `n_neighbors` is identified using the grid search results. | The best `n_neighbors` is not selected or incorrect. |\n",
"| **Model Evaluation on Test Data** | The model is evaluated on the test data using accuracy. | The model evaluation is missing or uses the wrong metric. |\n"
]
},
{
"cell_type": "markdown",
"id": "0b4390cc",
"metadata": {},
"source": [
"## Submission Information\n",
"\n",
"🚨 **Please review our [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md)** 🚨 for detailed instructions on how to format, branch, and submit your work. Following these guidelines is crucial for your submissions to be evaluated correctly.\n",
"\n",
"### Note:\n",
"\n",
"If you like, you may collaborate with others in the cohort. If you choose to do so, please indicate with whom you have worked with in your pull request by tagging their GitHub username. Separate submissions are required.\n",
"\n",
"### Submission Parameters:\n",
"* Submission Due Date: `11:59 PM - 01/12/2025`\n",
"* The branch name for your repo should be: `assignment-1`\n",
"* What to submit for this assignment:\n",
" * This Jupyter Notebook (assignment_1.ipynb) should be populated and should be the only change in your pull request.\n",
"* What the pull request link should look like for this assignment: `https://github.com/<your_github_username>/LCR/pull/<pr_id>`\n",
" * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n",
"\n",
"Checklist:\n",
"- [ ] Created a branch with the correct naming convention.\n",
"- [ ] Ensured that the repository is public.\n",
"- [ ] Reviewed the PR description guidelines and adhered to them.\n",
"- [ ] Verify that the link is accessible in a private browser window.\n",
"\n",
"If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#cohort-4-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges.\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "dsi_participant",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading
Loading