Python最后6个结果,删除las

2024-03-29 05:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我就是做不成。因此,我将张贴完整的代码。你知道吗

使用的.csv来自http://www.football-data.co.uk/mmz4281/1415/E0.csv

现在运行时,变量home\u team\u a、home\u team\u d、away\u team\u a和away\u team\u d基于之前的所有比赛,但我希望它们始终基于最后6场比赛。你知道吗

import csv, math, ast, numpy as np

def poisson(actual, mean):
    return math.pow(mean, actual) * math.exp(-mean) / math.factorial(actual)

csvFile = '20152016.csv'

team_list = []

k = open('team_list.txt', 'w')
k.write("""{
""")

csvRead = csv.reader(open(csvFile))
next(csvRead)

for row in csvRead:
    if row[2] not in team_list:
        team_list.append(row[2])
    if row[3] not in team_list:
        team_list.append(row[3])

team_list.sort()

for team in team_list:
    k.write(""" '%s': {'home_goals': 0, 'away_goals': 0, 'home_conceded': 0, 'away_conceded': 0, 'home_games': 0, 'away_games': 0, 'alpha_h': 0, 'beta_h': 0, 'alpha_a': 0, 'beta_a': 0},
""" % (team))

k.write("}")
k.close()

s = open('team_list.txt', 'r').read()
dict = ast.literal_eval(s)

GAMES_PLAYED = 0
WEEKS_WAIT = 4
TOTAL_VALUE = 0

csvRead = csv.reader(open(csvFile))
next(csvRead)

for game in csvRead:
    home_team = game[2]
    away_team = game[3]

    home_goals = int(game[4])
    away_goals = int(game[5])

    home_win_prob = 0
    draw_win_prob = 0
    away_win_prob = 0

    curr_home_goals = 0
    curr_away_goals = 0
    avg_home_goals = 1
    avg_away_goals = 1

    team_bet = ''
    ev_bet = ''

    # GETTING UPDATED VARIABLES
    for key, value in dict.items():
        curr_home_goals += dict[key]['home_goals']
        curr_away_goals += dict[key]['away_goals']

        if GAMES_PLAYED > (WEEKS_WAIT * 10):
            avg_home_goals = curr_home_goals / (GAMES_PLAYED)
            avg_away_goals = curr_away_goals / (GAMES_PLAYED)


    # CALCULATING FACTORS
    if GAMES_PLAYED > (WEEKS_WAIT * 10):
        home_team_a = (dict[home_team]['alpha_h'] + dict[home_team]['alpha_a']) / 2
        away_team_a = (dict[away_team]['alpha_h'] + dict[away_team]['alpha_a']) / 2

        home_team_d = (dict[home_team]['beta_h'] + dict[home_team]['beta_a']) / 2
        away_team_d = (dict[away_team]['beta_h'] + dict[away_team]['beta_a']) / 2

        home_team_exp = avg_home_goals * home_team_a * away_team_d
        away_team_exp = avg_away_goals * away_team_a * home_team_d


    # RUNNING POISSON   
        l = open('poisson.txt', 'w')

        for i in range(10):
            for j in range(10):
                prob = poisson(i, home_team_exp) * poisson(j, away_team_exp)
                l.write("Prob%s%s = %s\n" % (i, j, prob))

        l.close()

        with open('poisson.txt') as f:
            for line in f:

                home_goals_m = int(line.split(' = ')[0][4])
                away_goals_m = int(line.split(' = ')[0][5])

                prob = float(line.split(' = ')[1])

                if home_goals_m > away_goals_m:
                    home_win_prob += prob
                elif home_goals_m == away_goals_m:
                    draw_win_prob += prob
                elif home_goals_m < away_goals_m:
                    away_win_prob += prob

    #CALCULATE VALUE
        bet365odds_h, bet365odds_d, bet365odds_a = float(game[23]), float(game[24]), float(game[25])

        ev_h = (home_win_prob * (bet365odds_h - 1)) - (1 - home_win_prob)
        ev_d = (draw_win_prob * (bet365odds_d - 1)) - (1 - draw_win_prob)
        ev_a = (away_win_prob * (bet365odds_a - 1)) - (1 - away_win_prob)

        highestEV = max(ev_h, ev_d, ev_a)

        if (ev_h == highestEV) and (ev_h > 0):
            team_bet = home_team
            ev_bet = ev_h
            if home_goals > away_goals:
                TOTAL_VALUE += (bet365odds_h - 1)
            else:
                TOTAL_VALUE -= 1

        elif (ev_d == highestEV) and (ev_d > 0):
            team_bet = 'Draw'
            ev_bet = ev_d
            if home_goals == away_goals:
                TOTAL_VALUE += (bet365odds_d - 1)
            else:
                TOTAL_VALUE -= 1
        elif (ev_a == highestEV) and (ev_a > 0):
            team_bet = away_team
            ev_bet = ev_a
            if home_goals < away_goals:
                TOTAL_VALUE += (bet365odds_a - 1)
            else:
                TOTAL_VALUE -= 1

        if (team_bet != '') and (ev_bet != ''):
            print ("Bet on '%s' (EV = %s)" % (team_bet, ev_bet))    
            print (TOTAL_VALUE)

    # UPDATE VARIABLES AFTER MATCH HAS BEEN PLAYED
    dict[home_team]['home_goals'] += home_goals
    dict[home_team]['home_conceded'] += away_goals
    dict[home_team]['home_games'] += 1

    dict[away_team]['away_goals'] += away_goals
    dict[away_team]['away_conceded'] += home_goals
    dict[away_team]['away_games'] += 1

    GAMES_PLAYED += 1

    # CREATE FACTORS
    if GAMES_PLAYED > (WEEKS_WAIT * 10):
        for key, value in dict.items():
            alpha_h = (dict[key]['home_goals'] / dict[key]['home_games']) / avg_home_goals
            beta_h = (dict[key]['home_conceded'] / dict[key]['home_games']) / avg_away_goals

            alpha_a = (dict[key]['away_goals'] / dict[key]['away_games']) / avg_away_goals
            beta_a = (dict[key]['away_conceded'] / dict[key]['away_games']) / avg_home_goals

            dict[key]['alpha_h'] = alpha_h
            dict[key]['beta_h'] = beta_h
            dict[key]['alpha_a'] = alpha_a
            dict[key]['beta_a'] = beta_a

