{"cells":[{"cell_type":"markdown","id":"189e8d4d-d83c-46a1-be6f-4d5014769cae","metadata":{},"outputs":[],"source":["\u003ccenter\u003e\n","    \u003ca href=\"https://cognitiveclass.ai\"\u003e\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%204/images/IDSNlogo.png\" width=\"300\"\u003e \u003c/a\u003e\n","\u003c/center\u003e\n","\n","\n","# From Modeling to Evaluation\n","\n","\n","Estimated time needed: **20** minutes\n","    \n","\n","## Objectives\n","\n","After completing this lab you will be able to:\n","\n","* Create Models\n","* Evaluate the models\n"]},{"cell_type":"markdown","id":"945c93e9-824c-4578-a0e7-e1a4619bf0b3","metadata":{},"outputs":[],"source":["## Table of Contents\n","\n","\n","\u003cdiv class=\"alert alert-block alert-info\" style=\"margin-top: 20px\"\u003e\n","\n","1. [Recap](#0)\u003cbr\u003e\n","2. [Data Modeling](#2)\u003cbr\u003e\n","3. [Model Evaluation](#4)\u003cbr\u003e\n","\u003c/div\u003e\n","\u003chr\u003e\n"]},{"cell_type":"markdown","id":"e15f4ca4-92bf-4a3f-9bc0-6a1cc438394a","metadata":{},"outputs":[],"source":["# Recap \u003ca id=\"0\"\u003e\u003c/a\u003e\n","\n","In Lab **From Understanding to Preparation**, we explored the data and prepared it for modeling.\n"]},{"cell_type":"markdown","id":"85c9289b-5649-475e-9046-c038609f521e","metadata":{},"outputs":[],"source":["This recipe data was compiled by a researcher named Yong-Yeol Ahn, who scraped tens of thousands of food recipes (cuisines and ingredients) from three different websites, namely:\n"]},{"cell_type":"markdown","id":"dfbdf53c-7e9c-4491-aa1e-92ebfdc9ad0d","metadata":{},"outputs":[],"source":["\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%202/images/lab2_fig3_allrecipes.png\" width=\"500\"\u003e\n","\u003cdiv align=\"center\"\u003e\n","www.allrecipes.com\n","\u003c/div\u003e\n","\u003cbr/\u003e\u003cbr/\u003e\n","\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%202/images/lab2_fig4_epicurious.png\" width=\"500\"\u003e\n","\u003cdiv align=\"center\"\u003e\n","www.epicurious.com\n","\u003c/div\u003e\n","\u003cbr/\u003e\u003cbr/\u003e\n","\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%202/images/lab2_fig5_menupan.png\" width=\"500\"\u003e\n","\u003cdiv align=\"center\"\u003e\n","www.menupan.com\n","\u003c/div\u003e\n","\u003cbr/\u003e\u003cbr/\u003e\n"]},{"cell_type":"markdown","id":"1a7a5b45-8713-44e4-b8fb-967eec751553","metadata":{},"outputs":[],"source":["For more information on Yong-Yeol Ahn and his research, you can read his paper on [Flavor Network and the Principles of Food Pairing](http://yongyeol.com/papers/ahn-flavornet-2011.pdf).\n"]},{"cell_type":"markdown","id":"4d03c526-def4-4efd-b7fa-dc0cadbea1e6","metadata":{},"outputs":[],"source":["\u003cstrong\u003eImportant note:\u003c/strong\u003e Please note that you are not expected to know how to program in Python. The following code is meant to illustrate the stage of data collection, so it is totally fine if you do not understand the individual lines of code. You can access a full course in this certificate on programming in Python, \u003ca href=\"http://cocl.us/PY0101EN_DS0103EN_LAB4_PYTHON_edX\"\u003ePython for Data Science\u003c/a\u003e, which will teach you how to program in Python if you decide to complete this certificate.\n"]},{"cell_type":"markdown","id":"1ccf0460-40a6-416d-b3fd-e48cf357ab1d","metadata":{},"outputs":[],"source":["### Using this notebook:\n","\n","To run any of the following cells of code, you can type **Shift + Enter** to excute the code in a cell.\n"]},{"cell_type":"markdown","id":"49d81a0c-29fe-49e8-9973-a9370b45c929","metadata":{},"outputs":[],"source":["First, download the library and dependencies that you will need to run this lab.\n"]},{"cell_type":"code","id":"495404da-bad1-4b1f-9129-539d333210d2","metadata":{},"outputs":[],"source":["import pandas as pd # import library to read data into dataframe\npd.set_option(\"display.max_columns\", None)\nimport numpy as np # import numpy library\nimport re # import library for regular expression\nimport random # library for random number generation"]},{"cell_type":"markdown","id":"d0221acf-ef03-4390-a886-804aeee47c7d","metadata":{},"outputs":[],"source":["We already placed the data on an IBM server for your convenience, so download the data from the server and read the data into a dataframe called **recipes**.\n"]},{"cell_type":"code","id":"557c577e-3083-4d5f-be99-f2580f06c2f6","metadata":{},"outputs":[],"source":["recipes = pd.read_csv(\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%202/recipes.csv\")\n\nprint(\"Data read into dataframe!\") # takes about 30 seconds"]},{"cell_type":"markdown","id":"602eceaf-3f1d-4f6b-bf4c-898c55b250c5","metadata":{},"outputs":[],"source":["Next, repeat the preprocessing steps that you implemented in the lab **From Understanding to Preparation** to prepare the data for modeling. For more details on preparing the data, please refer to the Lab **From Understanding to Preparation**.\n"]},{"cell_type":"code","id":"281a9a18-9307-44bc-a124-26b39dd963e7","metadata":{},"outputs":[],"source":["# fix name of the column displaying the cuisine\ncolumn_names = recipes.columns.values\ncolumn_names[0] = \"cuisine\"\nrecipes.columns = column_names\n\n# convert cuisine names to lower case\nrecipes[\"cuisine\"] = recipes[\"cuisine\"].str.lower()\n\n# make the cuisine names consistent\nrecipes.loc[recipes[\"cuisine\"] == \"austria\", \"cuisine\"] = \"austrian\"\nrecipes.loc[recipes[\"cuisine\"] == \"belgium\", \"cuisine\"] = \"belgian\"\nrecipes.loc[recipes[\"cuisine\"] == \"china\", \"cuisine\"] = \"chinese\"\nrecipes.loc[recipes[\"cuisine\"] == \"canada\", \"cuisine\"] = \"canadian\"\nrecipes.loc[recipes[\"cuisine\"] == \"netherlands\", \"cuisine\"] = \"dutch\"\nrecipes.loc[recipes[\"cuisine\"] == \"france\", \"cuisine\"] = \"french\"\nrecipes.loc[recipes[\"cuisine\"] == \"germany\", \"cuisine\"] = \"german\"\nrecipes.loc[recipes[\"cuisine\"] == \"india\", \"cuisine\"] = \"indian\"\nrecipes.loc[recipes[\"cuisine\"] == \"indonesia\", \"cuisine\"] = \"indonesian\"\nrecipes.loc[recipes[\"cuisine\"] == \"iran\", \"cuisine\"] = \"iranian\"\nrecipes.loc[recipes[\"cuisine\"] == \"italy\", \"cuisine\"] = \"italian\"\nrecipes.loc[recipes[\"cuisine\"] == \"japan\", \"cuisine\"] = \"japanese\"\nrecipes.loc[recipes[\"cuisine\"] == \"israel\", \"cuisine\"] = \"jewish\"\nrecipes.loc[recipes[\"cuisine\"] == \"korea\", \"cuisine\"] = \"korean\"\nrecipes.loc[recipes[\"cuisine\"] == \"lebanon\", \"cuisine\"] = \"lebanese\"\nrecipes.loc[recipes[\"cuisine\"] == \"malaysia\", \"cuisine\"] = \"malaysian\"\nrecipes.loc[recipes[\"cuisine\"] == \"mexico\", \"cuisine\"] = \"mexican\"\nrecipes.loc[recipes[\"cuisine\"] == \"pakistan\", \"cuisine\"] = \"pakistani\"\nrecipes.loc[recipes[\"cuisine\"] == \"philippines\", \"cuisine\"] = \"philippine\"\nrecipes.loc[recipes[\"cuisine\"] == \"scandinavia\", \"cuisine\"] = \"scandinavian\"\nrecipes.loc[recipes[\"cuisine\"] == \"spain\", \"cuisine\"] = \"spanish_portuguese\"\nrecipes.loc[recipes[\"cuisine\"] == \"portugal\", \"cuisine\"] = \"spanish_portuguese\"\nrecipes.loc[recipes[\"cuisine\"] == \"switzerland\", \"cuisine\"] = \"swiss\"\nrecipes.loc[recipes[\"cuisine\"] == \"thailand\", \"cuisine\"] = \"thai\"\nrecipes.loc[recipes[\"cuisine\"] == \"turkey\", \"cuisine\"] = \"turkish\"\nrecipes.loc[recipes[\"cuisine\"] == \"vietnam\", \"cuisine\"] = \"vietnamese\"\nrecipes.loc[recipes[\"cuisine\"] == \"uk-and-ireland\", \"cuisine\"] = \"uk-and-irish\"\nrecipes.loc[recipes[\"cuisine\"] == \"irish\", \"cuisine\"] = \"uk-and-irish\"\n\n\n# remove data for cuisines with \u003c 50 recipes:\nrecipes_counts = recipes[\"cuisine\"].value_counts()\ncuisines_indices = recipes_counts \u003e 50\n\ncuisines_to_keep = list(np.array(recipes_counts.index.values)[np.array(cuisines_indices)])\nrecipes = recipes.loc[recipes[\"cuisine\"].isin(cuisines_to_keep)]\n\n# convert all Yes's to 1's and the No's to 0's\nrecipes = recipes.replace(to_replace=\"Yes\", value=1)\nrecipes = recipes.replace(to_replace=\"No\", value=0)"]},{"cell_type":"markdown","id":"ac7ad27b-dc48-4ebb-ae58-a85268379c2a","metadata":{},"outputs":[],"source":["\u003chr\u003e\n"]},{"cell_type":"markdown","id":"373b1a43-537f-4313-8673-e5b97ea45e29","metadata":{},"outputs":[],"source":["# Data Modeling \u003ca id=\"2\"\u003e\u003c/a\u003e\n"]},{"cell_type":"markdown","id":"ec67650a-c6ed-4325-a7da-7013d7a72b9e","metadata":{},"outputs":[],"source":["\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%204/images/flowchart_data_modeling.png\" width=\"500\"\u003e\n"]},{"cell_type":"markdown","id":"d8495a2d-d64a-47cd-8da7-18b0f4474a9f","metadata":{},"outputs":[],"source":["Now download and install more libraries and dependencies to build decision trees.\n"]},{"cell_type":"code","id":"4dc889b7-fd90-4131-9b83-529640d21a85","metadata":{},"outputs":[],"source":["# import decision trees scikit-learn libraries\n%matplotlib inline\nfrom sklearn import tree\nfrom sklearn.metrics import accuracy_score, confusion_matrix\n\nimport matplotlib.pyplot as plt\n\n!conda install python-graphviz --yes\nimport graphviz\n\nfrom sklearn.tree import export_graphviz\n\nimport itertools"]},{"cell_type":"markdown","id":"dda0fe8e-227e-473e-81b2-fdba606046c5","metadata":{},"outputs":[],"source":["Check the data again!\n"]},{"cell_type":"code","id":"e3bb7338-e017-4083-9f51-c2d4e1fd498c","metadata":{},"outputs":[],"source":["recipes.head()"]},{"cell_type":"markdown","id":"10e3ac64-1afe-4239-907c-124b88eb3f77","metadata":{},"outputs":[],"source":["## [bamboo_tree] Only Asian and Indian Cuisines\n","\n","Here, you are creating a decision tree for the recipes for just some of the Asian (Korean, Japanese, Chinese, Thai) and Indian cuisines. The reason this action is because the decision tree does not run well when the data is biased towards one cuisine or a group of cuisines, such as in this case, American cuisines. We can exclude the American cuisines from our analysis or just build decision trees for different subsets of the data. Let's go with the second solution.\n"]},{"cell_type":"markdown","id":"5abd16c6-4276-4f0c-8007-f92d19ec3807","metadata":{},"outputs":[],"source":["Let's build our decision tree using the data pertaining to the Asian and Indian cuisines and name our decision tree *bamboo_tree*.\n"]},{"cell_type":"code","id":"72efd356-4eba-4b32-a5f1-248a29bf5e77","metadata":{},"outputs":[],"source":["# select subset of cuisines\nasian_indian_recipes = recipes[recipes.cuisine.isin([\"korean\", \"japanese\", \"chinese\", \"thai\", \"indian\"])]\ncuisines = asian_indian_recipes[\"cuisine\"]\ningredients = asian_indian_recipes.iloc[:,1:]\n\nbamboo_tree = tree.DecisionTreeClassifier(max_depth=3)\nbamboo_tree.fit(ingredients, cuisines)\n\nprint(\"Decision tree model saved to bamboo_tree!\")"]},{"cell_type":"markdown","id":"31c8fd15-b3d9-40f2-ab5d-0c968388b8da","metadata":{},"outputs":[],"source":["Let's plot the decision tree and examine what the decision tree looks like.\n"]},{"cell_type":"code","id":"2f63c9bd-4f38-43ba-85dc-81f786144380","metadata":{},"outputs":[],"source":["export_graphviz(bamboo_tree,\n                feature_names=list(ingredients.columns.values),\n                out_file=\"bamboo_tree.dot\",\n                class_names=np.unique(cuisines),\n                filled=True,\n                node_ids=True,\n                special_characters=True,\n                impurity=False,\n                label=\"all\",\n                leaves_parallel=False)\n\nwith open(\"bamboo_tree.dot\") as bamboo_tree_image:\n    bamboo_tree_graph = bamboo_tree_image.read()\ngraphviz.Source(bamboo_tree_graph)"]},{"cell_type":"markdown","id":"7cedf7fb-e870-4822-8677-b3404cda962c","metadata":{},"outputs":[],"source":["The decision tree learned:\n","* If a recipe contains *cumin* and *fish* and **no** *yoghurt*, then it is most likely a **Thai** recipe.\n","* If a recipe contains *cumin* but **no** *fish* and **no** *soy_sauce*, then it is most likely an **Indian** recipe.\n"]},{"cell_type":"markdown","id":"0e478e1b-6e98-4e84-b5eb-1c275b485d74","metadata":{},"outputs":[],"source":["You can analyze the remaining branches of the tree to come up with similar rules for determining the cuisine of different recipes. \n"]},{"cell_type":"markdown","id":"5ed800d2-c5fd-49c5-bbd0-8ffde2a01fcf","metadata":{},"outputs":[],"source":["Feel free to select another subset of cuisines and build a decision tree of their recipes. You can select some European cuisines and build decision trees to explore the ingredients that differentiate those cuisines.\n"]},{"cell_type":"markdown","id":"d7033c1d-7823-4a3b-a4ee-fac1d06ebafa","metadata":{},"outputs":[],"source":["# Model Evaluation \u003ca id=\"4\"\u003e\u003c/a\u003e\n"]},{"cell_type":"markdown","id":"c46b886f-0931-4c11-a376-7d02c9df9966","metadata":{},"outputs":[],"source":["\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%204/images/flowchart_evaluation.png\" width=\"500\"\u003e\n"]},{"cell_type":"markdown","id":"55066a9f-f1df-4a3f-a83a-6c7244d81619","metadata":{},"outputs":[],"source":["Next, to evaluate our model of Asian and Indian cuisines, you will split the data set into a training set and a test set. You will build the decision tree using the training set. Then, you will test the model on the test set and compare the cuisines that the model predicts to the actual cuisines. \n"]},{"cell_type":"markdown","id":"76be166f-05bb-461e-b48c-d294b1db83d0","metadata":{},"outputs":[],"source":["First create a new dataframe using only the data pertaining to the Asian and the Indian cuisines, and let's call the new dataframe **bamboo**.\n"]},{"cell_type":"code","id":"62ade591-f49d-44f8-a12a-e5a7c5636b2a","metadata":{},"outputs":[],"source":["bamboo = recipes[recipes.cuisine.isin([\"korean\", \"japanese\", \"chinese\", \"thai\", \"indian\"])]"]},{"cell_type":"markdown","id":"d0a8746d-da5b-4fb7-90ac-dfb0b8c56fe9","metadata":{},"outputs":[],"source":["Now see how many recipes exist for each cuisine.\n"]},{"cell_type":"code","id":"b5c6779d-4350-4fd3-a75a-451abac23123","metadata":{},"outputs":[],"source":["bamboo[\"cuisine\"].value_counts()"]},{"cell_type":"markdown","id":"a1cbb358-8b4c-4345-b4ae-8d2e26b65039","metadata":{},"outputs":[],"source":["Let's remove 30 recipes from each cuisine to use as the test set, and let's name this test set **bamboo_test**.\n"]},{"cell_type":"code","id":"bdcdd3ea-3691-4818-801c-459474d7c0fe","metadata":{},"outputs":[],"source":["# set sample size\nsample_n = 30"]},{"cell_type":"markdown","id":"841a06ec-d5c8-499e-a0b4-db7a1d2db792","metadata":{},"outputs":[],"source":["Next, create a dataframe containing 30 recipes from each cuisine, selected randomly.\n"]},{"cell_type":"code","id":"ef110269-6383-44a6-b7d5-f6f20d4239e3","metadata":{},"outputs":[],"source":["# take 30 recipes from each cuisine\nrandom.seed(1234) # set random seed\nbamboo_test = bamboo.groupby(\"cuisine\", group_keys=False).apply(lambda x: x.sample(sample_n))\n\nbamboo_test_ingredients = bamboo_test.iloc[:,1:] # ingredients\nbamboo_test_cuisines = bamboo_test[\"cuisine\"] # corresponding cuisines or labels"]},{"cell_type":"markdown","id":"5a191870-507d-410d-86a0-b0fbaa5b7abd","metadata":{},"outputs":[],"source":["Check that there are 30 recipes for each cuisine.\n"]},{"cell_type":"code","id":"bccf44ea-8e66-4f58-a8b9-617f924dcd4a","metadata":{},"outputs":[],"source":["# check that we have 30 recipes from each cuisine\nbamboo_test[\"cuisine\"].value_counts()"]},{"cell_type":"markdown","id":"3a4c5aef-3adf-41bd-8b1b-bc480d97c3d5","metadata":{},"outputs":[],"source":["Now create the training set by removing the test set from the **bamboo** data set, and name the training set **bamboo_train**.\n"]},{"cell_type":"code","id":"d87d34ea-c432-460c-81a4-b5209ca4ab70","metadata":{},"outputs":[],"source":["bamboo_test_index = bamboo.index.isin(bamboo_test.index)\nbamboo_train = bamboo[~bamboo_test_index]\n\nbamboo_train_ingredients = bamboo_train.iloc[:,1:] # ingredients\nbamboo_train_cuisines = bamboo_train[\"cuisine\"] # corresponding cuisines or labels"]},{"cell_type":"markdown","id":"1469fb95-7782-4865-8caa-2a3bb3e7ecbe","metadata":{},"outputs":[],"source":["Verify that there are 30 _fewer_ recipes now for each cuisine.\n"]},{"cell_type":"code","id":"9b48b286-785a-4925-b428-31493de8d798","metadata":{},"outputs":[],"source":["bamboo_train[\"cuisine\"].value_counts()"]},{"cell_type":"markdown","id":"1529e869-0883-4e0e-b2b8-a8beedc1ad6b","metadata":{},"outputs":[],"source":["Let's build the decision tree using the training set, **bamboo_train**, and name the generated tree **bamboo_train_tree** for prediction.\n"]},{"cell_type":"code","id":"bf2cdc72-5701-4b32-92fc-34108a514d6f","metadata":{},"outputs":[],"source":["bamboo_train_tree = tree.DecisionTreeClassifier(max_depth=15)\nbamboo_train_tree.fit(bamboo_train_ingredients, bamboo_train_cuisines)\n\nprint(\"Decision tree model saved to bamboo_train_tree!\")"]},{"cell_type":"markdown","id":"f45dabb9-917e-4412-ac37-95a1f92d3fe0","metadata":{},"outputs":[],"source":["Let's plot the decision tree and explore it.\n"]},{"cell_type":"code","id":"c1db7ac0-e349-45db-b700-0981bb26aa53","metadata":{},"outputs":[],"source":["export_graphviz(bamboo_train_tree,\n                feature_names=list(bamboo_train_ingredients.columns.values),\n                out_file=\"bamboo_train_tree.dot\",\n                class_names=np.unique(bamboo_train_cuisines),\n                filled=True,\n                node_ids=True,\n                special_characters=True,\n                impurity=False,\n                label=\"all\",\n                leaves_parallel=False)\n\nwith open(\"bamboo_train_tree.dot\") as bamboo_train_tree_image:\n    bamboo_train_tree_graph = bamboo_train_tree_image.read()\ngraphviz.Source(bamboo_train_tree_graph)"]},{"cell_type":"markdown","id":"3185a8f5-bd45-4a7b-a599-f17fc04cbf9d","metadata":{},"outputs":[],"source":["Now that you defined our tree to be deeper, more decision nodes are generated.\n"]},{"cell_type":"markdown","id":"7b7b6e1b-97ff-48c1-aab6-c8addb44ce13","metadata":{},"outputs":[],"source":["#### Now let's test our model on the test data.\n"]},{"cell_type":"code","id":"f28e190e-40b0-4817-94e3-8c8dd002e6c9","metadata":{},"outputs":[],"source":["bamboo_pred_cuisines = bamboo_train_tree.predict(bamboo_test_ingredients)"]},{"cell_type":"markdown","id":"b98550f1-57b7-4627-b159-b5fb33f52d2a","metadata":{},"outputs":[],"source":["To quantify how well the decision tree is able to determine the cuisine of each recipe correctly, we will create a confusion matrix which presents a nice summary on how many recipes from each cuisine are correctly classified. It also sheds some light on what cuisines are being confused with what other cuisines.\n"]},{"cell_type":"markdown","id":"b7ceb1b8-ebc6-4cab-a919-cb73066fa9d6","metadata":{},"outputs":[],"source":["So let's go ahead and create the confusion matrix for how well the decision tree is able to correctly classify the recipes in **bamboo_test**.\n"]},{"cell_type":"code","id":"fe66b29f-04f7-4421-a80a-1a28160b7417","metadata":{},"outputs":[],"source":["test_cuisines = np.unique(bamboo_test_cuisines)\nbamboo_confusion_matrix = confusion_matrix(bamboo_test_cuisines, bamboo_pred_cuisines, labels = test_cuisines)\ntitle = 'Bamboo Confusion Matrix'\ncmap = plt.cm.Blues\n\nplt.figure(figsize=(8, 6))\nbamboo_confusion_matrix = (\n    bamboo_confusion_matrix.astype('float') / bamboo_confusion_matrix.sum(axis=1)[:, np.newaxis]\n    ) * 100\n\nplt.imshow(bamboo_confusion_matrix, interpolation='nearest', cmap=cmap)\nplt.title(title)\nplt.colorbar()\ntick_marks = np.arange(len(test_cuisines))\nplt.xticks(tick_marks, test_cuisines)\nplt.yticks(tick_marks, test_cuisines)\n\nfmt = '.2f'\nthresh = bamboo_confusion_matrix.max() / 2.\nfor i, j in itertools.product(range(bamboo_confusion_matrix.shape[0]), range(bamboo_confusion_matrix.shape[1])):\n    plt.text(j, i, format(bamboo_confusion_matrix[i, j], fmt),\n             horizontalalignment=\"center\",\n             color=\"white\" if bamboo_confusion_matrix[i, j] \u003e thresh else \"black\")\n\nplt.tight_layout()\nplt.ylabel('True label')\nplt.xlabel('Predicted label')\n\nplt.show()"]},{"cell_type":"markdown","id":"b93948b5-0af2-40d3-8fef-e439c215ea05","metadata":{},"outputs":[],"source":["After running the above code, you should get a confusion matrix similar to the following:\n"]},{"cell_type":"markdown","id":"20b99edb-2824-470e-88dd-a7902155b192","metadata":{},"outputs":[],"source":["\u003cimg src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DS0103EN-SkillsNetwork/labs/Module%204/images/lab4_fig6_confusion_matrix.png\" width=\"500\"\u003e\n"]},{"cell_type":"markdown","id":"ad8b3288-e6b4-47ab-a690-1e118b397fac","metadata":{},"outputs":[],"source":["The rows represent the actual cuisines from the dataset and the columns represent the predicted ones. Each row should sum to 100%. According to this confusion matrix, we make the following observations:\n","\n","* Using the first row in the confusion matrix, 60% of the **Chinese** recipes in **bamboo_test** were correctly classified by our decision tree whereas 37% of the **Chinese** recipes were misclassified as **Korean** and 3% were misclassified as **Indian**.\n","\n","* Using the Indian row, 77% of the **Indian** recipes in **bamboo_test** were correctly classified by our decision tree and 3% of the **Indian** recipes were misclassified as **Chinese** and 13% were misclassified as **Korean** and 7% were misclassified as **Thai**.\n"]},{"cell_type":"markdown","id":"4a9b81dd-da48-48b5-bd16-5ad223c81003","metadata":{},"outputs":[],"source":["**Please note** that because decision trees are created using random sampling of the datapoints in the training set, then you may not get the same results every time you create the decision tree even using the same training set. The performance should still be comparable though! So don't worry if you get slightly different numbers in your confusion matrix than the ones shown above.\n"]},{"cell_type":"markdown","id":"f08265ac-705b-4884-a78c-1542bdcced3a","metadata":{},"outputs":[],"source":["Using the reference confusion matrix, how many **Japanese** recipes were correctly classified by our decision tree?\n"]},{"cell_type":"raw","id":"21c06d41-12de-4814-b8fb-1eb3cf9fc2e8","metadata":{},"outputs":[],"source":["Your Answer:\n\n"]},{"cell_type":"markdown","id":"3a6fe957-c96e-4fa9-9c11-7a28da698304","metadata":{},"outputs":[],"source":["\u003cdetails\u003e\u003csummary\u003eClick here for a solution\u003c/summary\u003e\n","\n","```python\n","    #The correct answer is:\n","    36.67%.\n","```\n","\n","\u003c/details\u003e\n"]},{"cell_type":"markdown","id":"f80e5ce4-a749-4548-9523-cce49e6bd206","metadata":{},"outputs":[],"source":["Also using the reference confusion matrix, how many **Korean** recipes were misclassified as **Japanese**?\n"]},{"cell_type":"raw","id":"ee7bd34c-a9ef-4b11-b8a1-87b6f3d5d229","metadata":{},"outputs":[],"source":["Your Answer:\n\n"]},{"cell_type":"markdown","id":"c38a7163-75a3-4dcc-9482-ea9a0f849b8c","metadata":{},"outputs":[],"source":["\u003cdetails\u003e\u003csummary\u003eClick here for a solution\u003c/summary\u003e\n","\n","```python\n","    #The correct answer is:\n","     3.33%.\n","```\n","\n","\u003c/details\u003e\n"]},{"cell_type":"markdown","id":"db805c55-41f3-48d8-8203-09e85feadb9a","metadata":{},"outputs":[],"source":["What cuisine has the least number of recipes correctly classified by the decision tree using the reference confusion matrix?\n"]},{"cell_type":"raw","id":"0fe85b0b-73c2-4718-9bcb-122a08c13728","metadata":{},"outputs":[],"source":["Your Answer:\n\n"]},{"cell_type":"markdown","id":"9b31b4d6-1a3f-4d09-8677-547e8da55f9c","metadata":{},"outputs":[],"source":["\u003cdetails\u003e\u003csummary\u003eClick here for a solution\u003c/summary\u003e\n","\n","```python\n","    #The correct answer is:\n","     Japanese cuisine, with 36.67% only.\n","```\n","\n","\u003c/details\u003e\n"]},{"cell_type":"markdown","id":"dc85ef54-974a-49ee-b978-60838bc47f4c","metadata":{},"outputs":[],"source":["\u003cbr\u003e\n","\u003chr\u003e\n"]},{"cell_type":"markdown","id":"d65dab21-f440-4a71-a1f5-3a34deebebfa","metadata":{},"outputs":[],"source":["### Thank you for completing this lab!\n","\n","## Author\n","\n","\u003ca href=\"https://www.linkedin.com/in/aklson/\" target=\"_blank\"\u003eAlex Aklson\u003c/a\u003e\n","\n","\n","\n","## Change Log\n","\n","\n","|  Date (YYYY-MM-DD) |  Version | Changed By  |  Change Description |\n","|---|---|---|---|\n","| 2023-08-16 | 2.3| Patsy | Updated minor grammar items |\n","| 2021-04-06 | 2.2 | Malika | Updated the data set link |\n","| 2020-09-25  | 2.1  | Lakshmi  | Fixed typo errors |\n","| 2020-08-27  | 2.0  | Lavanya  |  Moved lab to course repo in GitLab |\n","\n","\n","\u003chr\u003e\n","\n","## \u003ch3 align=\"center\"\u003e © IBM Corporation 2020. All rights reserved. \u003ch3/\u003e\n"]}],"metadata":{"kernelspec":{"display_name":"Python","language":"python","name":"conda-env-python-py"},"language_info":{"name":""}},"nbformat":4,"nbformat_minor":4}