# Applying machine learning algorithms – exercises: solutions

Below are the solutions to these exercises on applying machine learning to your dataset.

```####################
#                  #
#    Exercise 1    #
#                  #
####################

install.packages("caret")
library(caret)
data(iris)
validation <- createDataPartition(iris\$Species, p=0.80, list=FALSE)
validation20 <- iris[-validation,]
iris <- iris[validation,]

library(caret)
control <- trainControl(method="cv", number=10)

####################
#                  #
#    Exercise 2    #
#                  #
####################

library(caret)
control <- trainControl(method="cv", number=10)
metric <- "Accuracy"

####################
#                  #
#    Exercise 3    #
#                  #
####################

install.packages("rpart")
install.packages("kernlab")
install.packages("e1071")
install.packages("randomForest")
library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
# a) linear algorithms
set.seed(7)
fit.lda <- train(Species~., data=iris, method="lda", metric=metric, trControl=control)
# b) nonlinear algorithms
# CART
set.seed(7)
fit.cart <- train(Species~., data=iris, method="rpart", metric=metric, trControl=control)
# kNN
set.seed(7)
fit.knn <- train(Species~., data=iris, method="knn", metric=metric, trControl=control)
# SVM
set.seed(7)
fit.svm <- train(Species~., data=iris, method="svmRadial", metric=metric, trControl=control)
# Random Forest
set.seed(7)
fit.rf <- train(Species~., data=iris, method="rf", metric=metric, trControl=control)

####################
#                  #
#    Exercise 4    #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))

####################
#                  #
#    Exercise 5    #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf))
summary(results)

####################
#                  #
#    Exercise 6    #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
dotplot(results)

####################
#                  #
#    Exercise 7    #
#                  #
####################

#LDA

####################
#                  #
#    Exercise 8    #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
print(fit.lda)

####################
#                  #
#    Exercise 9    #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
predictions <- predict(fit.lda, validation20)

####################
#                  #
#    Exercise 10   #
#                  #
####################

library(caret)
library(rpart)
library(kernlab)
library(e1071)
library(randomForest)
predictions <- predict(fit.lda, validation20)
confusionMatrix(predictions, validation20\$Species)
```

# Applying machine learning algorithms – exercises

INTRODUCTION

If you are a newbie in the world of machine learning, then this tutorial is exactly what you need in order to introduce yourself to this exciting new part of the data science world.

This post includes a full machine learning project that will guide you step by step to create a “template,” which you can use later on other datasets.

Look at the examples given and try to understand the logic behind them. Then try to solve the exercises below using R and without looking at the answers. Then see the solutions to check your answers.

Exercise 1

Create a list named “control” that runs a 10-fold cross-validation. HINT: Use `trainControl()`.

Exercise 2

Use the metric of “Accuracy” to evaluate models.

Exercise 3

Build the “LDA”, “CART”, “kNN”, “SVM” and “RF” models.

Exercise 4

Create a list of the 5 models you just built and name it “results”. HINT: Use `resamples()`.

Learn more about machine learning in the online course Beginner to Advanced Guide on Machine Learning with R Tool. In this course you will learn how to:

• Create a machine learning algorithm from a beginner point of view
• Quickly dive into more advanced methods in an accessible pace and with more explanations
• And much more

This course shows a complete workflow start to finish. It is a great introduction and fallback when you have some experience.

Exercise 5

Report the accuracy of each model by using the summary function on the list “results”. HINT: Use `summary()`.

Exercise 6

Create a plot of the model evaluation results and compare the spread and the mean accuracy of each model. HINT: Use `dotplot()`.

Exercise 7

Which model seems to be the most accurate?

Exercise 8

Summarize the results of the best model and print them. HINT: Use `print()`.

Exercise 9

Run the “LDA” model directly on the validation set to create a factor named “predictions”. HINT: Use `predict()`.

Exercise 10

Summarize the results in a confusion matrix. HINT: Use `confusionMatrix()`.

# Visualizing dataset to apply machine learning – exercises: solutions

Below are the solutions to these exercises on visualizing datasets to apply machine learning.

