From: Steinar H. Gunderson Date: Sat, 17 Mar 2012 12:07:33 +0000 (+0100) Subject: Add a script to train the model. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;ds=sidebyside;h=5daac690d3670f846dd8b7527074e2d9e5208920;p=wloh Add a script to train the model. --- diff --git a/train.pl b/train.pl new file mode 100755 index 0000000..4981e44 --- /dev/null +++ b/train.pl @@ -0,0 +1,65 @@ +#! /usr/bin/perl +use DBI; +use strict; +use warnings; +no warnings qw(once); +use POSIX; +require './config.pm'; + +my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password) + or die "connect: " . $DBI::errstr; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +# Find last completely done season +my $ref = $dbh->selectrow_hashref('SELECT sesong FROM fotballserier GROUP BY sesong HAVING COUNT(*)=COUNT(avgjort=1 OR NULL) ORDER BY sesong DESC LIMIT 1'); +my $last_season = $ref->{'sesong'}; + +my $tmpnam = POSIX::tmpnam(); +open DATA, ">", $tmpnam + or die "$tmpnam: $!"; + +# Fetch name (ID) list +my $q = $dbh->prepare('SELECT DISTINCT id FROM fotballdeltagere'); +$q->execute(); +my @ids = (); +while (my $ref = $q->fetchrow_hashref) { + my $id = $ref->{'id'}; + push @ids, $id; +} + +print DATA scalar @ids, "\n"; +for my $id (@ids) { + print DATA $id, "\n"; +} + +# Fetch games +$q = $dbh->prepare(' +SELECT + deltager1.id as p1, deltager2.id as p2, maalfor, maalmot, least(pow(2.0, (sesong - ? + 3) / 3.0), 1.0) AS vekt +FROM + Fotballresultater resultater + JOIN Fotballdeltagere deltager1 ON resultater.Lagrecno=deltager1.Nr AND resultater.Serie=deltager1.Serie + JOIN Fotballdeltagere deltager2 ON resultater.Motstander=deltager2.Nr AND resultater.Serie=deltager2.Serie + JOIN Fotballserier serier on resultater.Serie=serier.Nr +WHERE deltager1.Nr > deltager2.nr +'); +$q->execute($last_season); + +while (my $ref = $q->fetchrow_hashref) { + printf DATA "%d %d %d %d %f\n", $ref->{'p1'}, $ref->{'p2'}, $ref->{'maalfor'}, $ref->{'maalmot'}, $ref->{'vekt'}; +} + +close DATA; + +$dbh->do('DELETE FROM ratings'); +my $iq = $dbh->prepare('INSERT INTO ratings ( id, rating ) VALUES (?, ?)'); + +open RATINGS, "/home/sesse/dev/bayeswf/bayeswf < $tmpnam |" + or die "bayeswf: $!"; +while () { + /(.*) (.*)/ or next; + $iq->execute($2, $1); +} + +$dbh->commit;