<s> from pybrain . structure import SigmoidLayer <EOL> from pybrain . tools . shortcuts import buildNetwork <EOL> from pybrain . supervised . trainers import BackpropTrainer <EOL> from pybrain . datasets import ClassificationDataSet <EOL> from pybrain . utilities import percentError <EOL> from utils import ( get_matches , get_team_stats , extract_samples , normalize , <EOL> split_samples , team_year_key ) <EOL> class CrystalBall ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_features = None , output_feature = '<STR_LIT>' , <EOL> exclude_ties = True , duplicate_with_reversed = True , <EOL> recent_years = <NUM_LIT:2> ) : <EOL> if input_features is None : <EOL> input_features = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> self . input_features = input_features <EOL> self . output_feature = output_feature <EOL> self . exclude_ties = exclude_ties <EOL> self . duplicate_with_reversed = duplicate_with_reversed <EOL> self . recent_years = recent_years <EOL> def read_data ( self ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' <EOL> self . team_stats = get_team_stats ( self . recent_years ) <EOL> print '<STR_LIT>' <EOL> self . matches = get_matches ( <EOL> with_team_stats = True , <EOL> duplicate_with_reversed = self . duplicate_with_reversed , <EOL> exclude_ties = self . exclude_ties , <EOL> recent_years = self . recent_years , <EOL> use_these_team_stats = self . team_stats , <EOL> ) <EOL> def prepare_learning ( self ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' <EOL> self . inputs , self . outputs = extract_samples ( self . matches , <EOL> self . input_features , <EOL> self . output_feature ) <EOL> print '<STR_LIT>' <EOL> self . normalizer , self . inputs = normalize ( self . inputs ) <EOL> print '<STR_LIT>' <EOL> self . train_inputs , self . train_outputs , self . test_inputs , self . test_outputs = split_samples ( self . inputs , self . outputs ) <EOL> print '<STR_LIT>' <EOL> self . network = buildNetwork ( len ( self . input_features ) , <EOL> <NUM_LIT:2> * len ( self . input_features ) , <EOL> <NUM_LIT:1> , <EOL> outclass = SigmoidLayer , <EOL> bias = True ) <EOL> print '<STR_LIT>' <EOL> self . train_set = ClassificationDataSet ( len ( self . input_features ) ) <EOL> for i , input_line in enumerate ( self . train_inputs ) : <EOL> self . train_set . addSample ( self . train_inputs [ i ] , <EOL> [ self . train_outputs [ i ] - <NUM_LIT:1> ] ) <EOL> self . trainer = BackpropTrainer ( self . network , dataset = self . train_set , <EOL> momentum = <NUM_LIT:0.5> , weightdecay = <NUM_LIT:0.0> ) <EOL> self . train_set . assignClasses ( ) <EOL> def train ( self , iterations = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> for _ in range ( iterations ) : <EOL> self . trainer . train ( ) <EOL> self . test_network ( ) <EOL> def predict ( self , year , team1 , team2 ) : <EOL> """<STR_LIT>""" <EOL> inputs = [ ] <EOL> for feature in self . input_features : <EOL> stat_name = feature . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if stat_name in self . team_stats . columns . values : <EOL> if '<STR_LIT>' in feature : <EOL> stat_year = '<STR_LIT>' <EOL> else : <EOL> stat_year = year <EOL> team = team1 if '<STR_LIT>' in feature else team2 <EOL> team_year = team_year_key ( team , stat_year ) <EOL> value = self . team_stats . loc [ team_year , stat_name ] <EOL> elif feature == '<STR_LIT>' : <EOL> value = year <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + feature ) <EOL> inputs . append ( value ) <EOL> inputs = self . normalizer . transform ( inputs ) <EOL> result = self . neural_result ( inputs ) <EOL> if result == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif result == <NUM_LIT:1> : <EOL> return team1 <EOL> elif result == <NUM_LIT:2> : <EOL> return team2 <EOL> else : <EOL> return '<STR_LIT>' + str ( result ) <EOL> def neural_result ( self , input ) : <EOL> """<STR_LIT>""" <EOL> n_output = self . network . activate ( input ) <EOL> if n_output >= <NUM_LIT:0.5> : <EOL> return <NUM_LIT:2> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def test_network ( self ) : <EOL> """<STR_LIT>""" <EOL> train_accuracy = <NUM_LIT:100> - percentError ( map ( self . neural_result , <EOL> self . train_inputs ) , <EOL> self . train_outputs ) <EOL> print '<STR_LIT>' , train_accuracy <EOL> test_accuracy = <NUM_LIT:100> - percentError ( map ( self . neural_result , <EOL> self . test_inputs ) , <EOL> self . test_outputs ) <EOL> print '<STR_LIT>' , test_accuracy <EOL> print '<STR_LIT:#>' * int ( train_accuracy ) , '<STR_LIT>' <EOL> print '<STR_LIT:#>' * int ( test_accuracy ) , '<STR_LIT>' </s>