```####################
#                  #
#    Exercise 1    #
#                  #
####################

install.packages("caret")
library(caret)
data(iris)
validation <- createDataPartition(iris\$Species, p=0.80, list=FALSE)
validation20 <- iris[-validation,]
iris <- iris[validation,]

x <- iris[,1:4]

####################
#                  #
#    Exercise 2    #
#                  #
####################

library(caret)
y <- iris[,5]

####################
#                  #
#    Exercise 3    #
#                  #
####################

library(caret)
boxplot(x[,1], main=names(iris)[1])

####################
#                  #
#    Exercise 4    #
#                  #
####################

library(caret)
par(mfrow=c(1,4))
for(i in 1:4) {
boxplot(x[,i], main=names(iris)[i])
}

####################
#                  #
#    Exercise 5    #
#                  #
####################

library(caret)
plot(y)

####################
#                  #
#    Exercise 6    #
#                  #
####################

library(caret)
featurePlot(x=x, y=y)

####################
#                  #
#    Exercise 7    #
#                  #
####################

install.packages("ellipse")
library(ellipse)
library(caret)
featurePlot(x=x, y=y,plot="ellipse")

####################
#                  #
#    Exercise 8    #
#                  #
####################

library(caret)
featurePlot(x=x, y=y, plot="box")

####################
#                  #
#    Exercise 9    #
#                  #
####################

library(caret)
scales <- list(x=list(relation="free"), y=list(relation="free"))

####################
#                  #
#    Exercise 10   #
#                  #
####################

library(caret)
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)
```

# Visualizing dataset to apply machine learning-exercises

INTRODUCTION

If you are a newbie in the world of machine learning, then this tutorial is exactly what you need in order to introduce yourself to this exciting new part of the data science world.

This post includes a full machine learning project that will guide you step by step to create a “template,” which you can use later on other datasets.

Look at the examples given and try to understand the logic behind them. Then try to solve the exercises below using R and without looking at the answers. Then see solutions to check your answers.

Exercise 1

Create a variable “x” and attach to it the input attributes of the “iris” dataset. HINT: Use columns 1 to 4.

Exercise 2

Create a variable “y” and attach to it the output attribute of the “iris” dataset. HINT: Use column 5.

Exercise 3

Create a whisker plot (boxplot) for the variable of the first column of the “iris” dataset. HINT: Use `boxplot()`.

Exercise 4

Now create a whisker plot for each one of the four input variables of the “iris” dataset in one image. HINT: Use `par()`.

Learn more about machine learning in the online course Beginner to Advanced Guide on Machine Learning with R Tool. In this course you will learn how to:

• Create a machine learning algorithm from a beginner point of view
• Quickly dive into more advanced methods in an accessible pace and with more explanations
• And much more

This course shows a complete workflow start to finish. It is a great introduction and fallback when you have some experience.

Exercise 5

Create a barplot to breakdown your output attribute. HINT: Use plot().

Exercise 6

Create a scatterplot matrix of the “iris” dataset using the “x” and “y” variables. HINT: Use `featurePlot()`.

Exercise 7

Create a scatterplot matrix with ellipses around each separated group. HINT: Use `plot="ellipse"`.

Exercise 8

Create box and whisker plots of each input variable again, but this time broken down into separated plots for each class. HINT: Use `plot="box"`.

Exercise 9

Create a list named “scales” that includes the “x” and “y” variables and set `relation` to “free” for both of them. HINT: Use `list()`

Exercise 10

Create a density plot matrix for each attribute by class value. HINT: Use `featurePlot()`.

# Bonus: Data Frame exercises – solutions

Below are the solutions to these exercises on Data Frames.

```####################
#                  #
#    Exercise 1    #
#                  #
####################

numeric = c(1, 2, 3)
character = c("a", "b", "c")
logical = c(TRUE, FALSE, TRUE)
date=c("1/1/2017","2/1/2017","3/1/2017")
df = data.frame(numeric, character, logical,date)

####################
#                  #
#    Exercise 2    #
#                  #
####################

iris[2, 1]

####################
#                  #
#    Exercise 3    #
#                  #
####################

iris["2", "Sepal.Length"]

####################
#                  #
#    Exercise 4    #
#                  #
####################

nrow(mtcars)

####################
#                  #
#    Exercise 5    #
#                  #
####################

ncol(iris)

####################
#                  #
#    Exercise 6    #
#                  #
####################

iris[[4]]

####################
#                  #
#    Exercise 7    #
#                  #
####################

iris[["Petal.Width"]]

####################
#                  #
#    Exercise 8    #
#                  #
####################

iris\$Petal.Length

####################
#                  #
#    Exercise 9    #
#                  #
####################

iris[,"Sepal.Width"]

####################
#                  #
#    Exercise 10   #
#                  #
####################

iris["1",]
```

