Preface – Applied Unsupervised Learning with R



This section briefly introduces the author, the coverage of this book, the technical skills you'll need to get started, and the hardware and software requirements required to complete all of the included activities and exercises.

About the Book

Starting with the basics, Applied Unsupervised Learning with R explains clustering methods, distribution analysis, data encoders, and features of R that enable you to understand your data better and get answers to your most pressing business questions.

This book begins with the most important and commonly used method for unsupervised learning – clustering – and explains the three main clustering algorithms: k-means, divisive, and agglomerative. Following this, you'll study market basket analysis, kernel density estimation, principal component analysis, and anomaly detection. You'll be introduced to these methods using code written in R, with further instructions on how to work with, edit, and improve R code. To help you gain a practical understanding, the book also features useful tips on applying these methods to real business problems, including market segmentation and fraud detection. By working through interesting activities, you'll explore data encoders and latent variable models.

By the end of this book, you will have a better understanding of different anomaly detection methods, such as outlier detection, Mahalanobis distances, and contextual and collective anomaly detection.

About the Authors

Alok Malik is a data scientist based in India. He has previously worked on creating and deploying unsupervised learning solutions in fields such as finance, cryptocurrency trading, logistics, and natural language processing. He has a bachelor's degree in technology from the Indian Institute of Information Technology, Design and Manufacturing, Jabalpur, where he studied electronics and communication engineering.

Bradford Tuckfield has designed and implemented data science solutions for firms in a variety of industries. He studied math for his bachelor's degree and economics for his Ph.D. He has written for scholarly journals and the popular press, on topics including linear algebra, psychology, and public policy.

Elevator Pitch

Design clever algorithms that discover hidden patterns and business-relevant insights from unstructured, unlabeled data.

Key Features

  • Build state-of-the-art algorithms that can solve your business' problems
  • Learn how to find hidden patterns in your data
  • Implement key concepts with hands-on exercises using real-world datasets


Starting with the basics, Applied Unsupervised Learning with R explains clustering methods, distribution analysis, data encoders, and all the features of R that enable you to understand your data better and get answers to all your business questions.

Learning Objectives

  • Implement clustering methods such as k-means, agglomerative, and divisive clustering
  • Write code in R to analyze market segmentation and consumer behavior
  • Estimate distributions and probabilities of different outcomes
  • Implement dimension reduction using principal component analysis
  • Apply anomaly detection methods to identify fraud
  • Design algorithms with R and learn how to edit or improve code


Applied Unsupervised Learning with R is designed for business professionals who want to learn about methods to understand their data better, and developers who have an interest in unsupervised learning. Although the book is for beginners, it will be beneficial to have some basic, beginner-level familiarity with R. This includes an understanding of how to open the R console, how to read data, and how to create a loop. To easily understand the concepts of this book, you should also know basic mathematical concepts, including exponents, square roots, means, and medians.


Applied Unsupervised Learning with R takes a hands-on approach to using R to reveal the hidden patterns in your unstructured data. It contains multiple activities that use real-life business scenarios for you to practice and apply your new skills in a highly relevant context.

Hardware Requirements

For the optimal student experience, we recommend the following hardware configuration:

  • Processor: Intel Core i5 or equivalent
  • Memory: 4 GB RAM
  • Storage: 5 GB available space
  • An internet connection

Software Requirements

We also recommend that you have the following software installed in advance:

  • OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit or Windows 10 64-bit, Linux (Ubuntu, Debian, Red Hat, or Suse), or the latest version of OS X
  • R (3.0.0 or more recent, available for free at


Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We import a factoextra library for visualization of the clusters we just created."

A block of code is set as follows:





New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "There are many different types of algorithms for performing k-medoids clustering. The simplest and most efficient of them is Partitioning Around Medoids, or PAM for short."

Installation and Setup

Each great journey begins with a humble step. Our upcoming adventure in the land of data wrangling is no exception. Before we can do awesome things with data, we need to be prepared with the most productive environment. In this small note, we shall see how to do that.

Installing R on Windows

To install R on Windows, follow these steps:

  1. Open the page dedicated to the latest version of R for Windows:
  2. Click the link that says "Download R X.X.X for Windows", where each X is a natural number, for example R 3.5.3. This will initiate a file download.
  3. The file you downloaded in Step 1 is an exe file. Double-click on this file, and a program will run that will install R on your computer.
  4. The installation program that you ran in Step 3 will prompt you with some questions about how you want to install R. Choose the default options for each of these prompts.
  5. You can open R by double-clicking on its icon wherever it was installed on your Windows computer during Step 4.

Installing R on macOS X

To install R on macOS X, perform the following:

  1. Open the following link, which is dedicated to the latest version of R for macOS X:
  2. Under the Latest release heading, click on a file called R-X.X.X.pkg, where each X stands for a natural number, for example, R-3.5.2.pkg.
  3. Open the file that you downloaded in Step 2. This will be a .pkg file. When you open this file, OS X will open installation wizards that will guide you through the rest of the process.
  4. When the installation wizards ask you to make a decision about the installation, choose the default option.
  5. After the installation wizards have completed their work, you will be able to double-click on R's icon wherever it was installed. This will open an R console.

Installing R on Linux

  1. Determine which package manager your version of Linux uses. Two common examples of package managers are yum and apt.
  2. Open the Terminal and type the following two commands using your package manager:

    sudo apt update

    sudo apt install r-base

    In this case, we wrote apt as the package manager, but if your version of Linux uses yum or some other package manager, you should replace every apt in these two lines with yum or the name of your package manager.

  3. If you encounter problems, it could be that your version of Linux is not accessing the correct repository. To tell Linux where the right repository is, you should open the sources.list file for your version of Linux. For Ubuntu, this file is stored by default at /etc/apt/sources.list.
  4. In the sources.list file, you need to add one of the following two lines if you are using either Debian or Ubuntu. For Ubuntu versions, use the following: For Debian versions, use the following: deb If you are using Red Hat, you can run the following line in the Terminal to tell Linux which repository to use to download R: sudo yum install epel-release.