You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/4-Classification/2-Classifiers-1/solution/R/lesson_11-R.ipynb

1292 lines
82 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"nbformat": 4,
"nbformat_minor": 2,
"metadata": {
"colab": {
"name": "lesson_11-R.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
},
"kernelspec": {
"name": "ir",
"display_name": "R"
},
"language_info": {
"name": "R"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# Build a classification model: Delicious Asian and Indian Cuisines"
],
"metadata": {
"id": "zs2woWv_HoE8"
}
},
{
"cell_type": "markdown",
"source": [
"## Cuisine classifiers 1\n",
"\n",
"In this lesson, we'll explore a variety of classifiers to *predict a given national cuisine based on a group of ingredients.* While doing so, we'll learn more about some of the ways that algorithms can be leveraged for classification tasks.\n",
"\n",
"### [**Pre-lecture quiz**](https://white-water-09ec41f0f.azurestaticapps.net/quiz/21/)\n",
"\n",
"### **Preparation**\n",
"\n",
"This lesson builds up on our [previous lesson](https://github.com/microsoft/ML-For-Beginners/blob/main/4-Classification/1-Introduction/solution/lesson_10-R.ipynb) where we:\n",
"\n",
"- Made a gentle introduction to classifications using a dataset about all the brilliant cuisines of Asia and India 😋.\n",
"\n",
"- Explored some [dplyr verbs](https://dplyr.tidyverse.org/) to prep and clean our data.\n",
"\n",
"- Made beautiful visualizations using ggplot2.\n",
"\n",
"- Demonstrated how to deal with imbalanced data by preprocessing it using [recipes](https://recipes.tidymodels.org/articles/Simple_Example.html).\n",
"\n",
"- Demonstrated how to `prep` and `bake` our recipe to confirm that it will work as supposed to.\n",
"\n",
"#### **Prerequisite**\n",
"\n",
"For this lesson, we'll require the following packages to clean, prep and visualize our data:\n",
"\n",
"- `tidyverse`: The [tidyverse](https://www.tidyverse.org/) is a [collection of R packages](https://www.tidyverse.org/packages) designed to makes data science faster, easier and more fun!\n",
"\n",
"- `tidymodels`: The [tidymodels](https://www.tidymodels.org/) framework is a [collection of packages](https://www.tidymodels.org/packages/) for modeling and machine learning.\n",
"\n",
"\n",
"- `themis`: The [themis package](https://themis.tidymodels.org/) provides Extra Recipes Steps for Dealing with Unbalanced Data.\n",
"\n",
"- `nnet`: The [nnet package](https://cran.r-project.org/web/packages/nnet/nnet.pdf) provides functions for estimating feed-forward neural networks with a single hidden layer, and for multinomial logistic regression models.\n",
"\n",
"You can have them installed as:"
],
"metadata": {
"id": "iDFOb3ebHwQC"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"`install.packages(c(\"tidyverse\", \"tidymodels\", \"DataExplorer\", \"here\"))`\n",
"\n",
"Alternatively, the script below checks whether you have the packages required to complete this module and installs them for you in case they are missing."
],
"metadata": {
"id": "4V85BGCjII7F"
}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"suppressWarnings(if (!require(\"pacman\"))install.packages(\"pacman\"))\r\n",
"\r\n",
"pacman::p_load(tidyverse, tidymodels, themis, here)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Loading required package: pacman\n",
"\n"
]
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "an5NPyyKIKNR",
"outputId": "834d5e74-f4b8-49f9-8ab5-4c52ff2d7bc8"
}
},
{
"cell_type": "markdown",
"source": [
"Now, let's hit the ground running!\n",
"\n",
"## 1. Split the data into training and test sets.\n",
"\n",
"We'll start by picking a few steps from our previous lesson.\n",
"\n",
"### Drop the most common ingredients that create confusion between distinct cuisines, using `dplyr::select()`.\n",
"\n",
"Everyone loves rice, garlic and ginger!\n"
],
"metadata": {
"id": "0ax9GQLBINVv"
}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"# Load the original cuisines data\r\n",
"df <- read_csv(file = \"https://raw.githubusercontent.com/microsoft/ML-For-Beginners/main/4-Classification/data/cuisines.csv\")\r\n",
"\r\n",
"# Drop id column, rice, garlic and ginger from our original data set\r\n",
"df_select <- df %>% \r\n",
" select(-c(1, rice, garlic, ginger)) %>%\r\n",
" # Encode cuisine column as categorical\r\n",
" mutate(cuisine = factor(cuisine))\r\n",
"\r\n",
"# Display new data set\r\n",
"df_select %>% \r\n",
" slice_head(n = 5)\r\n",
"\r\n",
"# Display distribution of cuisines\r\n",
"df_select %>% \r\n",
" count(cuisine) %>% \r\n",
" arrange(desc(n))"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"New names:\n",
"* `` -> ...1\n",
"\n",
"\u001b[1m\u001b[1mRows: \u001b[1m\u001b[22m\u001b[34m\u001b[34m2448\u001b[34m\u001b[39m \u001b[1m\u001b[1mColumns: \u001b[1m\u001b[22m\u001b[34m\u001b[34m385\u001b[34m\u001b[39m\n",
"\n",
"\u001b[36m──\u001b[39m \u001b[1m\u001b[1mColumn specification\u001b[1m\u001b[22m \u001b[36m────────────────────────────────────────────────────────\u001b[39m\n",
"\u001b[1mDelimiter:\u001b[22m \",\"\n",
"\u001b[31mchr\u001b[39m (1): cuisine\n",
"\u001b[32mdbl\u001b[39m (384): ...1, almond, angelica, anise, anise_seed, apple, apple_brandy, a...\n",
"\n",
"\n",
"\u001b[36m\u001b[39m Use \u001b[30m\u001b[47m\u001b[30m\u001b[47m`spec()`\u001b[47m\u001b[30m\u001b[49m\u001b[39m to retrieve the full column specification for this data.\n",
"\u001b[36m\u001b[39m Specify the column types or set \u001b[30m\u001b[47m\u001b[30m\u001b[47m`show_col_types = FALSE`\u001b[47m\u001b[30m\u001b[49m\u001b[39m to quiet this message.\n",
"\n"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine almond angelica anise anise_seed apple apple_brandy apricot armagnac\n",
"1 indian 0 0 0 0 0 0 0 0 \n",
"2 indian 1 0 0 0 0 0 0 0 \n",
"3 indian 0 0 0 0 0 0 0 0 \n",
"4 indian 0 0 0 0 0 0 0 0 \n",
"5 indian 0 0 0 0 0 0 0 0 \n",
" artemisia ⋯ whiskey white_bread white_wine whole_grain_wheat_flour wine wood\n",
"1 0 ⋯ 0 0 0 0 0 0 \n",
"2 0 ⋯ 0 0 0 0 0 0 \n",
"3 0 ⋯ 0 0 0 0 0 0 \n",
"4 0 ⋯ 0 0 0 0 0 0 \n",
"5 0 ⋯ 0 0 0 0 0 0 \n",
" yam yeast yogurt zucchini\n",
"1 0 0 0 0 \n",
"2 0 0 0 0 \n",
"3 0 0 0 0 \n",
"4 0 0 0 0 \n",
"5 0 0 1 0 "
],
"text/markdown": [
"\n",
"A tibble: 5 × 381\n",
"\n",
"| cuisine &lt;fct&gt; | almond &lt;dbl&gt; | angelica &lt;dbl&gt; | anise &lt;dbl&gt; | anise_seed &lt;dbl&gt; | apple &lt;dbl&gt; | apple_brandy &lt;dbl&gt; | apricot &lt;dbl&gt; | armagnac &lt;dbl&gt; | artemisia &lt;dbl&gt; | ⋯ ⋯ | whiskey &lt;dbl&gt; | white_bread &lt;dbl&gt; | white_wine &lt;dbl&gt; | whole_grain_wheat_flour &lt;dbl&gt; | wine &lt;dbl&gt; | wood &lt;dbl&gt; | yam &lt;dbl&gt; | yeast &lt;dbl&gt; | yogurt &lt;dbl&gt; | zucchini &lt;dbl&gt; |\n",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"| indian | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| indian | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| indian | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| indian | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| indian | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 381\n",
"\\begin{tabular}{lllllllllllllllllllll}\n",
" cuisine & almond & angelica & anise & anise\\_seed & apple & apple\\_brandy & apricot & armagnac & artemisia & ⋯ & whiskey & white\\_bread & white\\_wine & whole\\_grain\\_wheat\\_flour & wine & wood & yam & yeast & yogurt & zucchini\\\\\n",
" <fct> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & ⋯ & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl>\\\\\n",
"\\hline\n",
"\t indian & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t indian & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t indian & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t indian & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t indian & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 381</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>almond</th><th scope=col>angelica</th><th scope=col>anise</th><th scope=col>anise_seed</th><th scope=col>apple</th><th scope=col>apple_brandy</th><th scope=col>apricot</th><th scope=col>armagnac</th><th scope=col>artemisia</th><th scope=col>⋯</th><th scope=col>whiskey</th><th scope=col>white_bread</th><th scope=col>white_wine</th><th scope=col>whole_grain_wheat_flour</th><th scope=col>wine</th><th scope=col>wood</th><th scope=col>yam</th><th scope=col>yeast</th><th scope=col>yogurt</th><th scope=col>zucchini</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>indian</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>indian</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>indian</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>indian</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>indian</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine n \n",
"1 korean 799\n",
"2 indian 598\n",
"3 chinese 442\n",
"4 japanese 320\n",
"5 thai 289"
],
"text/markdown": [
"\n",
"A tibble: 5 × 2\n",
"\n",
"| cuisine &lt;fct&gt; | n &lt;int&gt; |\n",
"|---|---|\n",
"| korean | 799 |\n",
"| indian | 598 |\n",
"| chinese | 442 |\n",
"| japanese | 320 |\n",
"| thai | 289 |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 2\n",
"\\begin{tabular}{ll}\n",
" cuisine & n\\\\\n",
" <fct> & <int>\\\\\n",
"\\hline\n",
"\t korean & 799\\\\\n",
"\t indian & 598\\\\\n",
"\t chinese & 442\\\\\n",
"\t japanese & 320\\\\\n",
"\t thai & 289\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 2</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>n</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>korean </td><td>799</td></tr>\n",
"\t<tr><td>indian </td><td>598</td></tr>\n",
"\t<tr><td>chinese </td><td>442</td></tr>\n",
"\t<tr><td>japanese</td><td>320</td></tr>\n",
"\t<tr><td>thai </td><td>289</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 735
},
"id": "jhCrrH22IWVR",
"outputId": "d444a85c-1d8b-485f-bc4f-8be2e8f8217c"
}
},
{
"cell_type": "markdown",
"source": [
"Perfect! Now, time to split the data such that 70% of the data goes to training and 30% goes to testing. We'll also apply a `stratification` technique when splitting the data to `maintain the proportion of each cuisine` in the training and validation datasets.\n",
"\n",
"[rsample](https://rsample.tidymodels.org/), a package in Tidymodels, provides infrastructure for efficient data splitting and resampling:"
],
"metadata": {
"id": "AYTjVyajIdny"
}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"# Load the core Tidymodels packages into R session\r\n",
"library(tidymodels)\r\n",
"\r\n",
"# Create split specification\r\n",
"set.seed(2056)\r\n",
"cuisines_split <- initial_split(data = df_select,\r\n",
" strata = cuisine,\r\n",
" prop = 0.7)\r\n",
"\r\n",
"# Extract the data in each split\r\n",
"cuisines_train <- training(cuisines_split)\r\n",
"cuisines_test <- testing(cuisines_split)\r\n",
"\r\n",
"# Print the number of cases in each split\r\n",
"cat(\"Training cases: \", nrow(cuisines_train), \"\\n\",\r\n",
" \"Test cases: \", nrow(cuisines_test), sep = \"\")\r\n",
"\r\n",
"# Display the first few rows of the training set\r\n",
"cuisines_train %>% \r\n",
" slice_head(n = 5)\r\n",
"\r\n",
"\r\n",
"# Display distribution of cuisines in the training set\r\n",
"cuisines_train %>% \r\n",
" count(cuisine) %>% \r\n",
" arrange(desc(n))"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Training cases: 1712\n",
"Test cases: 736"
]
},
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine almond angelica anise anise_seed apple apple_brandy apricot armagnac\n",
"1 chinese 0 0 0 0 0 0 0 0 \n",
"2 chinese 0 0 0 0 0 0 0 0 \n",
"3 chinese 0 0 0 0 0 0 0 0 \n",
"4 chinese 0 0 0 0 0 0 0 0 \n",
"5 chinese 0 0 0 0 0 0 0 0 \n",
" artemisia ⋯ whiskey white_bread white_wine whole_grain_wheat_flour wine wood\n",
"1 0 ⋯ 0 0 0 0 1 0 \n",
"2 0 ⋯ 0 0 0 0 1 0 \n",
"3 0 ⋯ 0 0 0 0 0 0 \n",
"4 0 ⋯ 0 0 0 0 0 0 \n",
"5 0 ⋯ 0 0 0 0 0 0 \n",
" yam yeast yogurt zucchini\n",
"1 0 0 0 0 \n",
"2 0 0 0 0 \n",
"3 0 0 0 0 \n",
"4 0 0 0 0 \n",
"5 0 0 0 0 "
],
"text/markdown": [
"\n",
"A tibble: 5 × 381\n",
"\n",
"| cuisine &lt;fct&gt; | almond &lt;dbl&gt; | angelica &lt;dbl&gt; | anise &lt;dbl&gt; | anise_seed &lt;dbl&gt; | apple &lt;dbl&gt; | apple_brandy &lt;dbl&gt; | apricot &lt;dbl&gt; | armagnac &lt;dbl&gt; | artemisia &lt;dbl&gt; | ⋯ ⋯ | whiskey &lt;dbl&gt; | white_bread &lt;dbl&gt; | white_wine &lt;dbl&gt; | whole_grain_wheat_flour &lt;dbl&gt; | wine &lt;dbl&gt; | wood &lt;dbl&gt; | yam &lt;dbl&gt; | yeast &lt;dbl&gt; | yogurt &lt;dbl&gt; | zucchini &lt;dbl&gt; |\n",
"|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n",
"| chinese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |\n",
"| chinese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |\n",
"| chinese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| chinese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"| chinese | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ⋯ | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 381\n",
"\\begin{tabular}{lllllllllllllllllllll}\n",
" cuisine & almond & angelica & anise & anise\\_seed & apple & apple\\_brandy & apricot & armagnac & artemisia & ⋯ & whiskey & white\\_bread & white\\_wine & whole\\_grain\\_wheat\\_flour & wine & wood & yam & yeast & yogurt & zucchini\\\\\n",
" <fct> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & ⋯ & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl>\\\\\n",
"\\hline\n",
"\t chinese & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t chinese & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t chinese & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t chinese & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\t chinese & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & ⋯ & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 381</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>almond</th><th scope=col>angelica</th><th scope=col>anise</th><th scope=col>anise_seed</th><th scope=col>apple</th><th scope=col>apple_brandy</th><th scope=col>apricot</th><th scope=col>armagnac</th><th scope=col>artemisia</th><th scope=col>⋯</th><th scope=col>whiskey</th><th scope=col>white_bread</th><th scope=col>white_wine</th><th scope=col>whole_grain_wheat_flour</th><th scope=col>wine</th><th scope=col>wood</th><th scope=col>yam</th><th scope=col>yeast</th><th scope=col>yogurt</th><th scope=col>zucchini</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>⋯</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>chinese</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>chinese</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>chinese</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>chinese</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"\t<tr><td>chinese</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>⋯</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine n \n",
"1 korean 559\n",
"2 indian 418\n",
"3 chinese 309\n",
"4 japanese 224\n",
"5 thai 202"
],
"text/markdown": [
"\n",
"A tibble: 5 × 2\n",
"\n",
"| cuisine &lt;fct&gt; | n &lt;int&gt; |\n",
"|---|---|\n",
"| korean | 559 |\n",
"| indian | 418 |\n",
"| chinese | 309 |\n",
"| japanese | 224 |\n",
"| thai | 202 |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 2\n",
"\\begin{tabular}{ll}\n",
" cuisine & n\\\\\n",
" <fct> & <int>\\\\\n",
"\\hline\n",
"\t korean & 559\\\\\n",
"\t indian & 418\\\\\n",
"\t chinese & 309\\\\\n",
"\t japanese & 224\\\\\n",
"\t thai & 202\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 2</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>n</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;int&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>korean </td><td>559</td></tr>\n",
"\t<tr><td>indian </td><td>418</td></tr>\n",
"\t<tr><td>chinese </td><td>309</td></tr>\n",
"\t<tr><td>japanese</td><td>224</td></tr>\n",
"\t<tr><td>thai </td><td>202</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 535
},
"id": "w5FWIkEiIjdN",
"outputId": "2e195fd9-1a8f-4b91-9573-cce5582242df"
}
},
{
"cell_type": "markdown",
"source": [
"## 2. Deal with imbalanced data\n",
"\n",
"As you might have noticed in the original data set as well as in our training set, there is quite an unequal distribution in the number of cuisines. Korean cuisines are *almost* 3 times Thai cuisines. Imbalanced data often has negative effects on the model performance. Many models perform best when the number of observations is equal and, thus, tend to struggle with unbalanced data.\n",
"\n",
"There are majorly two ways of dealing with imbalanced data sets:\n",
"\n",
"- adding observations to the minority class: `Over-sampling` e.g using a SMOTE algorithm which synthetically generates new examples of the minority class using nearest neighbors of these cases.\n",
"\n",
"- removing observations from majority class: `Under-sampling`\n",
"\n",
"In our previous lesson, we demonstrated how to deal with imbalanced data sets using a `recipe`. A recipe can be thought of as a blueprint that describes what steps should be applied to a data set in order to get it ready for data analysis. In our case, we want to have an equal distribution in the number of our cuisines for our `training set`. Let's get right into it."
],
"metadata": {
"id": "daBi9qJNIwqW"
}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"# Load themis package for dealing with imbalanced data\r\n",
"library(themis)\r\n",
"\r\n",
"# Create a recipe for preprocessing training data\r\n",
"cuisines_recipe <- recipe(cuisine ~ ., data = cuisines_train) %>% \r\n",
" step_smote(cuisine)\r\n",
"\r\n",
"# Print recipe\r\n",
"cuisines_recipe"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"Data Recipe\n",
"\n",
"Inputs:\n",
"\n",
" role #variables\n",
" outcome 1\n",
" predictor 380\n",
"\n",
"Operations:\n",
"\n",
"SMOTE based on cuisine"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 200
},
"id": "Az6LFBGxI1X0",
"outputId": "29d71d85-64b0-4e62-871e-bcd5398573b6"
}
},
{
"cell_type": "markdown",
"source": [
"You can of course go ahead and confirm (using prep+bake) that the recipe will work as you expect it - all the cuisine labels having `559` observations.\r\n",
"\r\n",
"Since we'll be using this recipe as a preprocessor for modeling, a `workflow()` will do all the prep and bake for us, so we won't have to manually estimate the recipe.\r\n",
"\r\n",
"Now we are ready to train a model 👩‍💻👨‍💻!\r\n",
"\r\n",
"## 3. Choosing your classifier\r\n",
"\r\n",
"<p >\r\n",
" <img src=\"../../images/parsnip.jpg\"\r\n",
" width=\"600\"/>\r\n",
" <figcaption>Artwork by @allison_horst</figcaption>\r\n"
],
"metadata": {
"id": "NBL3PqIWJBBB"
}
},
{
"cell_type": "markdown",
"source": [
"Now we have to decide which algorithm to use for the job 🤔.\r\n",
"\r\n",
"In Tidymodels, the [`parsnip package`](https://parsnip.tidymodels.org/index.html) provides consistent interface for working with models across different engines (packages). Please see the parsnip documentation to explore [model types & engines](https://www.tidymodels.org/find/parsnip/#models) and their corresponding [model arguments](https://www.tidymodels.org/find/parsnip/#model-args). The variety is quite bewildering at first sight. For instance, the following methods all include classification techniques:\r\n",
"\r\n",
"- C5.0 Rule-Based Classification Models\r\n",
"\r\n",
"- Flexible Discriminant Models\r\n",
"\r\n",
"- Linear Discriminant Models\r\n",
"\r\n",
"- Regularized Discriminant Models\r\n",
"\r\n",
"- Logistic Regression Models\r\n",
"\r\n",
"- Multinomial Regression Models\r\n",
"\r\n",
"- Naive Bayes Models\r\n",
"\r\n",
"- Support Vector Machines\r\n",
"\r\n",
"- Nearest Neighbors\r\n",
"\r\n",
"- Decision Trees\r\n",
"\r\n",
"- Ensemble methods\r\n",
"\r\n",
"- Neural Networks\r\n",
"\r\n",
"The list goes on!\r\n",
"\r\n",
"### **What classifier to go with?**\r\n",
"\r\n",
"So, which classifier should you choose? Often, running through several and looking for a good result is a way to test.\r\n",
"\r\n",
"> AutoML solves this problem neatly by running these comparisons in the cloud, allowing you to choose the best algorithm for your data. Try it [here](https://docs.microsoft.com/learn/modules/automate-model-selection-with-azure-automl/?WT.mc_id=academic-15963-cxa)\r\n",
"\r\n",
"Also the choice of classifier depends on our problem. For instance, when the outcome can be categorized into `more than two classes`, like in our case, you must use a `multiclass classification algorithm` as opposed to `binary classification.`\r\n",
"\r\n",
"### **A better approach**\r\n",
"\r\n",
"A better way than wildly guessing, however, is to follow the ideas on this downloadable [ML Cheat sheet](https://docs.microsoft.com/azure/machine-learning/algorithm-cheat-sheet?WT.mc_id=academic-15963-cxa). Here, we discover that, for our multiclass problem, we have some choices:\r\n",
"\r\n",
"<p >\r\n",
" <img src=\"../../images/cheatsheet.png\"\r\n",
" width=\"500\"/>\r\n",
" <figcaption>A section of Microsoft's Algorithm Cheat Sheet, detailing multiclass classification options</figcaption>\r\n",
"\r\n"
],
"metadata": {
"id": "a6DLAZ3vJZ14"
}
},
{
"cell_type": "markdown",
"source": [
"### **Reasoning**\n",
"\n",
"Let's see if we can reason our way through different approaches given the constraints we have:\n",
"\n",
"- **Deep Neural networks are too heavy**. Given our clean, but minimal dataset, and the fact that we are running training locally via notebooks, deep neural networks are too heavyweight for this task.\n",
"\n",
"- **No two-class classifier**. We do not use a two-class classifier, so that rules out one-vs-all.\n",
"\n",
"- **Decision tree or logistic regression could work**. A decision tree might work, or multinomial regression/multiclass logistic regression for multiclass data.\n",
"\n",
"- **Multiclass Boosted Decision Trees solve a different problem**. The multiclass boosted decision tree is most suitable for nonparametric tasks, e.g. tasks designed to build rankings, so it is not useful for us.\n",
"\n",
"Also, normally before embarking on more complex machine learning models e.g ensemble methods, it's a good idea to build the simplest possible model to get an idea of what is going on. So for this lesson, we'll start with a `multinomial regression` model.\n",
"\n",
"> Logistic regression is a technique used when the outcome variable is categorical (or nominal). For Binary logistic regression the number of outcome variables is two, whereas the number of outcome variables for multinomial logistic regression is more than two. See [Advanced Regression Methods](https://bookdown.org/chua/ber642_advanced_regression/multinomial-logistic-regression.html) for further reading.\n",
"\n",
"## 4. Train and evaluate a Multinomial logistic regression model.\n",
"\n",
"In Tidymodels, `parsnip::multinom_reg()`, defines a model that uses linear predictors to predict multiclass data using the multinomial distribution. See `?multinom_reg()` for the different ways/engines you can use to fit this model.\n",
"\n",
"For this example, we'll fit a Multinomial regression model via the default [nnet](https://cran.r-project.org/web/packages/nnet/nnet.pdf) engine.\n",
"\n",
"> I picked a value for `penalty` sort of randomly. There are better ways to choose this value that is, by using `resampling` and `tuning` the model which we'll discuss later.\n",
">\n",
"> See [Tidymodels: Get Started](https://www.tidymodels.org/start/tuning/) in case you want to learn more on how to tune model hyperparameters."
],
"metadata": {
"id": "gWMsVcbBJemu"
}
},
{
"cell_type": "code",
"execution_count": 6,
"source": [
"# Create a multinomial regression model specification\r\n",
"mr_spec <- multinom_reg(penalty = 1) %>% \r\n",
" set_engine(\"nnet\", MaxNWts = 2086) %>% \r\n",
" set_mode(\"classification\")\r\n",
"\r\n",
"# Print model specification\r\n",
"mr_spec"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"Multinomial Regression Model Specification (classification)\n",
"\n",
"Main Arguments:\n",
" penalty = 1\n",
"\n",
"Engine-Specific Arguments:\n",
" MaxNWts = 2086\n",
"\n",
"Computational engine: nnet \n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 166
},
"id": "Wq_fcyQiJvfG",
"outputId": "c30449c7-3864-4be7-f810-72a003743e2d"
}
},
{
"cell_type": "markdown",
"source": [
"Great job 🥳! Now that we have a recipe and a model specification, we need to find a way of bundling them together into an object that will first preprocess the data then fit the model on the preprocessed data and also allow for potential post-processing activities. In Tidymodels, this convenient object is called a [`workflow`](https://workflows.tidymodels.org/) and conveniently holds your modeling components! This is what we'd call *pipelines* in *Python*.\n",
"\n",
"So let's bundle everything up into a workflow!📦"
],
"metadata": {
"id": "NlSbzDfgJ0zh"
}
},
{
"cell_type": "code",
"execution_count": 7,
"source": [
"# Bundle recipe and model specification\r\n",
"mr_wf <- workflow() %>% \r\n",
" add_recipe(cuisines_recipe) %>% \r\n",
" add_model(mr_spec)\r\n",
"\r\n",
"# Print out workflow\r\n",
"mr_wf"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"══ Workflow ════════════════════════════════════════════════════════════════════\n",
"\u001b[3mPreprocessor:\u001b[23m Recipe\n",
"\u001b[3mModel:\u001b[23m multinom_reg()\n",
"\n",
"── Preprocessor ────────────────────────────────────────────────────────────────\n",
"1 Recipe Step\n",
"\n",
"• step_smote()\n",
"\n",
"── Model ───────────────────────────────────────────────────────────────────────\n",
"Multinomial Regression Model Specification (classification)\n",
"\n",
"Main Arguments:\n",
" penalty = 1\n",
"\n",
"Engine-Specific Arguments:\n",
" MaxNWts = 2086\n",
"\n",
"Computational engine: nnet \n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 333
},
"id": "Sc1TfPA4Ke3_",
"outputId": "82c70013-e431-4e7e-cef6-9fcf8aad4a6c"
}
},
{
"cell_type": "markdown",
"source": [
"Workflows 👌👌! A **`workflow()`** can be fit in much the same way a model can. So, time to train a model!"
],
"metadata": {
"id": "TNQ8i85aKf9L"
}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"# Train a multinomial regression model\n",
"mr_fit <- fit(object = mr_wf, data = cuisines_train)\n",
"\n",
"mr_fit"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"══ Workflow [trained] ══════════════════════════════════════════════════════════\n",
"\u001b[3mPreprocessor:\u001b[23m Recipe\n",
"\u001b[3mModel:\u001b[23m multinom_reg()\n",
"\n",
"── Preprocessor ────────────────────────────────────────────────────────────────\n",
"1 Recipe Step\n",
"\n",
"• step_smote()\n",
"\n",
"── Model ───────────────────────────────────────────────────────────────────────\n",
"Call:\n",
"nnet::multinom(formula = ..y ~ ., data = data, decay = ~1, MaxNWts = ~2086, \n",
" trace = FALSE)\n",
"\n",
"Coefficients:\n",
" (Intercept) almond angelica anise anise_seed apple\n",
"indian 0.19723325 0.2409661 0 -5.004955e-05 -0.1657635 -0.05769734\n",
"japanese 0.13961959 -0.6262400 0 -1.169155e-04 -0.4893596 -0.08585717\n",
"korean 0.22377347 -0.1833485 0 -5.560395e-05 -0.2489401 -0.15657804\n",
"thai -0.04336577 -0.6106258 0 4.903828e-04 -0.5782866 0.63451105\n",
" apple_brandy apricot armagnac artemisia artichoke asparagus\n",
"indian 0 0.37042636 0 -0.09122797 0 -0.27181970\n",
"japanese 0 0.28895643 0 -0.12651100 0 0.14054037\n",
"korean 0 -0.07981259 0 0.55756709 0 -0.66979948\n",
"thai 0 -0.33160904 0 -0.10725182 0 -0.02602152\n",
" avocado bacon baked_potato balm banana barley\n",
"indian -0.46624197 0.16008055 0 0 -0.2838796 0.2230625\n",
"japanese 0.90341344 0.02932727 0 0 -0.4142787 2.0953906\n",
"korean -0.06925382 -0.35804134 0 0 -0.2686963 -0.7233404\n",
"thai -0.21473955 -0.75594439 0 0 0.6784880 -0.4363320\n",
" bartlett_pear basil bay bean beech\n",
"indian 0 -0.7128756 0.1011587 -0.8777275 -0.0004380795\n",
"japanese 0 0.1288697 0.9425626 -0.2380748 0.3373437611\n",
"korean 0 -0.2445193 -0.4744318 -0.8957870 -0.0048784496\n",
"thai 0 1.5365848 0.1333256 0.2196970 -0.0113078024\n",
" beef beef_broth beef_liver beer beet\n",
"indian -0.7985278 0.2430186 -0.035598065 -0.002173738 0.01005813\n",
"japanese 0.2241875 -0.3653020 -0.139551027 0.128905553 0.04923911\n",
"korean 0.5366515 -0.6153237 0.213455197 -0.010828645 0.27325423\n",
"thai 0.1570012 -0.9364154 -0.008032213 -0.035063746 -0.28279823\n",
" bell_pepper bergamot berry bitter_orange black_bean\n",
"indian 0.49074330 0 0.58947607 0.191256164 -0.1945233\n",
"japanese 0.09074167 0 -0.25917977 -0.118915977 -0.3442400\n",
"korean -0.57876763 0 -0.07874180 -0.007729435 -0.5220672\n",
"thai 0.92554006 0 -0.07210196 -0.002983296 -0.4614426\n",
" black_currant black_mustard_seed_oil black_pepper black_raspberry\n",
"indian 0 0.38935801 -0.4453495 0\n",
"japanese 0 -0.05452887 -0.5440869 0\n",
"korean 0 -0.03929970 0.8025454 0\n",
"thai 0 -0.21498372 -0.9854806 0\n",
" black_sesame_seed black_tea blackberry blackberry_brandy\n",
"indian -0.2759246 0.3079977 0.191256164 0\n",
"japanese -0.6101687 -0.1671913 -0.118915977 0\n",
"korean 1.5197674 -0.3036261 -0.007729435 0\n",
"thai -0.1755656 -0.1487033 -0.002983296 0\n",
" blue_cheese blueberry bone_oil bourbon_whiskey brandy\n",
"indian 0 0.216164294 -0.2276744 0 0.22427587\n",
"japanese 0 -0.119186087 0.3913019 0 -0.15595599\n",
"korean 0 -0.007821986 0.2854487 0 -0.02562342\n",
"thai 0 -0.004947048 -0.0253658 0 -0.05715244\n",
"\n",
"...\n",
"and 308 more lines."
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "GMbdfVmTKkJI",
"outputId": "adf9ebdf-d69d-4a64-e9fd-e06e5322292e"
}
},
{
"cell_type": "markdown",
"source": [
"The output shows the coefficients that the model learned during training.\n",
"\n",
"### Evaluate the Trained Model\n",
"\n",
"It's time to see how the model performed 📏 by evaluating it on a test set! Let's begin by making predictions on the test set."
],
"metadata": {
"id": "tt2BfOxrKmcJ"
}
},
{
"cell_type": "code",
"execution_count": 9,
"source": [
"# Make predictions on the test set\n",
"results <- cuisines_test %>% select(cuisine) %>% \n",
" bind_cols(mr_fit %>% predict(new_data = cuisines_test))\n",
"\n",
"# Print out results\n",
"results %>% \n",
" slice_head(n = 5)"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine .pred_class\n",
"1 indian thai \n",
"2 indian indian \n",
"3 indian indian \n",
"4 indian indian \n",
"5 indian indian "
],
"text/markdown": [
"\n",
"A tibble: 5 × 2\n",
"\n",
"| cuisine &lt;fct&gt; | .pred_class &lt;fct&gt; |\n",
"|---|---|\n",
"| indian | thai |\n",
"| indian | indian |\n",
"| indian | indian |\n",
"| indian | indian |\n",
"| indian | indian |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 2\n",
"\\begin{tabular}{ll}\n",
" cuisine & .pred\\_class\\\\\n",
" <fct> & <fct>\\\\\n",
"\\hline\n",
"\t indian & thai \\\\\n",
"\t indian & indian\\\\\n",
"\t indian & indian\\\\\n",
"\t indian & indian\\\\\n",
"\t indian & indian\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 2</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>.pred_class</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>indian</td><td>thai </td></tr>\n",
"\t<tr><td>indian</td><td>indian</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 248
},
"id": "CqtckvtsKqax",
"outputId": "e57fe557-6a68-4217-fe82-173328c5436d"
}
},
{
"cell_type": "markdown",
"source": [
"Great job! In Tidymodels, evaluating model performance can be done using [yardstick](https://yardstick.tidymodels.org/) - a package used to measure the effectiveness of models using performance metrics. As we did in our logistic regression lesson, let's begin by computing a confusion matrix."
],
"metadata": {
"id": "8w5N6XsBKss7"
}
},
{
"cell_type": "code",
"execution_count": 10,
"source": [
"# Confusion matrix for categorical data\n",
"conf_mat(data = results, truth = cuisine, estimate = .pred_class)\n"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
" Truth\n",
"Prediction chinese indian japanese korean thai\n",
" chinese 83 1 8 15 10\n",
" indian 4 163 1 2 6\n",
" japanese 21 5 73 25 1\n",
" korean 15 0 11 191 0\n",
" thai 10 11 3 7 70"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 133
},
"id": "YvODvsLkK0iG",
"outputId": "bb69da84-1266-47ad-b174-d43b88ca2988"
}
},
{
"cell_type": "markdown",
"source": [
"When dealing with multiple classes, it's generally more intuitive to visualize this as a heat map, like this:"
],
"metadata": {
"id": "c0HfPL16Lr6U"
}
},
{
"cell_type": "code",
"execution_count": 11,
"source": [
"update_geom_defaults(geom = \"tile\", new = list(color = \"black\", alpha = 0.7))\n",
"# Visualize confusion matrix\n",
"results %>% \n",
" conf_mat(cuisine, .pred_class) %>% \n",
" autoplot(type = \"heatmap\")"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"plot without title"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA0gAAANICAMAAADKOT/pAAADAFBMVEUAAAABAQECAgIDAwMEBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJycoKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6Ojo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tMTExNTU1OTk5PT09QUFBRUVFSUlJTU1NUVFRVVVVWV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/2wnSeYckfyja1CwJ6TL3P0DKaZzl/G4nSJPdmMp3f/ydJKQR7rangZTLOMv53U6Qqm84sknr5we08Ly/nNqoFZByGWeZKISARBRCQCIKISARhRCQiEIISEQhBCSiEAISUQgBiSiEgEQUQkAiCiEgEYXQ/wMhANIDIZLX1QAAAABJRU5ErkJggg=="
},
"metadata": {
"image/png": {
"width": 420,
"height": 420
}
}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 436
},
"id": "HsAtwukyLsvt",
"outputId": "3032a224-a2c8-4270-b4f2-7bb620317400"
}
},
{
"cell_type": "markdown",
"source": [
"The darker squares in the confusion matrix plot indicate high numbers of cases, and you can hopefully see a diagonal line of darker squares indicating cases where the predicted and actual label are the same.\n",
"\n",
"Let's now calculate summary statistics for the confusion matrix."
],
"metadata": {
"id": "oOJC87dkLwPr"
}
},
{
"cell_type": "code",
"execution_count": 12,
"source": [
"# Summary stats for confusion matrix\n",
"conf_mat(data = results, truth = cuisine, estimate = .pred_class) %>% \n",
"summary()"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
" .metric .estimator .estimate\n",
"1 accuracy multiclass 0.7880435\n",
"2 kap multiclass 0.7276583\n",
"3 sens macro 0.7780927\n",
"4 spec macro 0.9477598\n",
"5 ppv macro 0.7585583\n",
"6 npv macro 0.9460080\n",
"7 mcc multiclass 0.7292724\n",
"8 j_index macro 0.7258524\n",
"9 bal_accuracy macro 0.8629262\n",
"10 detection_prevalence macro 0.2000000\n",
"11 precision macro 0.7585583\n",
"12 recall macro 0.7780927\n",
"13 f_meas macro 0.7641862"
],
"text/markdown": [
"\n",
"A tibble: 13 × 3\n",
"\n",
"| .metric &lt;chr&gt; | .estimator &lt;chr&gt; | .estimate &lt;dbl&gt; |\n",
"|---|---|---|\n",
"| accuracy | multiclass | 0.7880435 |\n",
"| kap | multiclass | 0.7276583 |\n",
"| sens | macro | 0.7780927 |\n",
"| spec | macro | 0.9477598 |\n",
"| ppv | macro | 0.7585583 |\n",
"| npv | macro | 0.9460080 |\n",
"| mcc | multiclass | 0.7292724 |\n",
"| j_index | macro | 0.7258524 |\n",
"| bal_accuracy | macro | 0.8629262 |\n",
"| detection_prevalence | macro | 0.2000000 |\n",
"| precision | macro | 0.7585583 |\n",
"| recall | macro | 0.7780927 |\n",
"| f_meas | macro | 0.7641862 |\n",
"\n"
],
"text/latex": [
"A tibble: 13 × 3\n",
"\\begin{tabular}{lll}\n",
" .metric & .estimator & .estimate\\\\\n",
" <chr> & <chr> & <dbl>\\\\\n",
"\\hline\n",
"\t accuracy & multiclass & 0.7880435\\\\\n",
"\t kap & multiclass & 0.7276583\\\\\n",
"\t sens & macro & 0.7780927\\\\\n",
"\t spec & macro & 0.9477598\\\\\n",
"\t ppv & macro & 0.7585583\\\\\n",
"\t npv & macro & 0.9460080\\\\\n",
"\t mcc & multiclass & 0.7292724\\\\\n",
"\t j\\_index & macro & 0.7258524\\\\\n",
"\t bal\\_accuracy & macro & 0.8629262\\\\\n",
"\t detection\\_prevalence & macro & 0.2000000\\\\\n",
"\t precision & macro & 0.7585583\\\\\n",
"\t recall & macro & 0.7780927\\\\\n",
"\t f\\_meas & macro & 0.7641862\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 13 × 3</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>.metric</th><th scope=col>.estimator</th><th scope=col>.estimate</th></tr>\n",
"\t<tr><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;chr&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>accuracy </td><td>multiclass</td><td>0.7880435</td></tr>\n",
"\t<tr><td>kap </td><td>multiclass</td><td>0.7276583</td></tr>\n",
"\t<tr><td>sens </td><td>macro </td><td>0.7780927</td></tr>\n",
"\t<tr><td>spec </td><td>macro </td><td>0.9477598</td></tr>\n",
"\t<tr><td>ppv </td><td>macro </td><td>0.7585583</td></tr>\n",
"\t<tr><td>npv </td><td>macro </td><td>0.9460080</td></tr>\n",
"\t<tr><td>mcc </td><td>multiclass</td><td>0.7292724</td></tr>\n",
"\t<tr><td>j_index </td><td>macro </td><td>0.7258524</td></tr>\n",
"\t<tr><td>bal_accuracy </td><td>macro </td><td>0.8629262</td></tr>\n",
"\t<tr><td>detection_prevalence</td><td>macro </td><td>0.2000000</td></tr>\n",
"\t<tr><td>precision </td><td>macro </td><td>0.7585583</td></tr>\n",
"\t<tr><td>recall </td><td>macro </td><td>0.7780927</td></tr>\n",
"\t<tr><td>f_meas </td><td>macro </td><td>0.7641862</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 494
},
"id": "OYqetUyzL5Wz",
"outputId": "6a84d65e-113d-4281-dfc1-16e8b70f37e6"
}
},
{
"cell_type": "markdown",
"source": [
"If we narrow down to some metrics such as accuracy, sensitivity, ppv, we are not badly off for a start 🥳!\n",
"\n",
"## 4. Digging Deeper\n",
"\n",
"Let's ask one subtle question: What criteria is used to settle for a given type of cuisine as the predicted outcome?\n",
"\n",
"Well, Statistical machine learning algorithms, like logistic regression, are based on `probability`; so what actually gets predicted by a classifier is a probability distribution over a set of possible outcomes. The class with the highest probability is then chosen as the most likely outcome for the given observations.\n",
"\n",
"Let's see this in action by making both hard class predictions and probabilities."
],
"metadata": {
"id": "43t7vz8vMJtW"
}
},
{
"cell_type": "code",
"execution_count": 13,
"source": [
"# Make hard class prediction and probabilities\n",
"results_prob <- cuisines_test %>%\n",
" select(cuisine) %>% \n",
" bind_cols(mr_fit %>% predict(new_data = cuisines_test)) %>% \n",
" bind_cols(mr_fit %>% predict(new_data = cuisines_test, type = \"prob\"))\n",
"\n",
"# Print out results\n",
"results_prob %>% \n",
" slice_head(n = 5)"
],
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
" cuisine .pred_class .pred_chinese .pred_indian .pred_japanese .pred_korean\n",
"1 indian thai 1.551259e-03 0.4587877 5.988039e-04 2.428503e-04\n",
"2 indian indian 2.637133e-05 0.9999488 6.648651e-07 2.259993e-05\n",
"3 indian indian 1.049433e-03 0.9909982 1.060937e-03 1.644947e-05\n",
"4 indian indian 6.237482e-02 0.4763035 9.136702e-02 3.660913e-01\n",
"5 indian indian 1.431745e-02 0.9418551 2.945239e-02 8.721782e-03\n",
" .pred_thai \n",
"1 5.388194e-01\n",
"2 1.577948e-06\n",
"3 6.874989e-03\n",
"4 3.863391e-03\n",
"5 5.653283e-03"
],
"text/markdown": [
"\n",
"A tibble: 5 × 7\n",
"\n",
"| cuisine &lt;fct&gt; | .pred_class &lt;fct&gt; | .pred_chinese &lt;dbl&gt; | .pred_indian &lt;dbl&gt; | .pred_japanese &lt;dbl&gt; | .pred_korean &lt;dbl&gt; | .pred_thai &lt;dbl&gt; |\n",
"|---|---|---|---|---|---|---|\n",
"| indian | thai | 1.551259e-03 | 0.4587877 | 5.988039e-04 | 2.428503e-04 | 5.388194e-01 |\n",
"| indian | indian | 2.637133e-05 | 0.9999488 | 6.648651e-07 | 2.259993e-05 | 1.577948e-06 |\n",
"| indian | indian | 1.049433e-03 | 0.9909982 | 1.060937e-03 | 1.644947e-05 | 6.874989e-03 |\n",
"| indian | indian | 6.237482e-02 | 0.4763035 | 9.136702e-02 | 3.660913e-01 | 3.863391e-03 |\n",
"| indian | indian | 1.431745e-02 | 0.9418551 | 2.945239e-02 | 8.721782e-03 | 5.653283e-03 |\n",
"\n"
],
"text/latex": [
"A tibble: 5 × 7\n",
"\\begin{tabular}{lllllll}\n",
" cuisine & .pred\\_class & .pred\\_chinese & .pred\\_indian & .pred\\_japanese & .pred\\_korean & .pred\\_thai\\\\\n",
" <fct> & <fct> & <dbl> & <dbl> & <dbl> & <dbl> & <dbl>\\\\\n",
"\\hline\n",
"\t indian & thai & 1.551259e-03 & 0.4587877 & 5.988039e-04 & 2.428503e-04 & 5.388194e-01\\\\\n",
"\t indian & indian & 2.637133e-05 & 0.9999488 & 6.648651e-07 & 2.259993e-05 & 1.577948e-06\\\\\n",
"\t indian & indian & 1.049433e-03 & 0.9909982 & 1.060937e-03 & 1.644947e-05 & 6.874989e-03\\\\\n",
"\t indian & indian & 6.237482e-02 & 0.4763035 & 9.136702e-02 & 3.660913e-01 & 3.863391e-03\\\\\n",
"\t indian & indian & 1.431745e-02 & 0.9418551 & 2.945239e-02 & 8.721782e-03 & 5.653283e-03\\\\\n",
"\\end{tabular}\n"
],
"text/html": [
"<table class=\"dataframe\">\n",
"<caption>A tibble: 5 × 7</caption>\n",
"<thead>\n",
"\t<tr><th scope=col>cuisine</th><th scope=col>.pred_class</th><th scope=col>.pred_chinese</th><th scope=col>.pred_indian</th><th scope=col>.pred_japanese</th><th scope=col>.pred_korean</th><th scope=col>.pred_thai</th></tr>\n",
"\t<tr><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;dbl&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><td>indian</td><td>thai </td><td>1.551259e-03</td><td>0.4587877</td><td>5.988039e-04</td><td>2.428503e-04</td><td>5.388194e-01</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td><td>2.637133e-05</td><td>0.9999488</td><td>6.648651e-07</td><td>2.259993e-05</td><td>1.577948e-06</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td><td>1.049433e-03</td><td>0.9909982</td><td>1.060937e-03</td><td>1.644947e-05</td><td>6.874989e-03</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td><td>6.237482e-02</td><td>0.4763035</td><td>9.136702e-02</td><td>3.660913e-01</td><td>3.863391e-03</td></tr>\n",
"\t<tr><td>indian</td><td>indian</td><td>1.431745e-02</td><td>0.9418551</td><td>2.945239e-02</td><td>8.721782e-03</td><td>5.653283e-03</td></tr>\n",
"</tbody>\n",
"</table>\n"
]
},
"metadata": {}
}
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 248
},
"id": "xdKNs-ZPMTJL",
"outputId": "68f6ac5a-725a-4eff-9ea6-481fef00e008"
}
},
{
"cell_type": "markdown",
"source": [
"Much better!\n",
"\n",
"✅ Can you explain why the model is pretty sure that the first observation is Thai?\n",
"\n",
"## **🚀Challenge**\n",
"\n",
"In this lesson, you used your cleaned data to build a machine learning model that can predict a national cuisine based on a series of ingredients. Take some time to read through the [many options](https://www.tidymodels.org/find/parsnip/#models) Tidymodels provides to classify data and [other ways](https://parsnip.tidymodels.org/articles/articles/Examples.html#multinom_reg-models) to fit multinomial regression.\n",
"\n",
"#### THANK YOU TO:\n",
"\n",
"[`Allison Horst`](https://twitter.com/allison_horst/) for creating the amazing illustrations that make R more welcoming and engaging. Find more illustrations at her [gallery](https://www.google.com/url?q=https://github.com/allisonhorst/stats-illustrations&sa=D&source=editors&ust=1626380772530000&usg=AOvVaw3zcfyCizFQZpkSLzxiiQEM).\n",
"\n",
"[Cassie Breviu](https://www.twitter.com/cassieview) and [Jen Looper](https://www.twitter.com/jenlooper) for creating the original Python version of this module ♥️\n",
"\n",
"<br>\n",
"Would have thrown in some jokes but I donut understand food puns 😅.\n",
"\n",
"<br>\n",
"\n",
"Happy Learning,\n",
"\n",
"[Eric](https://twitter.com/ericntay), Gold Microsoft Learn Student Ambassador.\n"
],
"metadata": {
"id": "2tWVHMeLMYdM"
}
}
]
}