# Bonus: Data Frame exercises

INTRODUCTION

Hello everybody! This is a bonus set of exercises that r exercises.com kindly provides.

In this part we are going to talk about data frames. A data frame is used for storing data tables. It is a list of vectors of equal length.

Answers to the exercises are available here.

Create a Data Frame

The following variable “df” is a data frame containing three vectors “numeric”, “character”, “logical”. Look at the example below:
```numeric = c(1, 2, 3) character = c("a", "b", "c") logical = c(TRUE, FALSE, TRUE) df = data.frame(numeric, character, logical) ```

Exercise 1

Create an experimental data frame with four vectors of equal length and fill them with content of your choice. HINT: Use `data.frame()`.

Built-in Data Frame

We will use built-in data frames in R for our lesson. For example, here is a built-in data frame in R, called iris.
`iris`

The first line of the table, is the header and includes the column names. Each horizontal line afterward is a data row, which begins with the name of the row, and then followed by the data. Each data member of a row is called a cell.

To retrieve data in a cell we should give its coordinates which begin with row position, then followed by a comma, and end with the column position.

For example here is the cell value from the first row, first column of iris.
`iris[1, 1]`

Exercise 2

Call the cell value from the second row, first column of the iris dataset.

We can use the row and column names instead of the numeric coordinates. Look at the example below:
`iris["3", "Sepal.Length"]`

Exercise 3

Call the cell value from second row, first column of the iris dataset using row and column names.

We can find the number of data rows in the data frame using the nrow function. Look at the example below:
`nrow(iris)`

Exercise 4

Display the number of rows of the iris dataset. HINT: Use `nrow()`.

The number of columns of a data frame is given by the ncol function. Look at the example below:
`ncol(iris)`

Exercise 5

Display the number of columns of the iris dataset. HINT: Use `ncol()`.

To retrieve a certain column vector of the built-in data set iris,for example the third, we write:
`iris[[3]]`

Exercise 6

Retrieve the fourth column vector of the iris dataset.

We can retrieve the same column vector by its name. Look at the example below:
`iris[["Sepal.Width"]]`

Exercise 7

Retrieve the fourth column vector of the iris dataset by using its name.

We can also retrieve with the “\$” operator like the example below:
`iris\$Sepal.Length`

Exercise 8

Retrieve the column vector “Petal.Length” using the “\$” operator.

You can also retrieve the same column vector is to use the single square bracket “[]” operator. Like the example below:
`iris[,"Petal.Length"]`

Exercise 9

Retrieve the column vector “Sepal.Width” using the single square bracket.

Exercise 10

Use the single square bracket method to call the first row of the iris dataset.

# Summarizing dataset to apply machine learning – exercises: solutions

Below are the solutions to these exercises on summarizing datasets to apply machine learning.

```####################
#                  #
#    Exercise 1    #
#                  #
####################

library(caret)
data(iris)
validation <- createDataPartition(iris\$Species, p=0.80, list=FALSE)

####################
#                  #
#    Exercise 2    #
#                  #
####################

library(caret)
data(iris)
validation <- createDataPartition(iris\$Species, p=0.80, list=FALSE)
validation20 <- iris[-validation,]

####################
#                  #
#    Exercise 3    #
#                  #
####################

library(caret)
data(iris)
validation <- createDataPartition(iris\$Species, p=0.80, list=FALSE)
validation20 <- iris[-validation,]
iris <- iris[validation,]

####################
#                  #
#    Exercise 4    #
#                  #
####################

library(caret)
dim(iris)

####################
#                  #
#    Exercise 5    #
#                  #
####################

library(caret)
sapply(iris, class)

####################
#                  #
#    Exercise 6    #
#                  #
####################

library(caret)

####################
#                  #
#    Exercise 7    #
#                  #
####################

library(caret)
levels(iris\$Species)

####################
#                  #
#    Exercise 8    #
#                  #
####################

library(caret)
percentage <- prop.table(table(iris\$Species)) * 100

####################
#                  #
#    Exercise 9    #
#                  #
####################

library(caret)
percentage <- prop.table(table(iris\$Species)) * 100
cbind(freq=table(iris\$Species), percentage=percentage)

####################
#                  #
#    Exercise 10   #
#                  #
####################

library(caret)
summary(iris)
```

