Tutorial 6: Classification
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
Recognize situations where a simple classifier would be appropriate for making predictions.
Explain the k-nearest neighbour classification algorithm.
Interpret the output of a classifier.
Compute, by hand, the distance between points when there are two explanatory variables/predictors.
Describe what a training data set is and how it is used in classification.
In a dataset with two explanatory variables/predictors, perform k-nearest neighbour classification in R using tidymodels to predict the class of a
single new observation.
In [ ]:
### Run this cell before continuing.
library(tidyverse)
library(repr)
library(tidymodels)
options(repr.matrix.max.rows = 6)
source('tests.R')
source("cleanup.R")
Question 0.1 Multiple Choice:
{points: 1}
Before applying k-nearest neighbour to a classification task, we need to scale the data. What is the purpose of this step?
A. To help speed up the knn algorithm.
B. To convert all data observations to numeric values.
C. To ensure all data observations will be on a comparable scale and contribute equal shares to the calculation of the distance between points.
D. None of the above.
Assign your answer to an object called answer0.1 . Make sure the correct answer is an uppercase letter. Surround your answer with quotation marks
(e.g. "F" ).
Note: we typically standardize (i.e., scale and center) the data before doing classification. For the K-nearest neighbour algorithm specifically, centering
has no effect. But it doesn't hurt, and can help with other predictive data analyses, so we will do it below.
In [ ]:
# Replace the fail() with your answer.
### BEGIN SOLUTION
answer0.1 <- "C"
### END SOLUTION
In [ ]:
test_0.1()
1. Fruit Data Example
In the agricultural industry, cleaning, sorting, grading, and packaging food products are all necessary tasks in the post-harvest process. Products are
classified based on appearance, size and shape, attributes which helps determine the quality of the food. Sorting can be done by humans, but it is tedious
and time consuming. Automatic sorting could help save time and money. Images of the food products are captured and analysed to determine visual
characteristics.
The dataset (https://www.kaggle.com/mjamilmoughal/k-nearest-neighbor-classifier-to-predict-fruits/notebook) contains observations of fruit described with
four features 1) mass (in g) 2) width (in cm) 3) height (in cm) and 4) color score (on a scale from 0 - 1).
, Question 1.0
{points: 1}
Load the file, fruit_data.csv , into your notebook.
mutate() the fruit_name column such that it is a factor using the as_factor() function.
Assign your data to an object called fruit_data .
In [ ]:
### BEGIN SOLUTION
fruit_data <- read_csv("data/fruit_data.csv")
fruit_data <- fruit_data %>%
mutate(fruit_name = as_factor(fruit_name))
### END SOLUTION
In [ ]:
test_1.0()
Let's take a look at the first few observations in the fruit dataset. Run the cell below.
In [ ]:
# Run this cell.
fruit_data
Question 1.0.1 Multiple Choice:
{points: 1}
Which of the columns should we treat as categorical variables?
A. Fruit label, width, fruit subtype
B. Fruit name, color score, height
C. Fruit label, fruit subtype, fruit name
D. Color score, mass, width
Assign your answer to an object called answer1.0.1 . Make sure the correct answer is an uppercase letter. Remember to surround your answer with
quotation marks (e.g. "E" ).
In [ ]:
# Replace the fail() with your answer.
### BEGIN SOLUTION
answer1.0.1 <- "C"
### END SOLUTION
In [ ]:
test_1.0.1()
Run the cell below, and find the nearest neighbour based on mass and width to the first observation just by looking at the scatterplot (the first observation
has been circled for you).
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
Recognize situations where a simple classifier would be appropriate for making predictions.
Explain the k-nearest neighbour classification algorithm.
Interpret the output of a classifier.
Compute, by hand, the distance between points when there are two explanatory variables/predictors.
Describe what a training data set is and how it is used in classification.
In a dataset with two explanatory variables/predictors, perform k-nearest neighbour classification in R using tidymodels to predict the class of a
single new observation.
In [ ]:
### Run this cell before continuing.
library(tidyverse)
library(repr)
library(tidymodels)
options(repr.matrix.max.rows = 6)
source('tests.R')
source("cleanup.R")
Question 0.1 Multiple Choice:
{points: 1}
Before applying k-nearest neighbour to a classification task, we need to scale the data. What is the purpose of this step?
A. To help speed up the knn algorithm.
B. To convert all data observations to numeric values.
C. To ensure all data observations will be on a comparable scale and contribute equal shares to the calculation of the distance between points.
D. None of the above.
Assign your answer to an object called answer0.1 . Make sure the correct answer is an uppercase letter. Surround your answer with quotation marks
(e.g. "F" ).
Note: we typically standardize (i.e., scale and center) the data before doing classification. For the K-nearest neighbour algorithm specifically, centering
has no effect. But it doesn't hurt, and can help with other predictive data analyses, so we will do it below.
In [ ]:
# Replace the fail() with your answer.
### BEGIN SOLUTION
answer0.1 <- "C"
### END SOLUTION
In [ ]:
test_0.1()
1. Fruit Data Example
In the agricultural industry, cleaning, sorting, grading, and packaging food products are all necessary tasks in the post-harvest process. Products are
classified based on appearance, size and shape, attributes which helps determine the quality of the food. Sorting can be done by humans, but it is tedious
and time consuming. Automatic sorting could help save time and money. Images of the food products are captured and analysed to determine visual
characteristics.
The dataset (https://www.kaggle.com/mjamilmoughal/k-nearest-neighbor-classifier-to-predict-fruits/notebook) contains observations of fruit described with
four features 1) mass (in g) 2) width (in cm) 3) height (in cm) and 4) color score (on a scale from 0 - 1).
, Question 1.0
{points: 1}
Load the file, fruit_data.csv , into your notebook.
mutate() the fruit_name column such that it is a factor using the as_factor() function.
Assign your data to an object called fruit_data .
In [ ]:
### BEGIN SOLUTION
fruit_data <- read_csv("data/fruit_data.csv")
fruit_data <- fruit_data %>%
mutate(fruit_name = as_factor(fruit_name))
### END SOLUTION
In [ ]:
test_1.0()
Let's take a look at the first few observations in the fruit dataset. Run the cell below.
In [ ]:
# Run this cell.
fruit_data
Question 1.0.1 Multiple Choice:
{points: 1}
Which of the columns should we treat as categorical variables?
A. Fruit label, width, fruit subtype
B. Fruit name, color score, height
C. Fruit label, fruit subtype, fruit name
D. Color score, mass, width
Assign your answer to an object called answer1.0.1 . Make sure the correct answer is an uppercase letter. Remember to surround your answer with
quotation marks (e.g. "E" ).
In [ ]:
# Replace the fail() with your answer.
### BEGIN SOLUTION
answer1.0.1 <- "C"
### END SOLUTION
In [ ]:
test_1.0.1()
Run the cell below, and find the nearest neighbour based on mass and width to the first observation just by looking at the scatterplot (the first observation
has been circled for you).