python - 在 Python 中评估体育比赛估算的最优雅的方法是什么?

标签 python

我想评估体育比赛的估计 - 在我的例子中是足球(即足球)比赛。我想为此使用 Python。

基本上,总有一个 team_home结果,一个team_away结果,estimate_homeestimate_away 。例如,一场比赛结束 1:0估计是 0:0 - 这将返回 wrong .

只有四种可能的情况和结果:

  1. wrong正如上面的例子
  2. tendency对获胜者的估计是正确的,但进球差不是正确的(例如 3:0 )
  3. goal difference正确的净胜球,例如 2:1
  4. right以获得准确的估计

在 Python 中处理估计和结果的最优雅的方法是什么?

最佳答案

另一个答案,反射(reflect)了我对优雅的看法(一个相当主观的参数,我同意)。我想让我的对象由类定义,用 OOP 构建牢记在心,并带有 ORM管理对象之间的关系。这带来了许多优点和更清晰的代码。

我正在使用pony ORM在这里,但还有许多其他出色的选择(最终会获得更宽松的许可),例如 SQLAlchemyDjango's ORM .

这是一个完整的示例 - 首先我们定义模型:

from pony.orm import *

class Player(db.Entity):
    """A player is somebody who place a bet, identified by its name."""
    name = Required(unicode)
    score = Required(int, default=0)
    bets = Set('Bet', reverse='player')
    # any other player's info can be stored here


class Match(db.Entity):
    """A Match is a game, played or not yet played."""

    ended = Required(bool, default=False)
    home_score = Required(int, default=0)
    visitors_score = Required(int, default=0)

    bets = Set('Bet', reverse='match')


class Bet(db.Entity):
    """A class that stores a bet for a specific game"""

    match = Required(Match, reverse="bets")
    home_score = Required(int, default=0)
    visitors_score = Required(int, default=0)
    player = Required(Player, reverse="bets")

@db_session
def calculate_wins(match):
    bets = select(b for b in Bet if b.match == match)[:]
    for bet in bets:
        if (match.home_score == bet.home_score) and (match.visitors_score == bet.visitors_score):
            bet.player.score += 3  # exact
        elif (match.home_score - match.visitors_score) == (bet.home_score - bet.visitors_score):
            bet.player.score += 2  # goal differences
        elif ((match.home_score > match.visitors_score) == (bet.home_score > bet.visitors_score)) and \
           (match.home_score != match.visitors_score) and (bet.home_score != bet.visitors_score):
            bet.player.score += 1  # tendency
        else:
            bet.player.score += 0  # wrong

通过这些类,您可以创建和更新您的比赛、球员、投注数据库。 如果您需要统计和数据聚合/排序,您可以根据您的需要查询数据库。

db = Database('sqlite', ':memory:')  # you may store it on a file if you like
db.generate_mapping(create_tables=True)

player1 = Player(name='furins')
player2 = Player(name='Martin')

match1 = Match()

furins_bet = Bet(match=match1, player=player1, home_score=0, visitors_score=0)
martin_bet = Bet(match=match1, player=player2, home_score=3, visitors_score=0)


# the game begins ...
match1.home_score = 1
match1.visitors_score = 0
# the game ended ...
match1.ended = True

commit() #let's update the database


calculate_wins(match1)

print("furins score: %d"%(player1.score)) # returns 0
print("Martin score: %d"%(player2.score)) # returns 1

如果您愿意,您最终甚至可以使用 numpy 集成非常复杂的时间序列数据分析,正如 Carst 所建议的那样,但我相信这些添加 - 尽管非常有趣 - 相对于您最初的问题来说有点 OT。

关于python - 在 Python 中评估体育比赛估算的最优雅的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828856/

相关文章:

python - ValueError : Expected 1D or 2D array, 得到了 0D 数组

Python - 从旋转角度对 OpenCV 进行透视变换

python - 将 Fortran 77 代码转换为 Python

python - 导入错误: No module named 'settings'

执行大型案例/切换的 Pythonic 方式

python - Amazon MWS Boto 解析 XML 缺失值

python - 线程和多处理组合 [Python]

python - 使 Python 中的 GStreamer 视频/音频流畅和循环

python - 基于 __hash__ 设置更改隐式顺序?

python statsmodels ARMAplot_predict