Machine-Learning-using-R - old 2022/C3/K-Means-Clustering-in-R/English
Title of the script: k-means Clustering in R.
Author: Tanmay Srinath and Sudhakar Kumar (IIT Bombay)
Keywords: R, RStudio, machine learning, supervised, unsupervised, classification, k-means clustering, k-means++, video tutorial.
Visual Cue | Narration |
Show slide'
Opening Slide' |
Welcome to this spoken tutorial on K-Means Clustering in R. |
Show slide'
Learning Objectives' |
In this tutorial, we will learn about:
|
Show slide
System Specifications |
This tutorial is recorded using,
It is recommended to install R version 4.1.0 or higher. |
Show slide
Prerequisites |
To follow this tutorial, the learner should know:
If not, please access the relevant tutorials on this website. |
Show slide
k-means Clustering |
k-means Clustering
k-means Clustering is a powerful algorithm. |
Here we will learn some benefits of using it. | |
Show slide
Benefits of k-means Clustering |
|
Now let us learn a few applications of k-means Clustering. | |
Show Slide
Applications of k-means Clustering
https://archive.ics.uci.edu/ml/datasets/online+retail
https://archive.ics.uci.edu/ml/datasets/ILPD+(Indian+Liver+Patient+Dataset) |
|
Show Slide
Optimising k-means |
|
Show Slide
k-means++ algorithm |
|
Show Slide
k-means++ Model |
In this tutorial we will create 3 k-means++ models and compare their results.
Now let us implement k-means++ on the iris dataset. |
Show Slide
Download Files |
For this tutorial, we will use a script file K-means.R.
Please download this file from the Code files link of this tutorial. Make a copy and then use it for practising. |
[Computer screen]
Highlight K_means.R and the folder K-means |
I have downloaded and moved this file to the K-means folder.
This folder is located in the MLProject folder on my Desktop. I have also set the K-means folder as my Working Directory. |
Let us switch to RStudio. | |
Click Kmeans.R in RStudio
Point to Kmeans.R in RStudio. |
Open the script K-means.R in RStudio.
The script Kmeans.R opens in RStudio. |
[RStudio]
Highlight library(LICORS) |
We will use the LICORS package for creating our k-means++ models.
|
[RStudio]
library(LICORS) data(iris) |
Since I have already installed the package I will directly import it.
If you don’t have the LICORS library, install it before importing. |
[RStudio]
library(LICORS) data(iris) |
Select and run these commands. |
Click in the Environment tab to load the iris dataset. | Click in the Environment tab to load the iris dataset. |
Point to Sepal.Length and Sepal.Width columns. | Now let us create our first k-means++ model.
We will use sepal length and sepal width to separate different species. |
[RStudio]
set.seed(121) km_1=kmeanspp(iris[,1:2],3,iter.max=100,nstart=10) print(km_1) |
Type these commands. |
Highlight set.seed(121)
Highlight 3 Highlight iter.max=100 |
We set a seed for reproducible results.
3 denotes the number of clusters of our model.
|
Click the Run button. | Run these commands.
The output is shown in the console window. |
Drag boundary to see the console window clearly. | Drag boundary to see the console window clearly. |
Highlight output in console
|
Our model’s specifications are displayed here.
|
Drag boundary to see the Source window clearly. | Drag boundary to see the Source window clearly. |
Cursor in the Source window. | Now we shall tabulate the results of our model |
[RStudio]
table(km_1$cluster,iris$Species)
|
Type this command.
|
Highlight output in console. | Our model has misclassified 27 data points.
|
Point to Petal.Length and Petal.Width columns. | Let us now try another model.
|
[RStudio]
km_2=kmeanspp(iris[,3:4],3,iter.max=100)
|
Type the following commands
|
Highlight output in console | The output is shown in the console window.
|
Cursor in the Source window. | Finally, let us create a model that uses all the 4 dimensions of an iris flower. |
[RStudio]
km_3=kmeanspp(iris[,1:4],3,iter.max=100)
|
Type these commands.
|
Highlight output in console | This model cannot properly distinguish between versicolor and virginica.
|
Only Narration | With this we come to the end of this tutorial.
Let us summarise. |
Show Slide
Summary |
In this tutorial we have learnt about:
|
Show Slide
Assignment |
Now we will suggest the assignment for this Spoken Tutorial.
|
Show slide
About the Spoken Tutorial Project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it. |
Show slide
Spoken Tutorial Workshops |
We conduct workshops using Spoken Tutorials and give certificates.
|
Show Slide
Spoken Tutorial Forum to answer questions |
Please post your timed queries in this forum. |
Show Slide
Forum to answer questions |
Do you have any general/technical questions?
Please visit the forum given in the link. |
Show Slide
Textbook Companion |
The FOSSEE team coordinates the coding of solved examples of popular books and case study projects.
|
Show Slide
Acknowledgment |
The Spoken Tutorial and FOSSEE projects are funded by the Ministry of Education Govt of India. |
Show Slide
Thank You |
This tutorial is contributed by Tanmay Srinath and Madhuri Ganapathi from IIT Bombay.
Thank you for watching. |