Tags: keyinalphahomeifwindictteam
3条回答

你可以这样做。。。。你知道吗

previous_index = 0
previous_max = 6 # max number of previous numbers to remember
previous = [None for _ in range(previous_max)]
csvFile = 'X.csv'
seen_records = 0
csvRead = csv.reader(open(csvFile))
# Enumerate over the records to keep track of the index of each one
for i, records in enumerate(csvRead):
    if (i > 50):
        seen_records =+ 1
        if previous_index == previous_max:
            previous_index = 0 # Reset to the beginning when we reach the end
        # Store the record and increment the index to the next location
        previous[previous_index] = record
        previous_index += 1

这将创建一个长度为previous_max的非常基本的数组,并将最早的数据存储在索引0处,最新的数据存储在previous_max -1。你知道吗

使用deque保存内存中最近的6项;添加新记录将“推出”最旧的记录。你知道吗

import collections
import itertools
import csv

with open("foo.csv") as fh:
    # Skip the first 44 rows
    csv_read = islice(csv.reader(fh), 44, None)

    # Initialize the deque with the next 6 rows
    d = collections.deque(islice(csv_read, 6), 6)

    for record in csv_read:
        d.append(record)
        print(list(d))  # Rows 46-51, then 47-52, then 48-53, etc

因为您将deque的最大长度设置为6,所以每个附加到“full”deque的deque都会推出旧的deque。在第一次迭代中,d.append推出第45行并添加第51行。在下一次迭代中,添加行52将推出行46,以此类推


一般来说,deque是一种数据结构,类似于队列和堆栈的组合;您可以有效地向任意一端添加或删除项,但访问任意项或修改“中间”的速度很慢。在这里,我们利用了一个事实,即附加到一个完整的deque会导致从另一端隐式删除。你知道吗

怎么样:

if seen_records == 200:
    recs = list(csvRead)[seen_records - 6:seen_records + 1]

相关问题 更多 >