Permalink
Browse files

added sofia ml based logistic regression predictor

  • Loading branch information...
rrenaud committed Sep 24, 2011
1 parent 63ac6a2 commit 7d0911fc15587947bcce41ecf617e6ab263d31b8
Showing with 35 additions and 29 deletions.
  1. +26 −20 annotate_game.py
  2. +3 −3 game_state_features.py
  3. +1 −4 rf_predict.py
  4. +5 −2 static/game_viewer.js
View
@@ -6,13 +6,21 @@
import game
import parse_game
-# import rf_predict
+import sofia_predict
def _pretty_format_html(v):
return '<br>' + pprint.pformat(v).replace(
'\n', '<br>').replace(' ', '&nbsp')
-# win_predictor = rf_predict.WinPredictor()
+win_predictor = sofia_predict.SofiaWinPredictor('data/logreg-peg.model')
+
+def make_graph(label, div_name):
+ return """
+ <tr>
+ <td width=50px>%s</td>
+ <td><div id="%s" style="width:1000px;height:250px;"></div></td>
+ </tr>
+""" % (label, div_name)
def annotate_game(contents, game_id, debug=False):
""" Decorate game contents with some JS that makes a score keeper
@@ -22,12 +30,13 @@ def annotate_game(contents, game_id, debug=False):
states = []
game_val = game.Game(parsed_game)
- # predictions = win_predictor.predict_all_turns(game_val)
- for game_state in game_val.game_state_iterator():
- #itertools.izip(game_val.game_state_iterator(),
- # predictions):
+ win_prob_enabled = len(game_val.get_player_decks()) == 2
+ if win_prob_enabled:
+ predictions = win_predictor.predict_all_turns(game_val)
+
+ for idx, game_state in enumerate(game_val.game_state_iterator()):
encoded = game_state.encode_game_state()
- #encoded['win_prob'] = win_prob
+ encoded['win_prob'] = predictions[idx] if win_prob_enabled else 0
states.append(encoded)
parsed_game['game_states'] = states
@@ -94,23 +103,20 @@ def annotate_game(contents, game_id, debug=False):
else:
before_end = turn_chunk.find('</html')
ret += turn_chunk[turn_chunk.find('\n'): before_end]
+ win_prob_graph = ''
+
+ if win_prob_enabled:
+ win_prob_graph = make_graph('win prob', 'win-prob-graph')
ret += """
<div id="end-game"></div>
<table>
- <tr>
- <td width=50px>score</td>
- <td><div id="score-graph" style="width:1000px;height:250px;"></div></td>
- </tr>
- <tr>
- <td width=50px>money</td>
- <td><div id="money-graph" style="width:1000px;height:250px;"></div></td>
- </tr>
+ %s
+ %s
+ %s
</table>
-"""
- # <tr>
- # <td width=50px>win prob</td>
- # <td><div id="win-prob-graph" style="width:1000px;height:250px;"></div></td>
- # </tr>
+""" % (make_graph('score', 'score-graph'), make_graph('money', 'money-graph'),
+ win_prob_graph)
+
ret += '</div>&nbsp<br>\n' * 10
ret += '</html>'
return ret
View
@@ -159,7 +159,7 @@ def main():
force_classification = True
- prefix = 'data/test_huge_'
+ prefix = 'data/test_small_'
# limit = 10000
r_output_file = open(prefix + 'r_format.data', 'w')
weka_output_file = open(prefix + 'games.arff', 'w')
@@ -171,8 +171,8 @@ def main():
for raw_game in utils.progress_meter(
c.test.games.find(
- {'_id': {'$gt': 'game-20110715'} }
- ), 100):
+ {'_id': {'$gt': 'game-2010-10'} }
+ ).limit(20000), 100):
g = game.Game(raw_game)
if g.dubious_quality() or len(g.get_player_decks()) != 2:
continue
View
@@ -16,10 +16,7 @@ def predict_all_turns(self, game_val):
encoded_states = []
for game_state in game_val.game_state_iterator():
encoded_state = (
- convert_to_r_fmt.encode_state_r_fmt(game_val, game_state,
- False))
- encoded_states.append(convert_to_r_fmt.encode_diff(
- encoded_state, False))
+ convert_to_r_fmt.encode_state_r_fmt(game_val, game_state)
# convert the data into this funny column vector format so that we
# can turn it into a data frame.
View
@@ -96,8 +96,11 @@ function DecorateGame() {
$.plot($('#score-graph'), MakeSeriesForPlayers(ScoreExtractor), graph_opts);
$.plot($('#money-graph'), MakeSeriesForPlayers(MoneyExtractor), graph_opts);
- //$.plot($('#win-prob-graph'),
- // MakeSeriesForPlayers(WinProbExtractor), graph_opts);
+
+ graph_opts.yaxis.position = { max: 1.0 };
+
+ $.plot($('#win-prob-graph'),
+ MakeSeriesForPlayers(WinProbExtractor), graph_opts);
$(window).scroll(UpdateDisplay);
}

0 comments on commit 7d0911f

Please sign in to comment.