# Summarizing dataset to apply machine learning – exercises

INTRODUCTION

If you are a newbie in the world of machine learning, then this tutorial is exactly what you need in order to introduce yourself to this exciting new part of the data science world.

This post includes a full machine learning project that will guide you step by step to create a “template,” which you can use later on other datasets.

Look at the examples given and try to understand the logic behind them. Then try to solve the exercises below using R and without looking at the answers. Then check the solutions.

Exercise 1

Create a list of 80% of the rows in the original dataset to use for training. HINT: Use `createDataPartition()`.

Exercise 2

Select 20% of the data for validation.

Exercise 3

Use the remaining 80% of data to train and test the models.

Exercise 4

Find the dimensions of the “iris” dataset. HINT: Use `dim()`.

Learn more about machine learning in the online course Beginner to Advanced Guide on Machine Learning with R Tool. In this course you will learn how to:

• Create a machine learning algorithm from a beginner point of view
• Quickly dive into more advanced methods in an accessible pace and with more explanations
• And much more

This course shows a complete workflow start to finish. It is a great introduction and fallback when you have some experience.

Exercise 5

Find the type of each attribute in your dataset. HINT: Use `sapply()`.

Exercise 6

Take a look at the first 5 rows of your dataset. HINT: Use `head()`.

Exercise 7

Find the levels of the variable “Species.” HINT: Use `levels()`.

Exercise 8

Find the percentages of rows that belong to the labels you found in Exercise 7. HINT: Use `prop.table()` and `table()`.

Exercise 9

Display the absolute count of instances for each class as well as its percentage. HINT: Use `cbind()`.

Exercise 10

Display the summary of the “iris” dataset. HINT: Use `summary()`.

# How to prepare and apply machine learning to your dataset

INTRODUCTION

If you are a newbie in the world of machine learning, then this tutorial is exactly what you need in order to introduce yourself to this exciting new part of the data science world.

This post includes a full machine learning project that will guide you step by step to create a “template,” which you can use later on other datasets.

In this step-by-step tutorial you will:

1. Use one of the most popular machine learning packages in R.
2. Explore a dataset by using statistical summaries and data visualization.
3. Build 5 machine-learning models, pick the best, and build confidence that the accuracy is reliable.

The process of a machine learning project may not be exactly the same, but there are certain standard and necessary steps:

1. Define Problem.
2. Prepare Data.
3. Evaluate Algorithms.
4. Improve Results.
5. Present Results.

1. PACKAGE INSTALLATION & DATA SET

The first thing you have to do is install and load the “caret” package with:
```install.packages("caret") library(caret)```

Moreover, we need a dataset to work with. The dataset we chose in our case is “iris,” which contains 150 observations of iris flowers. There are four columns of measurements of the flowers in centimeters. The fifth column is the species of the flower observed. All observed flowers belong to one of three species. To attach it to the environment, use:
`data(iris)`

1.1 Create a Validation Dataset

First of all, we need to validate that our data set is good. Later, we will use statistical methods to estimate the accuracy of the models that we create on unseen data. To be sure about the accuracy of the best model on unseen data, we will evaluate it on actual unseen data. To do this, we will “deposit” some data that the algorithms will not find and use this data later to get a second and independent idea of how accurate the best model really is.

We will split the loaded dataset into two, 80% of which we will use to train our models and 20% of which we will hold back as a validation dataset. Look at the example below:
#create a list of 80% of rows in the original dataset to use them for training
`validation_index <- createDataPartition(dataset\$Species, p=0.80, list=FALSE)`
# select 20% of the data for validation
`validation <- dataset[-validation_index,]`
# use the remaining 80% of data to training and testing the models
`dataset <- dataset[validation_index,]`

