Пример #1
0
    def train_network(self):
        """ Pure virtual method for training the network
        """
        db_query = self._database_session.query(PregamePitcherGameEntry)
        mlb_training_data, mlb_evaluation_data = self.get_train_eval_data(
            db_query, 0.8)
        X_train, Y_train = self.get_stochastic_batch(mlb_training_data,
                                                     self.SIZE_TRAINING_BATCH)
        self._decision_tree.fit(X_train, Y_train)
        dot_data = StringIO()
        tree.export_graphviz(
            self._decision_tree,
            out_file=dot_data,
            feature_names=PregamePitcherGameEntry.get_input_vector_labels())
        graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
        graph.write_pdf("pitcher_tree.pdf")
        x_test_actual = list()
        y_test_actual = list()
        for data in mlb_evaluation_data:
            postgame_entry = self._database_session.query(
                PostgamePitcherGameEntry).get(
                    (data.rotowire_id, data.game_date))

            if postgame_entry is None:
                print "Ignoring hitter %s since his postgame stats were not found." % data.rotowire_id
                continue

            y_test_actual.append([postgame_entry.actual_draftkings_points])
            x_test_actual.append(data.to_input_vector())

        self._database_session.close()
Пример #2
0
    def predict_daily_points(database_session, day=None):
        if day is None:
            day = date.today()
        #hitter_regression = HitterRegressionTrainer()
        hitter_regression = HitterRegressionForestTrainer()
        hitter_regression.train_network()
        #pitcher_regression = PitcherRegressionTrainer()
        pitcher_regression = PitcherRegressionForestTrainer()
        pitcher_regression.train_network()
        daily_entries = PregameHitterGameEntry.get_all_daily_entries(
            database_session, day)
        for daily_entry in daily_entries:
            predicted_points = hitter_regression.get_prediction(
                daily_entry.to_input_vector())

            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()

        daily_entries = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, day)
        for daily_entry in daily_entries:
            predicted_points = pitcher_regression.get_prediction(
                daily_entry.to_input_vector())
            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()
    def train_network(self):
        """ Pure virtual method for training the network
        """
        db_query = self._database_session.query(PregamePitcherGameEntry)
        mlb_training_data, mlb_evaluation_data = self.get_train_eval_data(db_query, 0.8)
        X_train, Y_train = self.get_stochastic_batch(mlb_training_data, self.SIZE_TRAINING_BATCH)
        self._decision_tree.fit(X_train, Y_train)
        dot_data = StringIO()
        tree.export_graphviz(self._decision_tree, out_file=dot_data,
                             feature_names=PregamePitcherGameEntry.get_input_vector_labels())
        graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
        graph.write_pdf("pitcher_tree.pdf")
        x_test_actual = list()
        y_test_actual = list()
        for data in mlb_evaluation_data:
            try:
                postgame_entry = self._database_session.query(PostgamePitcherGameEntry).filter(PostgamePitcherGameEntry.rotowire_id == data.rotowire_id,
                                                                                              PostgamePitcherGameEntry.game_date == data.game_date).one()
                y_test_actual.append([postgame_entry.actual_draftkings_points])
                x_test_actual.append(data.to_input_vector())
            except NoResultFound:
                print "Ignoring hitter %s since his postgame stats were not found." % data.rotowire_id
                continue

        self._database_session.close()
Пример #4
0
    def predict_daily_points(database_session, day=None):
        if day is None:
            day = date.today()
        #hitter_regression = HitterRegressionTrainer()
        hitter_regression = HitterRegressionForestTrainer()
        hitter_regression.train_network()
        #pitcher_regression = PitcherRegressionTrainer()
        pitcher_regression = PitcherRegressionForestTrainer()
        pitcher_regression.train_network()
        daily_entries = PregameHitterGameEntry.get_all_daily_entries(database_session, day)
        for daily_entry in daily_entries:
            predicted_points = hitter_regression.get_prediction(daily_entry.to_input_vector())

            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()

        daily_entries = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        for daily_entry in daily_entries:
            hitter_array = daily_entry.get_opponent_vector(database_session)
            final_array = np.concatenate([daily_entry.to_input_vector(), hitter_array])
            predicted_points = pitcher_regression.get_prediction(final_array)
            if predicted_points < 0:
                predicted_points = 0
            daily_entry.predicted_draftkings_points = predicted_points
            database_session.commit()
