]> git.sesse.net Git - wloh/blobdiff - train.pl
Store covariance matrix when training model.
[wloh] / train.pl
index 119b738dd4cddf322fee45107db587cb4df1dd47..9238b61cb73ef1e6945c352a46f5f04f257906e3 100755 (executable)
--- a/train.pl
+++ b/train.pl
@@ -11,6 +11,8 @@ my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config:
 $dbh->{AutoCommit} = 0;
 $dbh->{RaiseError} = 1;
 
+$dbh->do('SET client_min_messages TO WARNING');
+
 # 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'};
@@ -54,15 +56,33 @@ for my $ref (@games) {
 }
 close DATA;
 
-$dbh->do('DELETE FROM ratings');
-my $iq = $dbh->prepare('INSERT INTO ratings ( id, rating, rating_stddev ) VALUES (?, ?, ?)');
+my @ratings = ();
+my @covariances = ();
 
 open RATINGS, "$config::base_dir/bayeswf < $tmpnam |"
        or die "bayeswf: $!";
 while (<RATINGS>) {
-       /(.*) (.*) (.*)/ or next;
-       $iq->execute($3, $1, $2);
+       chomp;
+       my @x = split;
+       if ($x[0] eq 'covariance') {
+               push @covariances, (join("\t", @x[1..3]));
+       } else {
+               push @ratings, ($x[2] . "\t" . $x[0] . "\t" . $x[1]);
+       }
 }
 
+$dbh->do('TRUNCATE ratings');
+$dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @ratings));
+$dbh->pg_putcopyend();
+
+$dbh->do('CREATE TABLE new_covariance ( player1 smallint NOT NULL, player2 smallint NOT NULL, cov float NOT NULL )');
+$dbh->do('COPY new_covariance ( player1, player2, cov ) FROM STDIN');
+$dbh->pg_putcopydata(join("\n", @covariances));
+$dbh->pg_putcopyend();
+$dbh->do('ALTER TABLE new_covariance ADD PRIMARY KEY ( player1, player2 );');
+$dbh->do('DROP TABLE IF EXISTS covariance');
+$dbh->do('ALTER TABLE new_covariance RENAME TO covariance');
+
 $dbh->commit;
 unlink($tmpnam);