Machine learning “Top Trumps”

A simple demonstration of machine learning to let a child train a computer to play Top Trumps

I’ve been talking for a while now about how we introduce the idea behind machine learning to school kids. I’ve given several talks about it but I’ve also tried out a couple of approaches to it.

Now I’m trying out another: training a machine learning bot how to play Top Trumps.

I’ve put a demo at


What is this?

It’s basically Top Trumps: that card game I used to play as a kid where you choose one of the attributes on a card, and if it beats the other player you get their card. Except it’s online, and you’re playing against a computer.

But the computer hasn’t been given any strategies on how to play, and has to learn from the player.

Initially, it makes random choices, but it learns from playing against the player. The more turns it plays, the more training it gets, which it uses to make predictions of which choice would give it the best chance of winning.

What is the point?

It’s a quick and simple demo of machine learning, aimed at primary school children.

The idea is to give them a first-hand experience in training a machine learning system, by letting them train it to play a simple game they’re already familiar with. And with that first-hand experience, hopefully see for themselves how it behaves and how it improves.

As I’ve talked about before, the point is to explain that there is another approach to the “programming means giving computers a list of steps to follow” concept introduced in schools. It’s about introducing the idea that sometimes we get computers to do something by giving it examples of the job we want it to do.

I’ve been trying this on my kids for a while now, and they get it. For example, Faith has mentioned how when she watches and rates things on Netflix, that she’s training the Netflix app how to choose other programmes that she could watch.

That’s the point! It’s about helping them understand how the world around them works, by giving them a few tangible experiences where they can try it for themselves.

How does it work?

I’ve put the source code on github.

In short, it’s using a decision tree classifier that’s trained on the attributes of a card, the choice of attribute made by the player, and the outcome (whether they won, lost, or drew).

This felt like a reasonably good fit for this sort of job. Plus it was super quick to code, and runs quickly.

The back-end code is all in Python… mostly because I started it at a hackday, and wanted to remind myself how to use Python after not doing anything in it for a couple of years.

The front-end looks so much better than my previous ML kids projects thanks to Joe Pavitt. All the UI elements, like the animated cards, the decks and the game screen, were made by him. It means that the finished Top Trumps game doesn’t have the default-bootstrap look of my Rock, Paper, Scissors game or the vanilla-HTML-ugliness look of my my Guess Who game.

I went with English Kings and Queens as the data for the cards. I wanted it to be something educational and this was the first idea I had.

The data all came from Wikipedia so I think it’s accurate. (If it’s not, it’s most likely that I messed something up when I was copying it.)

All the card data and rules for the deck comes from a CSV file not the code. My code just looks in a data/training folder, so adding CSV files with different card data to this folder will make it possible to theme the game with different topics. I’m thinking of adding a food based deck, with each card being a different meal or type of food, and different nutritional values as the attributes.

If you think of any other themes that would be good to do, let me know. Or even better, send me a pull request with a new CSV file I could use 😉

What next?

It won’t just be my kids who have this inflicted on them. On Monday, it’ll be used as one of the activities at XploreIT, which is a schools event we hold at IBM’s Hursley Park. Unfortunately, I won’t be able to be there (I’m working in Ascot this week!) but I’ll be keeping my fingers crossed that it goes well.

Tags: , ,

One Response to “Machine learning “Top Trumps””

  1. Hi Dale,

    The topic of machine learning to school kids is very interesting. Technology and education is such a popular, debated topic.