使用Python进行体育竞技分析(预测球队成绩)
Python  /  管理员 发布于 7年前   234
今天我们用python进行体育竞技分析,预测球队成绩
一. 体育竞技分析的IPO模式 :
输入I(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来表示,
一个能力值为0.8的球员,在他发球时,有80%的可能性赢得1分)
处理P(process):模拟比赛过程
输出O(output):两个球员获胜的概率
该体育竞技程序,我们采用自顶向下的设计方法。
自顶向下的设计是一种解决复杂问题的行之有效的方法。其步骤如下
自顶向下设计的基本思想,如下图:
二.我们首先采用兵乓球的比赛规则
一局比赛中,先得11分的一方为胜方,如果10平后,则比对方多得两分为胜方
一场比赛中,采用7局四胜的方式
代码如下:
# -*- coding: utf-8 -*-"""Created on Wed May 15 12:49:17 2019@author: moyulin"""from random import randomdef printIntro(): print("BY 2018310143103") print("这个程序模拟两个选手A和B的兵乓球比赛") print("程序运行需要A和B的能力值(以0到1之间的小数表示)")def getInputs(): a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) n = eval(input("请输入模拟比赛的局数: ")) return a, b, ndef simNGames(n, probA, probB): WinsA, WinsB = 0, 0 winsA, winsB = 0, 0 for i in range(1,n+1): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 if i%7==0: if winsA>winsB: WinsA+=1 print("单打第{}场胜利的为A".format(int(i/7))) else: WinsB+=1 print("单打第{}场胜利的为B".format(int(i/7))) winsA,winsB=0,0 return WinsA, WinsBdef gameOver(a,b): if a>=10 and b>=10: if abs(a-b)==2: return True if a<10 or b<10: if a==11 or b==11: return True else: return Falsedef simOneGame(probA, probB): scoreA, scoreB = 0, 0 serving = "A" while not gameOver(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: scoreB +=1 serving="B" else: if random() < probB: scoreB += 1 else: scoreA += 1 serving="A" return scoreA, scoreBdef printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main(): printIntro() probA, probB, n = getInputs() WinsA, WinsB = simNGames(n, probA, probB) printSummary(WinsA, WinsB)main()
运行结果如下:
三.运用pyinstaller打包应用程序,使之可运行
win+cmd打开命令行
1.安装pyinstaller库
pip install pyinstaller
安装完成后就可以使用了,下面介绍pyinstaller的部分使用方法
-F, Conefile 打包一个单个文件,如果你的代码都写在一个.py文件的话,可以用这个,如果是多个.py文件就别用
-D, Conedir 打包多个文件,在dist中生成很多依赖文件,适合以框架形式编写工具代码,我个人比较推荐这样,代码易于维护
-K, Ctk 在部署时包含 TCL/TK
-a, Cascii 不包含编码.在支持Unicode的python版本上默认包含所有的编码.
-d, Cdebug 产生debug版本的可执行文件
-w,Cwindowed,Cnoconsole 使用Windows子系统执行.当程序启动的时候不会打开命令行(只对Windows有效)
-c,Cnowindowed,Cconsole
2.打开命令行使用
输入
pyinstaller -F C:\#py文件地址
图例
最后回到根目录上会看到dist文件夹,里面有个exe文件,直接运行即可,如图
四.模拟体育竞技分析之篮球
假设谁先获得100分谁胜利
代码如下
from random import randomdef printIntro(): print("by 2018310143103") print("这个程序模拟两个队A和B的篮球比赛") print("程序运行需要队A和队B的能力值(以0到1之间的小数表示)")def getInputs(): a = eval(input("请输入队A的能力值(0-1): ")) b = eval(input("请输入队B的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a, b, ndef simNGames(n, probA, probB): winsA, winsB = 0, 0 for i in range(n): scoreA, scoreB = simOneGame(probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsBdef gameOver(a,b): return a==100 or b==100def simOneGame(probA, probB): scoreA, scoreB = 0, 0 serving = "A" while not gameOver(scoreA, scoreB): if serving == "A": if random() < probA: scoreA += 1 else: scoreB += 1 else: if random() < probB: scoreB += 1 else: scoreA += 1 return scoreA, scoreBdef printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("队A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("队B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))def main(): printIntro() probA, probB, n = getInputs() winsA, winsB = simNGames(n, probA, probB) printSummary(winsA, winsB)main()
运行结果如下
总结
以上所述是小编给大家介绍的使用Python进行体育竞技分析(预测球队成绩),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号