Пример #5
0
    def update_salaries(database_session, csv_dict=None, game_date=None):
        if game_date is None:
            game_date = date.today()
        if csv_dict is None:
            csv_dict = Draftkings.get_csv_dict()
        #Hitters
        pregame_hitters = PregameHitterGameEntry.get_all_daily_entries(
            database_session, game_date)
        for pregame_entry in pregame_hitters:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            hitter_entry = database_session.query(HitterEntry).get(
                pregame_entry.rotowire_id)
            if hitter_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(hitter_entry.first_name + " " +
                                      hitter_entry.last_name +
                                      hitter_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                positions = csv_entry["Position"].split("/")
                pregame_entry.primary_position = positions[0]
                pregame_entry.secondary_position = positions[len(positions) -
                                                             1]
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (
                    hitter_entry.first_name + " " + hitter_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()

        # Pitchers
        pregame_pitchers = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, game_date)
        for pregame_entry in pregame_pitchers:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            pitcher_entry = database_session.query(PitcherEntry).get(
                pregame_entry.rotowire_id)
            if pitcher_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(pitcher_entry.first_name + " " +
                                      pitcher_entry.last_name +
                                      pitcher_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (
                    pitcher_entry.first_name + " " + pitcher_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()
Пример #6
0
    def update_salaries(database_session, csv_dict=None, game_date=None):
        if game_date is None:
            game_date = date.today()
        if csv_dict is None:
            csv_dict = Draftkings.get_csv_dict()
        #Hitters
        pregame_hitters = PregameHitterGameEntry.get_all_daily_entries(database_session, game_date)
        for pregame_entry in pregame_hitters:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            hitter_entry = database_session.query(HitterEntry).get(pregame_entry.rotowire_id)
            if hitter_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(hitter_entry.first_name + " " + hitter_entry.last_name + hitter_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                positions = csv_entry["Position"].split("/")
                pregame_entry.primary_position = positions[0]
                pregame_entry.secondary_position = positions[len(positions) - 1]
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (hitter_entry.first_name + " " + hitter_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()

        # Pitchers
        pregame_pitchers = PregamePitcherGameEntry.get_all_daily_entries(database_session, game_date)
        for pregame_entry in pregame_pitchers:
            # Lookup the player's name in the database
            # Lookup the name in the dictionary
            pitcher_entry = database_session.query(PitcherEntry).get(pregame_entry.rotowire_id)
            if pitcher_entry is None:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % pregame_entry.rotowire_id
            try:
                csv_entry = csv_dict[(pitcher_entry.first_name + " " + pitcher_entry.last_name + pitcher_entry.team).lower()]
                pregame_entry.draftkings_salary = int(csv_entry["Salary"])
                database_session.commit()
            except KeyError:
                print "Player %s not found in the Draftkings CSV file. Deleting entry." % (pitcher_entry.first_name + " " + pitcher_entry.last_name)
                database_session.delete(pregame_entry)
                database_session.commit()
Пример #7
0
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return: an OptimalLineupDict structure
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = list()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(
                database_session, fielding_position, day)
            query_results = list(
                query_results.order_by(
                    desc(PregameHitterGameEntry.predicted_draftkings_points)))
            if fielding_position == "OF":
                while len(optimal_lineup["OF"]
                          ) < OptimalLineupDict.MAX_OUTFIELDERS and len(
                              query_results) > 0:
                    candidate_player = heapq.heappop(query_results)
                    if not optimal_lineup.is_in_dict(candidate_player):
                        optimal_lineup.add(candidate_player)
            else:
                try:
                    candidate_player = heapq.heappop(query_results)
                except IndexError:
                    print "Exception."
                if not optimal_lineup.is_in_dict(candidate_player):
                    optimal_lineup.add(candidate_player)
            for player in query_results:
                try:
                    heapq.heappush(
                        player_heap,
                        (-player.predicted_draftkings_points, player))
                except ZeroDivisionError:
                    continue

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(
            database_session, day)
        query_results = list(
            query_results.order_by(
                desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for i in range(0, OptimalLineupDict.MAX_PITCHERS):
            candidate_player = heapq.heappop(query_results)
            if not optimal_lineup.is_in_dict(candidate_player):
                optimal_lineup.add(candidate_player)

        for pitcher in query_results:
            try:
                heapq.heappush(player_heap,
                               (-pitcher.predicted_draftkings_points, pitcher))
            except ZeroDivisionError:
                continue

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            next_player = heapq.heappop(player_heap)[1]
            if not optimal_lineup.is_in_dict(next_player):
                optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        print "Runner-up players"
        for player in player_heap:
            print player[1]
        print " "

        # Commit the prediction to the database
        lineup_db_entry = LineupEntry()
        lineup_db_entry.game_date = date.today()
        lineup_db_entry.game_time = datetime.now().strftime("%H:%M:%S")
        lineup_db_entry.starting_pitcher_1 = optimal_lineup["SP"][0][
            1].rotowire_id
        lineup_db_entry.starting_pitcher_2 = optimal_lineup["SP"][1][
            1].rotowire_id
        lineup_db_entry.catcher = optimal_lineup["C"].rotowire_id
        lineup_db_entry.first_baseman = optimal_lineup["1B"].rotowire_id
        lineup_db_entry.second_baseman = optimal_lineup["2B"].rotowire_id
        lineup_db_entry.third_baseman = optimal_lineup["3B"].rotowire_id
        lineup_db_entry.shortstop = optimal_lineup["SS"].rotowire_id
        lineup_db_entry.outfielder_1 = optimal_lineup["OF"][0][1].rotowire_id
        lineup_db_entry.outfielder_2 = optimal_lineup["OF"][1][1].rotowire_id
        lineup_db_entry.outfielder_3 = optimal_lineup["OF"][2][1].rotowire_id
        database_session.add(lineup_db_entry)
        database_session.commit()

        return optimal_lineup
Пример #8
0
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return:
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = list()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(database_session, fielding_position, day)
            query_results = list(query_results.order_by(desc(PregameHitterGameEntry.predicted_draftkings_points)))
            if fielding_position == "OF":
                while len(optimal_lineup["OF"]) < OptimalLineupDict.MAX_OUTFIELDERS and len(query_results) > 0:
                    candidate_player = heapq.heappop(query_results)
                    if not optimal_lineup.is_in_dict(candidate_player):
                        optimal_lineup.add(candidate_player)
            else:
                candidate_player = heapq.heappop(query_results)
                if not optimal_lineup.is_in_dict(candidate_player):
                    optimal_lineup.add(candidate_player)
            for player in query_results:
                try:
                    dollars_per_point = OptimalLineupDict.dollars_per_point(player)
                    if dollars_per_point > 0:
                        heapq.heappush(player_heap, (dollars_per_point, player))
                except ZeroDivisionError:
                    continue

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        query_results = list(query_results.order_by(desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for i in range(0, OptimalLineupDict.MAX_PITCHERS):
            candidate_player = heapq.heappop(query_results)
            if not optimal_lineup.is_in_dict(candidate_player):
                optimal_lineup.add(candidate_player)

        for pitcher in query_results:
            try:
                dollars_per_point = OptimalLineupDict.dollars_per_point(pitcher)
                if dollars_per_point > 0:
                    heapq.heappush(player_heap, (OptimalLineupDict.dollars_per_point(pitcher), pitcher))
            except ZeroDivisionError:
                continue

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            # TODO: we should add the player back on the player heap when kicked out of lineup
            next_player = heapq.heappop(player_heap)[1]
            if not optimal_lineup.is_in_dict(next_player):
                optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        print "Runner-up players"
        for player in player_heap:
            print player[1]
        print " "

        return optimal_lineup
Пример #9
0
    def get_optimal_lineup(database_session, day=None):
        """ Get the optimal lineup of the players to choose for tonight
        :param database_session: SQLAlchemy database session
        :return: an OptimalLineupDict structure
        """
        if day is None:
            day = date.today()
        optimal_lineup = OptimalLineupDict()
        player_heap = dict()

        # Look for the hitter entries
        for fielding_position in OptimalLineupDict.FieldingPositions:
            query_results = PregameHitterGameEntry.get_daily_entries_by_position(database_session, fielding_position, day)
            query_results = list(query_results.order_by(desc(PregameHitterGameEntry.predicted_draftkings_points)))
            query_result_heap = list()
            for query_result in query_results:
                heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result))
            while not optimal_lineup.position_map[fielding_position].is_valid() and len(query_results) > 0:
                candidate_player = heapq.heappop(query_result_heap)[1]
                optimal_lineup.add(candidate_player)

            player_heap[fielding_position] = list()
            while len(query_result_heap) > 0:
                player = heapq.heappop(query_result_heap)
                heapq.heappush(player_heap[fielding_position], player)

        # Look for pitchers
        query_results = PregamePitcherGameEntry.get_all_daily_entries(database_session, day)
        query_results = list(query_results.order_by(desc(PregamePitcherGameEntry.predicted_draftkings_points)))
        for query_result in query_results:
            heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result))
        while not optimal_lineup.position_map["SP"].is_valid() and len(query_results) > 0:
            candidate_player = heapq.heappop(query_result_heap)[1]
            optimal_lineup.add(candidate_player)

        player_heap["SP"] = list()
        while len(query_result_heap) > 0:
            player = heapq.heappop(query_result_heap)
            heapq.heappush(player_heap["SP"], player)

        # Replace players one by one who are "overpaid" based on predicted points per dollar
        while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \
                not optimal_lineup.is_valid():
            worst_position = optimal_lineup.get_worst_position()
            next_player = heapq.heappop(player_heap[worst_position])[1]
            optimal_lineup.add(next_player)

        # Print out all the remaining players in order of their value
        runner_up_text = "Runner-up players\n"
        for fielding_position in OptimalLineupDict.FieldingPositions:
            runner_up_text += fielding_position + "\n"
            while len(player_heap[fielding_position]) > 0:
                player = heapq.heappop(player_heap[fielding_position])
                runner_up_text += "%s\n" % str(player[1])
            runner_up_text += "\n"

        runner_up_text += "SP\n"
        while len(player_heap["SP"]) > 0:
            player = heapq.heappop(player_heap["SP"])
            runner_up_text += "%s\n" % str(player[1])

        send_email(runner_up_text)
        print runner_up_text

        # Commit the prediction to the database
        lineup_db_entry = LineupEntry()
        lineup_db_entry.game_date = date.today()
        lineup_db_entry.game_time = datetime.now().strftime("%H:%M:%S")
        lineup_db_entry.starting_pitcher_1 = optimal_lineup.position_map["SP"]._position_heap[0][1].rotowire_id
        lineup_db_entry.starting_pitcher_2 = optimal_lineup.position_map["SP"]._position_heap[1][1].rotowire_id
        lineup_db_entry.catcher = optimal_lineup.position_map["C"]._player.rotowire_id
        lineup_db_entry.first_baseman = optimal_lineup.position_map["1B"]._player.rotowire_id
        lineup_db_entry.second_baseman = optimal_lineup.position_map["2B"]._player.rotowire_id
        lineup_db_entry.third_baseman = optimal_lineup.position_map["3B"]._player.rotowire_id
        lineup_db_entry.shortstop = optimal_lineup.position_map["SS"]._player.rotowire_id
        lineup_db_entry.outfielder_1 = optimal_lineup.position_map["OF"]._position_heap[0][1].rotowire_id
        lineup_db_entry.outfielder_2 = optimal_lineup.position_map["OF"]._position_heap[1][1].rotowire_id
        lineup_db_entry.outfielder_3 = optimal_lineup.position_map["OF"]._position_heap[2][1].rotowire_id
        database_session.add(lineup_db_entry)
        database_session.commit()

        return optimal_lineup