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/translations/pcm/4-Classification/2-Classifiers-1/solution/R/lesson_11-R.ipynb

1302 lines
76 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"
},
"coopTranslator": {
"original_hash": "6ea6a5171b1b99b7b5a55f7469c048d2",
"translation_date": "2025-11-18T19:22:32+00:00",
"source_file": "4-Classification/2-Classifiers-1/solution/R/lesson_11-R.ipynb",
"language_code": "pcm"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"# Build classification model: Sweet Asian and Indian Foods\n"
],
"metadata": {
"id": "zs2woWv_HoE8"
}
},
{
"cell_type": "markdown",
"source": [
"## Cuisine classifiers 1\n",
"\n",
"For dis lesson, we go look different classifiers wey fit *predict one national cuisine based on di ingredients wey dey.* As we dey do am, we go sabi more about how algorithms fit dey use for classification work.\n",
"\n",
"### [**Pre-lecture quiz**](https://gray-sand-07a10f403.1.azurestaticapps.net/quiz/21/)\n",
"\n",
"### **Preparation**\n",
"\n",
"Dis lesson dey build on top di [previous lesson](https://github.com/microsoft/ML-For-Beginners/blob/main/4-Classification/1-Introduction/solution/lesson_10-R.ipynb) wey we:\n",
"\n",
"- Do small introduction to classifications using one dataset about di sweet cuisines wey dey Asia and India 😋.\n",
"\n",
"- Check some [dplyr verbs](https://dplyr.tidyverse.org/) to clean and prepare our data.\n",
"\n",
"- Make fine visualizations using ggplot2.\n",
"\n",
"- Show how to handle unbalanced data by preprocessing am using [recipes](https://recipes.tidymodels.org/articles/Simple_Example.html).\n",
"\n",
"- Show how to `prep` and `bake` our recipe to make sure say e go work well.\n",
"\n",
"#### **Prerequisite**\n",
"\n",
"For dis lesson, we go need di following packages to clean, prepare and visualize our data:\n",
"\n",
"- `tidyverse`: Di [tidyverse](https://www.tidyverse.org/) na [collection of R packages](https://www.tidyverse.org/packages) wey dey make data science fast, easy and enjoyable!\n",
"\n",
"- `tidymodels`: Di [tidymodels](https://www.tidymodels.org/) framework na [collection of packages](https://www.tidymodels.org/packages/) for modeling and machine learning.\n",
"\n",
"- `themis`: Di [themis package](https://themis.tidymodels.org/) dey provide Extra Recipes Steps to handle unbalanced data.\n",
"\n",
"- `nnet`: Di [nnet package](https://cran.r-project.org/web/packages/nnet/nnet.pdf) dey provide functions to estimate feed-forward neural networks wey get one hidden layer, and for multinomial logistic regression models.\n",
"\n",
"You fit install dem like dis:\n"
],
"metadata": {
"id": "iDFOb3ebHwQC"
}
},
{
"cell_type": "markdown",
"source": [
"`install.packages(c(\"tidyverse\", \"tidymodels\", \"DataExplorer\", \"here\"))`\n",
"\n",
"Or, di script wey dey below go check if you get di packages wey you need to finish dis module, and e go install dem for you if dem no dey.\n"
],
"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": [
"Make we start dey waka sharp sharp!\n",
"\n",
"## 1. Share di data into training and test sets.\n",
"\n",
"We go start by pick some steps from di lesson wey we do before.\n",
"\n",
"### Comot di common ingredients wey dey cause wahala between different cuisines, use `dplyr::select()`.\n",
"\n",
"Everybody like 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, we go share di data so 70% go enter training and 30% go enter testing. We go also use one `stratification` method wen we dey share di data to `keep di proportion of each cuisine` for di training and validation datasets.\n",
"\n",
"[rsample](https://rsample.tidymodels.org/), one package for Tidymodels, dey provide infrastructure for beta data sharing and resampling:\n"
],
"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. How to handle data wey no balance well\n",
"\n",
"As you fit don notice for di original data set and di one wey we dey use train, di number of cuisines no dey balance well. Korean cuisines dey *almost* 3 times pass Thai cuisines. Data wey no balance well fit affect how di model go perform. Many models dey work well when di number of observations dey equal, so dem dey struggle when di data no balance.\n",
"\n",
"Two main ways dey to handle data wey no balance well:\n",
"\n",
"- Add more observations to di class wey get small number: `Over-sampling`, like using SMOTE algorithm wey dey create new examples for di class wey get small number by using di nearest neighbors of di cases.\n",
"\n",
"- Remove some observations from di class wey get plenty number: `Under-sampling`\n",
"\n",
"For di lesson wey we do before, we show how to handle data wey no balance well by using `recipe`. Recipe na like plan wey dey describe di steps wey you go follow for di data set to make am ready for analysis. For our case, we wan make di number of cuisines for our `training set` dey equal. Make we start.\n"
],
"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 fit go ahead confirm (use prep+bake) say di recipe go work as you dey expect am - all di cuisine labels get `559` observations.\n",
"\n",
"Since we go use dis recipe as preprocessor for modeling, `workflow()` go do all di prep and bake for us, so we no go need manually estimate di recipe.\n",
"\n",
"Now we don ready to train model 👩‍💻👨‍💻!\n",
"\n",
"## 3. Choose your classifier\n",
"\n",
"<p >\n",
" <img src=\"../../../../../../translated_images/parsnip.cd2ce92622976502.pcm.jpg\"\n",
" width=\"600\"/>\n",
" <figcaption>Artwork by @allison_horst</figcaption>\n"
],
"metadata": {
"id": "NBL3PqIWJBBB"
}
},
{
"cell_type": "markdown",
"source": [
"Now we gatz decide which algorithm go work well for di job 🤔.\n",
"\n",
"For Tidymodels, di [`parsnip package`](https://parsnip.tidymodels.org/index.html) dey give one kind steady way to take work wit models for different engines (packages). Abeg check di parsnip documentation to see [model types & engines](https://www.tidymodels.org/find/parsnip/#models) and di [model arguments](https://www.tidymodels.org/find/parsnip/#model-args) wey dem get. Di options plenty well well for first look. For example, di following methods all dey use classification techniques:\n",
"\n",
"- C5.0 Rule-Based Classification Models\n",
"\n",
"- Flexible Discriminant Models\n",
"\n",
"- Linear Discriminant Models\n",
"\n",
"- Regularized Discriminant Models\n",
"\n",
"- Logistic Regression Models\n",
"\n",
"- Multinomial Regression Models\n",
"\n",
"- Naive Bayes Models\n",
"\n",
"- Support Vector Machines\n",
"\n",
"- Nearest Neighbors\n",
"\n",
"- Decision Trees\n",
"\n",
"- Ensemble methods\n",
"\n",
"- Neural Networks\n",
"\n",
"Di list no dey finish!\n",
"\n",
"### **Which classifier we go choose?**\n",
"\n",
"So, which classifier you go pick? Sometimes, to try plenty classifiers and see which one go give better result na one way to test.\n",
"\n",
"> AutoML dey solve dis wahala well by running all di comparisons for cloud, so you fit choose di best algorithm for your data. Try am [here](https://docs.microsoft.com/learn/modules/automate-model-selection-with-azure-automl/?WT.mc_id=academic-77952-leestott)\n",
"\n",
"Di classifier wey you go choose sef dey depend on di kind problem wey you wan solve. For example, if di outcome fit get `more than two classes`, like for our case, you gatz use `multiclass classification algorithm` instead of `binary classification.`\n",
"\n",
"### **Better way**\n",
"\n",
"One better way wey pass to dey guess anyhow na to follow di ideas wey dey for dis downloadable [ML Cheat sheet](https://docs.microsoft.com/azure/machine-learning/algorithm-cheat-sheet?WT.mc_id=academic-77952-leestott). For here, we go see say for our multiclass problem, we get some options:\n",
"\n",
"<p >\n",
" <img src=\"../../../../../../translated_images/cheatsheet.07a475ea444d2223.pcm.png\"\n",
" width=\"500\"/>\n",
" <figcaption>One part of Microsoft's Algorithm Cheat Sheet, wey dey show multiclass classification options</figcaption>\n"
],
"metadata": {
"id": "a6DLAZ3vJZ14"
}
},
{
"cell_type": "markdown",
"source": [
"### **Reasoning**\n",
"\n",
"Make we try reason how we fit take handle different ways wey go work based on di constraints wey we get:\n",
"\n",
"- **Deep Neural networks too heavy**. Based on say our dataset dey clean but e no too big, and we dey run di training locally for notebooks, deep neural networks go too heavy for dis kain task.\n",
"\n",
"- **No be two-class classifier**. We no dey use two-class classifier, so dat one don already rule out one-vs-all.\n",
"\n",
"- **Decision tree or logistic regression fit work**. Decision tree fit work, or multinomial regression/multiclass logistic regression for multiclass data.\n",
"\n",
"- **Multiclass Boosted Decision Trees dey solve different problem**. Di multiclass boosted decision tree dey best for nonparametric tasks, like tasks wey dem design to build rankings, so e no go help us for here.\n",
"\n",
"Normally, before we go start to use more complex machine learning models like ensemble methods, e good make we first build di simplest model wey we fit use to understand wetin dey happen. So for dis lesson, we go start with `multinomial regression` model.\n",
"\n",
"> Logistic regression na one technique wey dem dey use when di outcome variable na categorical (or nominal). For Binary logistic regression, di number of outcome variables na two, but for multinomial logistic regression, di number of outcome variables dey pass two. Check [Advanced Regression Methods](https://bookdown.org/chua/ber642_advanced_regression/multinomial-logistic-regression.html) if you wan read more.\n",
"\n",
"## 4. Train and evaluate Multinomial logistic regression model.\n",
"\n",
"For Tidymodels, `parsnip::multinom_reg()`, na di function wey define model wey dey use linear predictors to predict multiclass data with di multinomial distribution. Check `?multinom_reg()` to see di different ways/engines wey you fit use to fit dis model.\n",
"\n",
"For dis example, we go fit Multinomial regression model through di default [nnet](https://cran.r-project.org/web/packages/nnet/nnet.pdf) engine.\n",
"\n",
"> I just pick one value for `penalty` randomly. E get better ways to choose dis value, like using `resampling` and `tuning` di model, wey we go discuss later.\n",
">\n",
"> Check [Tidymodels: Get Started](https://www.tidymodels.org/start/tuning/) if you wan sabi more about how to tune model hyperparameters.\n"
],
"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": [
"Nice work 🥳! Now wey we don get recipe and model specification, we go need find way to join dem together inside one object wey go first preprocess di data, den fit di model on top di preprocessed data, and e go still allow for post-processing activities if we wan do am. For Tidymodels, dis object wey dey make sense na [`workflow`](https://workflows.tidymodels.org/) and e dey carry all your modeling components well well! Na wetin we go call *pipelines* for *Python*.\n",
"\n",
"So make we package everything inside workflow!📦\n"
],
"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 👌👌! **`workflow()`** fit dey work like how model fit dey work. So, time to train model!\n"
],
"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 dey show the coefficients wey the model learn during training.\n",
"\n",
"### Check How the Model Perform\n",
"\n",
"E don reach time to see how the model take do work 📏 by testing am for test set! Make we start by making predictions for the test set.\n"
],
"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": [
"Good job! For Tidymodels, to check how model dey perform, we fit use [yardstick](https://yardstick.tidymodels.org/) - na package wey dem dey use measure how model dey work well wit performance metrics. As we do for our logistic regression lesson, make we start by calculate confusion matrix.\n"
],
"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": [
"Wen you dey deal wit plenty classes, e dey make sense to see am as heat map, like dis:\n"
],
"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/pAAADAFBMVEUAAAABAQECAgIDAwMEBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUWFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJycoKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ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z5h17blq1HP3tjwS+iXeFeEpDyuzpIbd2c5HYHJK+f23Hjj74W4fL2noCU32UgNf6XZyE19a/J61UY4fL2noCU32UgHeFvd4V0tD/sx0OcizjL+V0G0tH+FkjRxVnO73aCdOpJ/vY0IEUQZzm/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 dark squares wey dey for the confusion matrix plot show say plenty cases dey there, and you fit see one diagonal line of dark squares wey dey show cases wey the predicted label and the actual label match.\n",
"\n",
"Make we calculate summary statistics for the confusion matrix now.\n"
],
"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 look metrics like accuracy, sensitivity, ppv, we no dey too bad for start 🥳!\n",
"\n",
"## 4. Dig Deep\n",
"\n",
"Make we ask one small question: Wetin be the criteria wey dem dey use to choose one type of food as the predicted result?\n",
"\n",
"Well, Statistical machine learning algorithms, like logistic regression, dey work based on `probability`; so wetin classifier dey predict na probability distribution for different possible results. The class wey get the highest probability na im dem go choose as the most likely result for the observations wey dem give.\n",
"\n",
"Make we see how e dey work by doing both hard class predictions and probabilities.\n"
],
"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 beta!\n",
"\n",
"✅ Fit explain why di model sure sey di first observation na Thai?\n",
"\n",
"## **🚀Challenge**\n",
"\n",
"For dis lesson, you use di clean data wey you get to build machine learning model wey fit predict national cuisine based on di ingredients wey dey. Take small time read di [plenty options](https://www.tidymodels.org/find/parsnip/#models) wey Tidymodels get to classify data and [other ways](https://parsnip.tidymodels.org/articles/articles/Examples.html#multinom_reg-models) wey you fit use fit multinomial regression.\n",
"\n",
"#### THANK YOU TO:\n",
"\n",
"[`Allison Horst`](https://twitter.com/allison_horst/) for di amazing illustrations wey she create wey dey make R more friendly and interesting. You fit find more illustrations for 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 di original Python version of dis module wey dem create ♥️\n",
"\n",
"<br>\n",
"I for add some jokes but I no sabi food puns 😅.\n",
"\n",
"<br>\n",
"\n",
"Enjoy di learning,\n",
"\n",
"[Eric](https://twitter.com/ericntay), Gold Microsoft Learn Student Ambassador.\n"
],
"metadata": {
"id": "2tWVHMeLMYdM"
}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n\n<!-- CO-OP TRANSLATOR DISCLAIMER START -->\n**Disclaimer**: \nDis dokyument don use AI translation service [Co-op Translator](https://github.com/Azure/co-op-translator) do di translation. Even as we dey try make am accurate, abeg make you sabi say machine translation fit get mistake or no dey correct well. Di original dokyument for di native language na di main source wey you go trust. For important information, e better make professional human translator check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.\n<!-- CO-OP TRANSLATOR DISCLAIMER END -->\n"
]
}
]
}