As my dad was learning how to code, I learned more about what he had been doing in his job for the past two decades. He worked at a supplying company (Supplier Inc) that buys supplies for ship voyages in southeast Asia.
When I listened and asked questions about his main task, I wondered if JavaScript could have saved him some time. I wanted to teach him about machine learning, so I came up with a quick example. It turns out that a couple hours of coding could do his full-time job in seconds.
Ships sometimes sail for weeks non-stop. This means when a ship is in port, it needs to stock up with massive quantities of food for its next voyage. The ship sends Supplier Inc an Excel file of everything it needs (beef, lamb, soda, Red Bull, Oreo cookies, lettuce, etc).
If Supplier Inc buys 100lbs of beef from a store, it will be very expensive. It's cheaper to find a wholesaler that specializes in each type of food (meat, veggies, etc.). So Supplier Inc would put all the meat items into an Excel file and send it to several meat suppliers to get quotes. It would do the same with veggie items and send to veggie suppliers. The remaining items would be purchased from stores.
Supplier Inc needs to separate each order into categories: meat, veggies, and everything else (items that can't be bought in bulk; call it 'store'). My dad and his peers worked all year round to take these Excel sheets from the ship captains and sort each item into supplier categories: VEGGIES.xlsx, MEAT.xlsx, and SHOP.xlsx.
This project shows my dad how a few hundred lines of JavaScript code could easily replace him and his coworkers' jobs.
We first started with a sample of the Excel he would receive from a ship.
After looking at it, we wanted to just pick a few input /output to build a model, which is a set of mathematical equations ( model
) that can most accurately give the correct output given a set of inputs. We picked a few from the excel above and saved the result into a file. Each key is a random item from the Excel (input) and the value is the category that it belongs to (output).
provisionData.json
(case doesn't matter to the model; this is just how the items are listed in the suppliers' catalogs). Also notice how there are typos (lamp instead of lamb). Hopefully our model will do a best guess and little typos should not affect the output.
{
"LAMP RUMP (OFFER LAMP LEG) - LAMP LEG": "meat",
"FROZEN CHICKEN LEG": "meat",
"CALF'S LIVER - BEEF LIVER": "meat",
"BEEF SIRLOIN CONTRAFILLET": "meat",
"BEEF ANTRICOT": "meat",
"FAT TAIL": "meat",
"BEEF RUMP": "meat",
"CHICKEN BREAST": "meat",
"TOMATO PASTE 830 GR": "veggie",
"JACKFRUIT": "veggie",
"CUCUMBER": "veggie",
"FROZEN SPINACH": "veggie",
"KETCHUP": "store",
"PASTA Basedow 4": "store",
"KIWI FRUIT": "veggie",
"GRAPES RED": "veggie",
"GRAPES GREEN": "veggie",
"JALAPENO PEPPER PICKLE 5 LT": "veggie",
"WHITE CHEESE EZINE TAHSILDAROGLU 600 GR": "store",
"RIZE TURIST BLACK TEA 1 KG": "store",
"DOUGH THIN FOR BAKLAVA 450 GR": "store",
"TABLE SALT": "store",
"OLIVE OIL": "store",
"ROASTED GROUNDNUT 120 GR - 12 PCS": "store",
"MCVITIE'S DIGESTIVE BISCUITS - DARK CHOCOLATE 24 PCS": "store",
"BEEF BONELESS SILVER SIDE": "meat"
}
JSON files are a bit unforgiving when you make careless mistakes. Make sure the last value in your object does not have ,
module.exports = ...
and changing the filename to provisionData.js
Now comes machine learning! Using the training data above, we tell the computer to build a model
that can most accurately place the items into the correct categories. Then we'll apply the data to categorize new data.
There are many machine learning libraries for JavaScript. For this project, we will use brain.js.
We'll create a file called index.js
and look at it in two sections. The first section will train the model. The steps are: