How I used Keras to build a neural network that recognises high quality wine

Edward Leoni
Learn stuff with Ed
4 min readMar 3, 2018

It’s Friday night, you are having a sip of wine you bought in the super market after leaving work, you waited until dinner is ready to try your wine, and then urrrrgh.

That’s one crappy wine you bought, but how would you know? You’re just a software developer, you know nothing about wine. You have always been keen to up-skill in Machine Learning though, so you do the logical thing, you spend the rest of the weekend building a neural network to recognise good quality wine, obviously.

Full disclosure: By the end of this post, you too, will be able to build your own neural network that will prevent you from buying crappy wine.

First thing I needed to do was to find a dataset of wines to work from, luckily I quickly found a dataset from a university that contained information about 4898 different wines and I thought to myself: “that ought to do it!

The dataset contained 12 attributes, 11 wine features and 1 rate at the end. It was perfect. At this point it’s late, I’m sleepy, and I’m a few glasses deep into the crappy wine (You didn’t think I was going to throw it away, did you?) so I decide to call it a night and continue the next day.

So the next morning the research continues during breakfast, I’m unstoppable, no more crappy wine for me, oh no sir!

I started looking at all the things AI, it took me a while to figure that out, but when it comes to googling Artificial Intelligence, it has the same effect as watching cat videos on youtube, you finish a video, then there’s the next, then there’s the next, then there’s the next and by the end of it you are watching Demi Lovato’s interview after the last concert.

I digress.

The morning went by, but at least I had a good place to start: I had found Keras. And I was in love, I find Keras so easy to use and the barrier to entry is really low, I highly recommend it if you are just starting.

At this point I had the concepts and I had the tool, it was time to get coding! So here’s what I came up with after looking at a few examples:

BOOM! Easy, 15 lines of code if you don’t count empty lines and comments. Something like that would just be impossible without Keras and Python ❤.

First 3 lines I’m importing dependencies, line 5, loading the dataset containing wine features and rating, lines 7 and 8 I’m separating the input (features) from the output (rating).

A bit of house keeping on line 11, and then from line 13 to 17 I’m straight up setting up my neural network.

Yes, by line 19 I’m already compiling the model and comes line 21 I’m already training it!

To close it on line 23, I save the model to disk to use it later.

At this stage I have my mouth open about how easy Keras is, and the wine? Well the wine is gone by now.

Before getting real about using the model to verify which wine I should buy next, I needed to make sure I could trust this thing. So I came up with this little snippet to evaluate the accuracy of the model:

The result tells me that the model has an accuracy of just over 80%. GOOD ENOUGH.

And so, from now on, how would I go about testing whether a wine is good or not? Well there’s a snippet of code for that too. But I’ll step back a little and explain the features that are being considered by my model:

  1. fixed acidity
  2. volatile acidity
  3. citric acid
  4. residual sugar
  5. chlorides
  6. free sulfur dioxide
  7. total sulfur dioxide
  8. density
  9. pH
  10. sulfates
  11. alcohol level

The above is the content of the dataset I mentioned before, the dataset also contains a rate from 0 to 10 for each of the wines.

That allows the Neural Network above to find a correlation between the value of the features and final rate and thereafter be able to predict if a wine is good or bad. Here’s what the prediction code looks like:

Again, 10 lines, how great is the combination Python and Keras?

On line 8 I’m describing a wine that I know has been rated as good, and on line 9 I’m describing a wine that I know has been rated as bad. So as a test, when I run the above, the results I get are…

ACCURATE.

Or should I say…

And that’s the end of the almost real story of how I’m never drinking bad wine again!

--

--

Edward Leoni
Learn stuff with Ed

I teach over 50.000 people online via Udemy. Are you one of them?