def cross_validate(args): # import some functions encode = Data_Loader().encode find_error = NFL_Predictor().compareVector try: nn = Neural_Network.createFromFile(args.file) print "Loaded Neural Network with %i hidden nodes" % len(nn.hidden_nodes) totalCorrect = 0.0 total_tested = 0.0 for y in range(args.start,args.end+1): classRight = [0, 0, 0, 0, 0, 0] correct = incorrect = 0 if(args.db == 'u'): dl = Data_Loader() teams = dl.getAllTeams(y) elif(args.db == 'b'): dl = Data_Loader() teams = dl.getAllTeams(y) elif(args.db == 'p'): dl = Data_Loader('playoffTeams.csv') teams = dl.getAllTeams(y) elif(args.db == 'o'): dl = Data_Loader('balancedData.csv') teams = dl.getAllTeams(y) total_tested += len(teams) total_error = 0.0 for t in teams: t.result = nn.feed_forward(t.stats) error = (find_error(t.result, encode(t.classification))) total_error += error**2 if error < .08: correct += 1 classRight if args.v: print "team %s, results %s, class %s, error %s" % (t.name, t.result, encode(t.classification), error) if not args.q: print "%d \t within threshold: %d/%d \t error: %s" % (y, correct, len(teams), str(total_error)) totalCorrect += correct print "totalCorrect: %i/%i, %.2f%%" % (totalCorrect, total_tested, (totalCorrect/total_tested)*100) except Exception as e: print "invalid formatting, consult neural_main.py c --help \nError: %s" % e
def train(args): try: inputs = [] targets = [] y = range(args.start,args.end+1) if(args.db == 'u'): dl = Data_Loader() i,t = dl.getTargets(y) elif(args.db == 'b'): dl = Data_Loader() i,t = dl.getBalancedTargets(y) elif(args.db == 'p'): dl = Data_Loader('playoffTeams.csv') i,t = dl.getTargets(y) elif(args.db == 'o'): dl = Data_Loader('balancedData.csv') i,t = dl.getTargets(y) elif(args.db == 's'): dl = Data_Loader() i,t = dl.getBLSmoteTargets(y,.25) #i,t = dl.getSmoteTargets(y) inputs += i targets += t #create NN # if file already exists, build on that training if (os.path.exists(args.file)): print "file exists" nn = Neural_Network.createFromFile(args.file) pass else: print "file does not exist" nn = Neural_Network.createWithRandomWeights(len(inputs[0]),args.nodes,len(targets[0])) #train NN with the given data print 'Beginning Training...' nn = nn.train(args.epochs,inputs,targets,args.learn_rate) nn.saveToFile(args.file) print "Neural Network saved to %s" % (args.file) except Exception as e: print "invalid formatting, consult neural_main.py t --help \n Error: %s" % e
def predict(args): try: nn = Neural_Network.createFromFile(args.file) dl = Data_Loader() #team = dl.getTeam(args.team, args.year) team = dl.getTeam(args.team, args.year) result = nn.feed_forward(team.stats) print "\n\nPredicting the %i %s..." % (args.year, team_dict.teams[args.team]) print "RESULTS: %.3f\n" % result[0] if args.show_expected: print "EXPECTED: %s" % (dl.encode(team.classification))[0] results_graph = "\t|" + "".join(repeat("-",int(result[0]/.8*50))) + "|" + "".join(repeat("-",(int((1-result[0]/.8)*50)))) + "|" print " Not in playoffs" + "".join(repeat(" ",35)) + "Super Bowl Champs" print results_graph post_processor = NFL_Predictor(nn) similar_teams = post_processor.compareWithPastTeams(dl.getEveryTeam(), team, 16) print"\nThe 15 most similar teams throughout history:" del similar_teams[0] for t in similar_teams: print "%s \tScore: %f" % t except Exception as e: print "invalid formatting, consult neural_main.py t --help \n Error: %s" % e
DL = Data_Loader() ''' nn = Neural_Network.createWithRandomWeights(66,40,6) # train! with learning rate proportional to # of teams in the situations inputs = [] targets = [] for y in range(2005,2007): i,t = DL.getTargets(y) inputs += i targets += t #print targets nn = nn.train(10000,inputs,targets,1.5) nn.saveToFile("predictortest.txt") ''' nn = Neural_Network.createFromFile("predictortest.txt") teams_2011 = DL.getAllTeams(2011) pats_2011 = filter(lambda t: t.name == "nwe", teams_2011)[0] all_other_teams = filter(lambda t: t.name != "nwe", teams_2011) predictor = NFL_Predictor(nn) similar = predictor.compareWithPastTeams(all_other_teams, pats_2011, 3) for t,d in similar: print t.name + " " + str(d) + "\n"