You now have training data in the dataset variable and a validation set that will be used later in the validation variable.

Learn more about machine learning in the online course Beginner to Advanced Guide on Machine Learning with R Tool. In this course you will learn how to:

• Create a machine learning algorhitm from a beginner point of view
• Quickly dive into more advanced methods in an accessible pace and with more explanations
• And much more

This course shows a complete workflow start to finish. It is a great introduction and fallback when you have some experience.

2. DATASET SUMMARY

In this step, we are going to explore our data set. More specifically, we need to know certain features of our dataset, like:

1. Dimensions of the dataset.
2. Types of the attributes.
3. Details of the data.
4. Levels of the class attribute.
5. Analysis of the instances in each class.
6. Statistical summary of all attributes.

2.1 Dimensions of Dataset

We can see of how many instances (rows) and how many attributes (columns) the data contains with the dim function. Look at the example below:
`dim(dataset)`

2.2 Types of Attributes

Knowing the types is important as it can help you summarize the data you have and possible transformations you might need to use to prepare the data before modeilng. They could be doubles, integers, strings, factors and other types. You can find it with:
`sapply(dataset, class)`

2.3 Details of the Data

You can take a look at the first 5 rows of the data with:
`head(dataset)`

2.4 Levels of the Class

The class variable is a factor that has multiple class labels or levels. Let’s look at the levels:
`levels(dataset\$Species)`

There are two types of classification problems: the multinomial like this one and the binary if there were two levels.

2.5 Class Distribution

Let’s now take a look at the number of instances that belong to each class. We can view this as an absolute count and as a percentage with:
```percentage <- prop.table(table(dataset\$Species)) * 100 cbind(freq=table(dataset\$Species), percentage=percentage)```

2.6 Statistical Summary

This includes the mean, the min and max values, as well as some percentiles. Look at the example below:
`summary(dataset)`

3. DATASET VISUALIZATION

We now have a basic idea about the data. We need to extend that with some visualizations, and for that reason we are going to use two types of plots:

1. Univariate plots to understand each attribute.
2. Multivariate plots to understand the relationships between attributes.

3.1 Univariate Plots

We can visualize just the input attributes and just the output attributes. Let’s set that up and call the input attributes x and the output attributes y.
```x <- dataset[,1:4] y <- dataset[,5]```

Since the input variables are numeric, we can create box and whisker plots of each one with:
```par(mfrow=c(1,4)) for(i in 1:4) { boxplot(x[,i], main=names(iris)[i]) }```

We can also create a barplot of the Species class variable to graphically display the class distribution.
`plot(y)`

3.2 Multivariate Plots

First, we create scatterplots of all pairs of attributes and color the points by class. Then, we can draw ellipses around them to make them more easily separated.
You have to install and call the “ellipse” package to do this.
```install.packages("ellipse") library(ellipse) featurePlot(x=x, y=y, plot="ellipse")```

We can also create box and whisker plots of each input variable, but this time they are broken down into separate plots for each class.
`featurePlot(x=x, y=y, plot="box")`

Next, we can get an idea of the distribution of each attribute. We will use some probability density plots to give smooth lines for each distribution.
```scales <- list(x=list(relation="free"), y=list(relation="free")) featurePlot(x=x, y=y, plot="density", scales=scales)```

4. ALGORITHMS EVALUATION

Now it is time to create some models of the data and estimate their accuracy on unseen data.

1. Use the test harness to use 10-fold cross validation.
2. Build 5 different models to predict species from flower measurements.
3. Select the best model.

4.1 Test Harness

This will split our dataset into 10 parts, train in 9, test on 1, and release for all combinations of train-test splits.
```control <- trainControl(method="cv", number=10) metric <- "Accuracy"```

We are using the metric of “Accuracy” to evaluate models. This is: (number of correctly predicted instances / divided by the total number of instances in the dataset)*100 to give a percentage.

4.2 Build Models

We don’t know which algorithms would be good on this problem or what configurations to use. We get an idea from the plots that we created earlier.

Algorithms evaluation:

1. Linear Discriminant Analysis (LDA)
2. Classification and Regression Trees (CART).
3. k-Nearest Neighbors (kNN).
4. Support Vector Machines (SVM) with a linear kernel.
5. Random Forest (RF)

