Tutorial 10 - Clustering
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
Describe a case where clustering would be an appropriate tool, and what insight it would bring from the data.
Explain the k-means clustering algorithm.
Interpret the output of a k-means cluster analysis.
Perform k-means clustering in R using k-means
Visualize the output of k-means clustering in R using a coloured scatter plot
Identify when it is necessary to scale variables before clustering and do this using R
Use the elbow method to choose the number of clusters for k-means
Describe advantages, limitations and assumptions of the kmeans clustering algorithm.
In [ ]:
### Run this cell before continuing.
library(tidyverse)
library(repr)
library(GGally)
library(broom)
options(repr.matrix.max.rows = 6)
source('tests.R')
source("cleanup.R")
1. Pokemon
We will be working with the Pokemon dataset from Kaggle, which can be found here. (https://www.kaggle.com/abcsds/pokemon) This dataset compiles
the statistics on 721 Pokemon. The information in this dataset includes Pokemon name, type, health points, attack strength, defensive strength, speed
points etc. These are values that apply to a Pokemon's abilities (higher values are better). We are interested in seeing if there are any sub-groups/clusters
of pokemon based on these statistics. And if so, how many sub-groups/clusters there are.
Source: https://media.giphy.com/media/3oEduV4SOS9mmmIOkw/giphy.gif (https://media.giphy.com/media/3oEduV4SOS9mmmIOkw/giphy.gif)
Question 1.0
{points: 1}
Use read_csv to load pokemon.csv from the data/ folder.
Assign your answer to an object called pm_data .
In [ ]:
### BEGIN SOLUTION
pm_data <- read_csv("data/pokemon.csv")
### END SOLUTION
pm_data
In [ ]:
test_1.0()
, Question 1.1
{points: 1}
Create a matrix of plots using ggpairs , choosing columns 5 to 11 (or equivalently, columns Total to Speed ) from pm_data . First use the
select function to extract columns "Total":"Speed" , and then pass the resulting dataframe to ggpairs to plot.
Assign your answer to an object called pm_pairs .
In [ ]:
### BEGIN SOLUTION
options(repr.plot.height = 10, repr.plot.width = 20)
pm_pairs <- pm_data %>% select("Total":"Speed") %>%
ggpairs(aes(alpha = 0.05)) +
theme(text = element_text(size = 20))
### END SOLUTION
pm_pairs
In [ ]:
test_1.1()
Question 1.2
{points: 1}
Select the columns Speed and Defense , creating a new dataframe with only those columns.
Assign your answer to an object named km_data .
In [ ]:
### BEGIN SOLUTION
km_data <- pm_data %>%
select(Speed, Defense)
### END SOLUTION
km_data
In [ ]:
test_1.2()
Question 1.3
{points: 1}
Make a scatterplot to visualize the relationship between Speed and Defense of the Pokemon. Put the Speed variable on the x-axis, and the
Defense variable on the y-axis.
Assign your plot to an object called pm_scatter . Don't forget to do everything needed to make an effective visualization.
In [ ]:
### BEGIN SOLUTION
options(repr.plot.height = 6, repr.plot.width = 6)
pm_scatter <- ggplot(km_data, aes(x = Speed, y = Defense)) +
geom_point(alpha = 0.4, size = 2) +
labs(x = "Speed Points",
y = "Defense Points") +
theme(text = element_text(size = 20))
### END SOLUTION
pm_scatter
In [ ]:
test_1.3()
Question 1.4.1
{points: 3}
We are going to cluster the Pokemon based on their Speed and Defense . Will it matter much for our clustering if we scale our variables? Is there any
argument against scaling here?
Lecture and Tutorial Learning Goals:
After completing this week's lecture and tutorial work, you will be able to:
Describe a case where clustering would be an appropriate tool, and what insight it would bring from the data.
Explain the k-means clustering algorithm.
Interpret the output of a k-means cluster analysis.
Perform k-means clustering in R using k-means
Visualize the output of k-means clustering in R using a coloured scatter plot
Identify when it is necessary to scale variables before clustering and do this using R
Use the elbow method to choose the number of clusters for k-means
Describe advantages, limitations and assumptions of the kmeans clustering algorithm.
In [ ]:
### Run this cell before continuing.
library(tidyverse)
library(repr)
library(GGally)
library(broom)
options(repr.matrix.max.rows = 6)
source('tests.R')
source("cleanup.R")
1. Pokemon
We will be working with the Pokemon dataset from Kaggle, which can be found here. (https://www.kaggle.com/abcsds/pokemon) This dataset compiles
the statistics on 721 Pokemon. The information in this dataset includes Pokemon name, type, health points, attack strength, defensive strength, speed
points etc. These are values that apply to a Pokemon's abilities (higher values are better). We are interested in seeing if there are any sub-groups/clusters
of pokemon based on these statistics. And if so, how many sub-groups/clusters there are.
Source: https://media.giphy.com/media/3oEduV4SOS9mmmIOkw/giphy.gif (https://media.giphy.com/media/3oEduV4SOS9mmmIOkw/giphy.gif)
Question 1.0
{points: 1}
Use read_csv to load pokemon.csv from the data/ folder.
Assign your answer to an object called pm_data .
In [ ]:
### BEGIN SOLUTION
pm_data <- read_csv("data/pokemon.csv")
### END SOLUTION
pm_data
In [ ]:
test_1.0()
, Question 1.1
{points: 1}
Create a matrix of plots using ggpairs , choosing columns 5 to 11 (or equivalently, columns Total to Speed ) from pm_data . First use the
select function to extract columns "Total":"Speed" , and then pass the resulting dataframe to ggpairs to plot.
Assign your answer to an object called pm_pairs .
In [ ]:
### BEGIN SOLUTION
options(repr.plot.height = 10, repr.plot.width = 20)
pm_pairs <- pm_data %>% select("Total":"Speed") %>%
ggpairs(aes(alpha = 0.05)) +
theme(text = element_text(size = 20))
### END SOLUTION
pm_pairs
In [ ]:
test_1.1()
Question 1.2
{points: 1}
Select the columns Speed and Defense , creating a new dataframe with only those columns.
Assign your answer to an object named km_data .
In [ ]:
### BEGIN SOLUTION
km_data <- pm_data %>%
select(Speed, Defense)
### END SOLUTION
km_data
In [ ]:
test_1.2()
Question 1.3
{points: 1}
Make a scatterplot to visualize the relationship between Speed and Defense of the Pokemon. Put the Speed variable on the x-axis, and the
Defense variable on the y-axis.
Assign your plot to an object called pm_scatter . Don't forget to do everything needed to make an effective visualization.
In [ ]:
### BEGIN SOLUTION
options(repr.plot.height = 6, repr.plot.width = 6)
pm_scatter <- ggplot(km_data, aes(x = Speed, y = Defense)) +
geom_point(alpha = 0.4, size = 2) +
labs(x = "Speed Points",
y = "Defense Points") +
theme(text = element_text(size = 20))
### END SOLUTION
pm_scatter
In [ ]:
test_1.3()
Question 1.4.1
{points: 3}
We are going to cluster the Pokemon based on their Speed and Defense . Will it matter much for our clustering if we scale our variables? Is there any
argument against scaling here?