{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we'll try to solve to a problem that arose when I was working on the Kaggle What's Cooking challenge (see my previous posts on the subject [here](http://flothesof.github.io/kaggle-whats-cooking-machine-learning.html) and [here](http://flothesof.github.io/kaggle-whatscooking-bokeh-plots.html)). \n",
"\n",
"In this challenge, the data, which consists of recipes, contains a few quirks. For example, the data may contain names of brands such as *knorr garlic minicub*, or mention weights and quantities that should have been cleaned up beforehand *(10 oz.) frozen chopped spinach, thawed and squeezed dry*.\n",
"\n",
"The goal of this notebook is therefore to develop a method that suggests better ingredient names in an automatic way, based on a given recipe.\n",
"\n",
"The outline of this blog post is as follows: we will first look at some overall statistics about the data, then develop a probabilistic model inspired by the principle behind spellcheckers and google auto-correct as explained by [Peter Norvig](http://norvig.com/spell-correct.html) and finally apply it to the machine learning algorithm used in the previous posts, the logistic regression."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Statistics about the data, at ingredient and word level"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, we'll do some basic data exploration. In particular, let's try to answer the following questions:\n",
"\n",
"- how many ingredients do we find in the data? how many are unique?\n",
"- how many individual words make up these ingredients?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, let's load the data. We read it using codecs because pandas hasn't support for specifying encoding in the json loading routine (see
\n", " | cuisine | \n", "id | \n", "ingredients | \n", "
---|---|---|---|
0 | \n", "greek | \n", "10259 | \n", "[romaine lettuce, black olives, grape tomatoes... | \n", "
1 | \n", "southern_us | \n", "25693 | \n", "[plain flour, ground pepper, salt, tomatoes, g... | \n", "
2 | \n", "filipino | \n", "20130 | \n", "[eggs, pepper, salt, mayonaise, cooking oil, g... | \n", "
3 | \n", "indian | \n", "22213 | \n", "[water, vegetable oil, wheat, salt] | \n", "
4 | \n", "indian | \n", "13162 | \n", "[black pepper, shallots, cornflour, cayenne pe... | \n", "
\n", " | original ingredient | \n", "improved ingredient | \n", "
---|---|---|
0 | \n", "pillsbury™ crescent recipe creations® refriger... | \n", "dough | \n", "
1 | \n", "kraft mexican style shredded four cheese with ... | \n", "cheese | \n", "
2 | \n", "bertolli vineyard premium collect marinara wit... | \n", "wine | \n", "
3 | \n", "kraft shredded pepper jack cheese with a touch... | \n", "pepper | \n", "
4 | \n", "hidden valley® farmhouse originals italian wit... | \n", "herbs | \n", "
5 | \n", "hidden valley® original ranch salad® dressing ... | \n", "seasoning | \n", "
6 | \n", "condensed reduced fat reduced sodium cream of ... | \n", "cream | \n", "
7 | \n", "hellmann’ or best food canola cholesterol fr... | \n", "canola | \n", "
8 | \n", "condensed reduced fat reduced sodium cream of ... | \n", "chicken | \n", "
9 | \n", "i can't believ it' not butter! made with olive... | \n", "oil olive | \n", "
10 | \n", "wish-bone light asian sesame ginger vinaigrett... | \n", "ginger | \n", "
11 | \n", "(10 oz.) frozen chopped spinach, thawed and sq... | \n", "spinach | \n", "
12 | \n", "kraft mexican style 2% milk finely shredded fo... | \n", "milk | \n", "
13 | \n", "kraft shredded low-moisture part-skim mozzarel... | \n", "mozzarella cheese shredded | \n", "
14 | \n", "hurst family harvest chipotle lime black bean ... | \n", "lime | \n", "
15 | \n", "reduced fat reduced sodium tomato and herb pas... | \n", "sauce tomato | \n", "
16 | \n", "frozen orange juice concentrate, thawed and un... | \n", "orange | \n", "
17 | \n", "crystal farms reduced fat shredded marble jack... | \n", "cheese | \n", "
18 | \n", "i can't believe it's not butter!® all purpose ... | \n", "believe can't butter!® purpose it's not all st... | \n", "
19 | \n", "sargento® traditional cut shredded mozzarella ... | \n", "mozzarella cheese shredded | \n", "
20 | \n", "lipton sparkling diet green tea with strawberr... | \n", "kiwi | \n", "
21 | \n", "sargento® traditional cut shredded 4 cheese me... | \n", "cheese | \n", "
22 | \n", "hidden valley® original ranch® spicy ranch dre... | \n", "ranch dressing | \n", "
23 | \n", "hidden valley® greek yogurt original ranch® di... | \n", "greek yogurt | \n", "
24 | \n", "conimex woksaus specials vietnamese gember kno... | \n", "woksaus | \n", "
25 | \n", "honeysuckle white® hot italian turkey sausage ... | \n", "sausage italian | \n", "
26 | \n", "ragu old world style sweet tomato basil pasta ... | \n", "pasta | \n", "
27 | \n", "sargento® artisan blends® shredded parmesan ch... | \n", "parmesan cheese | \n", "
28 | \n", "reduced fat reduced sodium cream of mushroom soup | \n", "cream | \n", "
29 | \n", "frozen lemonade concentrate, thawed and undiluted | \n", "frozen lemonade concentrate | \n", "
30 | \n", "reduced sodium reduced fat cream of mushroom soup | \n", "cream | \n", "
31 | \n", "condensed reduced fat reduced sodium tomato soup | \n", "fat | \n", "
32 | \n", "shredded reduced fat reduced sodium swiss cheese | \n", "cheese | \n", "
33 | \n", "knorr reduc sodium chicken flavor bouillon cube | \n", "chicken | \n", "
34 | \n", "2 1/2 to 3 lb. chicken, cut into serving pieces | \n", "chicken | \n", "
35 | \n", "kraft mexican style finely shredded four cheese | \n", "cheese | \n", "
36 | \n", "frozen chopped spinach, thawed and squeezed dry | \n", "spinach | \n", "
37 | \n", "frank's® redhot® original cayenne pepper sauce | \n", "pepper | \n", "
38 | \n", "reduced sodium condensed cream of chicken soup | \n", "chicken | \n", "
39 | \n", "foster farms boneless skinless chicken breasts | \n", "breasts boneless skinless chicken | \n", "
40 | \n", "knorr tomato bouillon with chicken flavor cube | \n", "chicken | \n", "
41 | \n", "pompeian canola oil and extra virgin olive oil | \n", "oil olive | \n", "
42 | \n", "hidden valley® original ranch® light dressing | \n", "dressing | \n", "
43 | \n", "uncle ben's ready rice whole grain brown rice | \n", "rice | \n", "
44 | \n", "soy vay® veri veri teriyaki® marinade & sauce | \n", "soy sauce | \n", "
45 | \n", "taco bell® home originals® taco seasoning mix | \n", "seasoning taco | \n", "
46 | \n", "pillsbury™ refrigerated crescent dinner rolls | \n", "rolls | \n", "
47 | \n", "kraft reduced fat shredded mozzarella cheese | \n", "mozzarella cheese shredded | \n", "
48 | \n", "reduced sodium italian style stewed tomatoes | \n", "tomatoes | \n", "
49 | \n", "skinless and boneless chicken breast fillet | \n", "chicken | \n", "
\n", " | original ingredient | \n", "improved ingredient | \n", "
---|---|---|
0 | \n", "pillsbury™ crescent recipe creations® refriger... | \n", "refrigerated seamless dough crescent | \n", "
1 | \n", "kraft mexican style shredded four cheese with ... | \n", "cheese shredded | \n", "
2 | \n", "bertolli vineyard premium collect marinara wit... | \n", "wine marinara bertolli vineyard with burgundi ... | \n", "
3 | \n", "kraft shredded pepper jack cheese with a touch... | \n", "kraft jack pepper cheese shredded | \n", "
4 | \n", "hidden valley® farmhouse originals italian wit... | \n", "herbs italian | \n", "
5 | \n", "hidden valley® original ranch salad® dressing ... | \n", "ranch dressing | \n", "
6 | \n", "condensed reduced fat reduced sodium cream of ... | \n", "mushroom fat cream reduced soup of sodium | \n", "
7 | \n", "hellmann’ or best food canola cholesterol fr... | \n", "best canola cholesterol mayonnais food or free... | \n", "
8 | \n", "condensed reduced fat reduced sodium cream of ... | \n", "cream chicken reduced soup condensed of sodium | \n", "
9 | \n", "i can't believ it' not butter! made with olive... | \n", "oil olive | \n", "
10 | \n", "wish-bone light asian sesame ginger vinaigrett... | \n", "vinaigrette dressing | \n", "
11 | \n", "(10 oz.) frozen chopped spinach, thawed and sq... | \n", "chopped frozen spinach thawed squeezed and dry | \n", "
12 | \n", "kraft mexican style 2% milk finely shredded fo... | \n", "mexican shredded finely style cheese kraft four | \n", "
13 | \n", "kraft shredded low-moisture part-skim mozzarel... | \n", "kraft mozzarella cheese shredded | \n", "
14 | \n", "hurst family harvest chipotle lime black bean ... | \n", "bean soup mix | \n", "
15 | \n", "reduced fat reduced sodium tomato and herb pas... | \n", "sauce tomato | \n", "
16 | \n", "frozen orange juice concentrate, thawed and un... | \n", "orange frozen juice concentrate | \n", "
17 | \n", "crystal farms reduced fat shredded marble jack... | \n", "cheese reduced shredded fat | \n", "
18 | \n", "i can't believe it's not butter!® all purpose ... | \n", "believe can't butter!® sticks not it's all i p... | \n", "
19 | \n", "sargento® traditional cut shredded mozzarella ... | \n", "mozzarella cheese shredded | \n", "
20 | \n", "lipton sparkling diet green tea with strawberr... | \n", "tea green | \n", "
21 | \n", "sargento® traditional cut shredded 4 cheese me... | \n", "cheese shredded | \n", "
22 | \n", "hidden valley® original ranch® spicy ranch dre... | \n", "hidden valley® ranch® original dressing | \n", "
23 | \n", "hidden valley® greek yogurt original ranch® di... | \n", "greek yogurt | \n", "
24 | \n", "conimex woksaus specials vietnamese gember kno... | \n", "woksaus specials knoflook gember conimex vietn... | \n", "
25 | \n", "honeysuckle white® hot italian turkey sausage ... | \n", "hot sausage turkey italian links | \n", "
26 | \n", "ragu old world style sweet tomato basil pasta ... | \n", "pasta world old ragu style sauc | \n", "
27 | \n", "sargento® artisan blends® shredded parmesan ch... | \n", "parmesan cheese shredded | \n", "
28 | \n", "reduced fat reduced sodium cream of mushroom soup | \n", "mushroom fat cream reduced soup of | \n", "
29 | \n", "frozen lemonade concentrate, thawed and undiluted | \n", "frozen lemonade concentrate | \n", "
30 | \n", "reduced sodium reduced fat cream of mushroom soup | \n", "mushroom fat cream reduced soup of | \n", "
31 | \n", "condensed reduced fat reduced sodium tomato soup | \n", "condensed soup tomato | \n", "
32 | \n", "shredded reduced fat reduced sodium swiss cheese | \n", "swiss reduced cheese fat | \n", "
33 | \n", "knorr reduc sodium chicken flavor bouillon cube | \n", "bouillon reduc flavor knorr chicken sodium | \n", "
34 | \n", "2 1/2 to 3 lb. chicken, cut into serving pieces | \n", "pieces chicken | \n", "
35 | \n", "kraft mexican style finely shredded four cheese | \n", "cheese shredded | \n", "
36 | \n", "frozen chopped spinach, thawed and squeezed dry | \n", "chopped frozen spinach | \n", "
37 | \n", "frank's® redhot® original cayenne pepper sauce | \n", "pepper cayenne sauce | \n", "
38 | \n", "reduced sodium condensed cream of chicken soup | \n", "condensed soup cream chicken of | \n", "
39 | \n", "foster farms boneless skinless chicken breasts | \n", "breasts boneless skinless chicken | \n", "
40 | \n", "knorr tomato bouillon with chicken flavor cube | \n", "bouillon tomato with flavor knorr chicken | \n", "
41 | \n", "pompeian canola oil and extra virgin olive oil | \n", "oil virgin olive | \n", "
42 | \n", "hidden valley® original ranch® light dressing | \n", "hidden valley® ranch® original dressing | \n", "
43 | \n", "uncle ben's ready rice whole grain brown rice | \n", "whole grain rice | \n", "
44 | \n", "soy vay® veri veri teriyaki® marinade & sauce | \n", "soy sauce | \n", "
45 | \n", "taco bell® home originals® taco seasoning mix | \n", "seasoning taco mix | \n", "
46 | \n", "pillsbury™ refrigerated crescent dinner rolls | \n", "refrigerated rolls crescent | \n", "
47 | \n", "kraft reduced fat shredded mozzarella cheese | \n", "cheese reduced shredded fat | \n", "
48 | \n", "reduced sodium italian style stewed tomatoes | \n", "tomatoes style italian stewed | \n", "
49 | \n", "skinless and boneless chicken breast fillet | \n", "skinless breast chicken boneless | \n", "
\n", " | cuisine | \n", "id | \n", "ingredients | \n", "all_ingredients | \n", "better_ingredients | \n", "
---|---|---|---|---|---|
0 | \n", "greek | \n", "10259 | \n", "[romaine lettuce, black olives, grape tomatoes... | \n", "romaine lettuce;black olives;grape tomatoes;ga... | \n", "romaine lettuce;black olives;grape tomatoes;ga... | \n", "
1 | \n", "southern_us | \n", "25693 | \n", "[plain flour, ground pepper, salt, tomatoes, g... | \n", "plain flour;ground pepper;salt;tomatoes;ground... | \n", "plain flour;ground pepper;salt;tomatoes;ground... | \n", "
2 | \n", "filipino | \n", "20130 | \n", "[eggs, pepper, salt, mayonaise, cooking oil, g... | \n", "eggs;pepper;salt;mayonaise;cooking oil;green c... | \n", "eggs;pepper;salt;mayonaise;cooking oil;green c... | \n", "
3 | \n", "indian | \n", "22213 | \n", "[water, vegetable oil, wheat, salt] | \n", "water;vegetable oil;wheat;salt | \n", "water;vegetable oil;wheat;salt | \n", "
4 | \n", "indian | \n", "13162 | \n", "[black pepper, shallots, cornflour, cayenne pe... | \n", "black pepper;shallots;cornflour;cayenne pepper... | \n", "black pepper;shallots;cornflour;cayenne pepper... | \n", "
5 | \n", "jamaican | \n", "6602 | \n", "[plain flour, sugar, butter, eggs, fresh ginge... | \n", "plain flour;sugar;butter;eggs;fresh ginger roo... | \n", "plain flour;sugar;butter;eggs;fresh ginger roo... | \n", "
6 | \n", "spanish | \n", "42779 | \n", "[olive oil, salt, medium shrimp, pepper, garli... | \n", "olive oil;salt;medium shrimp;pepper;garlic;cho... | \n", "olive oil;salt;medium shrimp;pepper;garlic;cho... | \n", "
7 | \n", "italian | \n", "3735 | \n", "[sugar, pistachio nuts, white almond bark, flo... | \n", "sugar;pistachio nuts;white almond bark;flour;v... | \n", "sugar;nuts pistachio;white bark almond;flour;v... | \n", "
8 | \n", "mexican | \n", "16903 | \n", "[olive oil, purple onion, fresh pineapple, por... | \n", "olive oil;purple onion;fresh pineapple;pork;po... | \n", "olive oil;purple onion;fresh pineapple;pork;po... | \n", "
9 | \n", "italian | \n", "12734 | \n", "[chopped tomatoes, fresh basil, garlic, extra-... | \n", "chopped tomatoes;fresh basil;garlic;extra-virg... | \n", "chopped tomatoes;fresh basil;garlic;extravirgi... | \n", "