## Posts Tagged ‘python’

### Machine learning “Top Trumps”

Saturday, October 1st, 2016

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 toptrumps.eu-gb.mybluemix.net.

### 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. (more…)

### Parsing roman numerals with Python

Sunday, November 2nd, 2014

Or… how I managed to make some of Grace’s maths homework into another Code Club session

Grace had some maths homework to do this weekend, converting a bunch of roman numerals into normal numbers.

Being an interfering sort of parent, I got her to show me what she’d done when she finished.

I could see that she’d gotten a lot of them wrong. She had missed the subtraction you’re supposed to do when a large value follows a smaller value.

I’m a big fan of rubber duck problem solving, but she hadn’t spotted that she’d gone wrong. I decided to try something similar.

### Comparing XML files ignoring order of attributes and child elements

Monday, October 6th, 2014

I need to diff some XML files.

For these particular XML files, order is not important. The XML is being used to contain a set of things, not a list – the order of the elements has no significance. Similarly, the order of the attributes within each element isn’t significant.

For example, for my purposes, these two XML files are equivalent:

```<myroot>
<mychild id="123">
<fruit>apple</fruit>
<comment>This is a comment</comment>
</mychild>
<mychild id="456">
<fruit>banana</fruit>
</mychild>
<mychild id="789">
<fruit>orange</fruit>
<test brackets="round" hello="greeting">
<number>111</number>
</test>
<dates>
<modified>123</modified>
<created>253</created>
<accessed>44</accessed>
</dates>
</mychild>
</myroot>```
```<myroot>
<mychild id="789">
<fruit>orange</fruit>
<test hello="greeting" brackets="round">
<number>111</number>
</test>
<dates>
<accessed>44</accessed>
<modified>123</modified>
<created>253</created>
</dates>
</mychild>
<mychild id="123">
<comment>This is a comment</comment>
<fruit>apple</fruit>
</mychild>
<mychild id="456">
<fruit>banana</fruit>
</mychild>
</myroot>```

I needed to compare some large XML files, which have big differences in the order of elements, and I couldn’t find a tool that would do the job. So I wrote a bit of Python to do it for me.

### Creating an iCalendar from online timetables

Sunday, January 19th, 2014

I’m a member of my local swimming pool, Fleming Park. I’m trying to swim a lot at the moment (as it’s a big help for my back).

I don’t have a regular schedule, I just try and squeeze in time for a swim any time I can spare. This means I’ve not learned the pool’s schedule and frequently have to check their website to find when the pool is available.

I’m checking it so frequently that it’s one of my Most Visited thumbnails in Chrome.

This isn’t efficient, particularly as it’s normally on my phone making me switch between the browser and Calendar apps. It’d be quicker and easier if I had the timetable in my calendar alongside my appointments, so I could easily see when I’m free and the pool is open.

The leisure centre doesn’t provide a feed so I can subscribe and add their schedule to my calendar.

dalelane.co.uk/…/swimflemingpark.ics

If you use the Fleming Park pool, import this in your Calendar app (or subscribe to it from Google Calendar) and the next week’s pool timetable will be kept up to date in your calendar.

### Smile!

Tuesday, April 3rd, 2012

This is my mood (as identified from my facial expressions) over time while watching Never Mind the Buzzcocks.

The green areas are times where I looked happy.

This shows my mood while playing XBox Live. Badly.

The red areas are times where I looked cross.

I smile more while watching comedies than when getting shot in the head. Shocker, eh?

### My first experience using BlueVia APIs

Sunday, October 2nd, 2011

I wrote yesterday about a quick hack I did at Over The Air using the BlueVia API. I thought it was worth a quick post to show just how simple it was.

Read yesterday’s post for background to the idea behind the hack, but in essence, what I wanted was:

• monitor the location of my mobile phone
• send an SMS to a different mobile number when my phone goes into a predefined known area

BlueVia provides an API that let me doing this using network operator data. In other words, nothing needs to run on my phone itself as location data is obtained from where O2 thinks my phone is.

This means there is no battery-life impact on the phone for this monitoring.

It also means this will work with any phone – from iPhones and Androids to cheap feature phones.

The whole thing took me less than an hour and needed only 90 lines of Python.

This is how I did it.

### Annotating photos with tweets

Sunday, September 11th, 2011

I have a lot of digital photos.

An insane amount – something like 40,000 photos that go back over a dozen years since I first got a digital camera at University.

I store them based on the date that they were taken, using a folder structure like this:

For a while, I used to drop a readme.txt text file into some of the folders saying where the photos were taken or what I was doing. This was partly so that when I look at the photos ten years later I’ve got something to remind me what is going on, but mainly to make it possible for me to search for photos of something when I can’t remember the date it happened.

But in recent years, I’ve been too lazy to keep that up, and rarely ever add a readme file.

I thought that my tweets might be a good alternative. There is a reasonable chance that if I took a photo of something interesting, that I might have tweeted sometime that day about where I am or what I’m doing.

I wanted to populate each of my folders with a day’s photos in it with a tweets.txt text file containing tweets posted on that day.

### Counting my key presses

Friday, June 3rd, 2011

Overview

Counting and visualising which keyboard keys I press the most often

Background

I noticed something yesterday.

The lettering on my left Ctrl button is a lot more faded than the lettering on my right Ctrl button.

I must press the left Ctrl button more often than I do the right one.

That got me looking at the rest of the keys on the keyboard. Some of them are faded, too. Some a little, some a lot.

I must press some of those keys more than I do others.

I’ve played hangman, so I know that there are some letters that occur more frequently in English words than others. So I could have just said that those are the keys I probably use the most and left it at that.

But… I don’t spend that much time writing documents or large chunks of English.

I’m a code monkey. I’m not sure that distribution would necessarily apply to me.

For example, I probably use the semi-colon key quite a lot – at the end of every line when writing in some languages. That wouldn’t be true for people writing English.

So I wondered how much I use each keyboard key in comparison to each other.

Being an obsessive compulsive geek, I couldn’t leave that as an idle wondering. I had to find out. I had to go and get the data.