# Classifying Cookies

In this post we’ll use data from a supemarket website to classify products, as either cookies or crisps.

I’m boring enough to spend my free time classifying supermarket data, but not boring enough to manually build a data set, so I wrote this code to grab the data. You can run it yourself or you can download the result here.

The data has unit price, price per quanitity, ingredients and nutrition information. We will figure out a few more features, but I don’t want this to be a feature building exercise. I generally expect cookies to be heavier, more expensive and have more ingredients.

We load in our libraries and the data.

Reshape the data so it’s easier to use.

Figure out how much something costs per gram.

Then we figure out the weight.

We’ll build a kNN. To do this we randomly split our data into training and testing samples.

We can check that the split is representative of the full data of 95 cookies and 216 crisps.

The test set has a few more cookies than the full set, but it looks good enough for us.

We then train the model on, the number of ingredients, price and weight. I’ve set the model to look at the 5 nearest neighbours. Considering our data only contains ‘cookies’ and ‘crisps’ a tie should be impossible.

We can get a feel of our result below. Red circles and green triangles are correct classifications. I’ve added the training data in blue so we can see the full data set.

I was hoping we would see cookies (a bigger, heavier so presumably more costly product) form a cluster of high price but unfortunately, we do not.

We can compare the result of the classifier with the true classification to see how accurate we are.

0.786

Is this accuracy good? Not really, if we just assumed everything was crisps we’d do about as well as the model.

cookies = 0.226

crisps = 0.774

You might think comparing to more neighbours will improve the accuracy, so I wrote a bit of code that varies the number of neighbours to check.

We’ll compare models that use between 1 and 30 neighbours.

I want to check the overall accuracy of the model, as well as how many cookies or crisps it gets right, regardless of false matches.

We use the caret package to split the data into a set of 10 groups, each group then takes a turn as the test set. We repeat this 5 times and take the average for each number of nieghbours we’re testing.

We then do a small reshape and plot to see how accurate the model is for how many nearest neighbours are used.

The lines for cookies and crisps show the proportion of each that were classified. If we predicted all cookies and crisps correctly, everything would have an accuracy of 1. If we just said everything is a cookie, our accuracy for cookies would be 1, crisps would be 0 and the accuracy of the model would be somewhere between.

We see the model is best at k = 1. When we use more neighbours the model generally gets better at finding crisps at the expense of finding cookes and reduces the models overall accuracy ie. it’s incorrectly flagging cookies as crisps.

I’m very suspicious at how good k = 1 looks. It could be because there are many more crisps than cookies and the data isn’t particularly ‘clustered’ (I hoped that cookies would give a cluster in high price, heavy weight to examine). This would mean adding more neighbours to the model simply finds more crisps and so an item is less likely to be predicted as a cookie.

With hindisight I went straight to a solution for the sake of a snappy post title. I should have spent more time exploring and understanding the data before trying to classify cookies, but for now, thanks for reading.