This is a good mixture of simple linear (LDA), nonlinear (CART, kNN) and complex nonlinear methods (SVM, RF). We reset the random number seed before reach run to ensure that the evaluation of each algorithm is performed using exactly the same data splits. It ensures the results are directly comparable.

NOTE: To proceed, first install and load the following packages: “rpart”, “kernlab”, “e1071” and “randomForest”.

Let’s build our five models:
# a) linear algorithms
```set.seed(7) fit.lda <- train(Species~., data=dataset, method="lda", metric=metric, trControl=control)```
# b) nonlinear algorithms
# CART
```set.seed(7) fit.cart <- train(Species~., data=dataset, method="rpart", metric=metric, trControl=control)```
# kNN
```set.seed(7) fit.knn <- train(Species~., data=dataset, method="knn", metric=metric, trControl=control)```
# SVM
```set.seed(7) fit.svm <- train(Species~., data=dataset, method="svmRadial", metric=metric, trControl=control)```
# Random Forest
```set.seed(7) fit.rf <- train(Species~., data=dataset, method="rf", metric=metric, trControl=control)```

4.3 Select the Best Model

We now have 5 models and accuracy estimations for each so we have to compare them.

It is a good idea to create a list of the created models and use the summary function.
```results <- resamples(list(lda=fit.lda, cart=fit.cart, knn=fit.knn, svm=fit.svm, rf=fit.rf)) summary(results)```

Moreover, we can create a plot of the model evaluation results and compare the spread and the mean accuracy of each model. There is a population of accuracy measures for each algorithm because each algorithm was evaluated 10 times.
`dotplot(results)`

You can summarize the results for just the LDA model that seems to be the most accurate.
`print(fit.lda)`

5. Make Predictions

The LDA was the most accurate model. Now we want to get an idea of the accuracy of the model on our validation set.

We can run the LDA model directly on the validation set and summarize the results in a confusion matrix.
```predictions <- predict(fit.lda, validation) confusionMatrix(predictions, validation\$Species)```

# ggvis Exercises (Part-2)

INTRODUCTION

The ggvis package is used to make interactive data visualizations. The fact that it combines shiny’s reactive programming model and dplyr’s grammar of data transformation make it a useful tool for data scientists.

This package may allows us to implement features like interactivity, but on the other hand every interactive ggvis plot must be connected to a running R session.

Look at the examples given and try to understand the logic behind them. Then try to solve the exercises below using R and without looking at the answers. Then check the solutions.

Exercise 1

Create a list which will include the variables “Horsepower” and “MPG.city” of the “Cars93” data set and make a scatterplot. HINT: Use `ggvis()` and `layer_points()`.

Exercise 2

Add a slider to the scatterplot of Exercise 1 that sets the point size from 10 to 100. HINT: Use `input_slider()`.

Learn more about using ggvis in the online course R: Complete Data Visualization Solutions. In this course you will learn how to:

• Work extensively with the ggvis package and its functionality
• Learn what visualizations exist for your specific use case
• And much more

Exercise 3

Add a slider to the scatterplot of Exercise 1 that sets the point `opacity` from 0 to 1. HINT: Use `input_slider()`.

Exercise 4

Create a histogram of the variable “Horsepower” of the “Cars93” data set. HINT: Use `layer_histograms()`.

Exercise 5

Set the `width` and the `center` of the histogram bins you just created to 10.

Exercise 6

Add 2 sliders to the histogram you just created, one for `width` and the other for `center` with values from 0 to 10 and set the `step` to 1. HINT: Use `input_slider()`.

Exercise 7

Add the labels “Width” and “Center” to the two sliders respectively. HINT: Use `label`.

Exercise 8

Create a scatterplot of the variables “Horsepower” and “MPG.city” of the “Cars93” dataset with `size` = 10 and `opacity` = 0.5.

Exercise 9

Add to the scatterplot you just created a function which will set the `size` with the left and right keyboard controls. HINT: Use `left_right()`.

Exercise 10

Add interactivity to the scatterplot you just created using a function that shows the value of the “Horsepower” when you “mouseover” a certain point. HINT: Use `add_tooltip()`.