脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 使用Python进行体育竞技分析(预测球队成绩)

使用Python进行体育竞技分析(预测球队成绩)

2021-06-27 00:08小莫1999 Python

这篇文章主要介绍了用Python进行体育竞技分析(预测球队成绩),本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

今天我们用python进行体育竞技分析,预测球队成绩

一. 体育竞技分析的ipo模式 :

输入i(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来表示,

一个能力值为0.8的球员,在他发球时,有80%的可能性赢得1分)

处理p(process):模拟比赛过程

输出o(output):两个球员获胜的概率

该体育竞技程序,我们采用自顶向下的设计方法。

自顶向下的设计是一种解决复杂问题的行之有效的方法。其步骤如下

使用Python进行体育竞技分析(预测球队成绩)

自顶向下设计的基本思想,如下图:

使用Python进行体育竞技分析(预测球队成绩)

二.我们首先采用兵乓球的比赛规则

一局比赛中,先得11分的一方为胜方,如果10平后,则比对方多得两分为胜方

一场比赛中,采用7局四胜的方式

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""
created on wed may 15 12:49:17 2019
@author: moyulin
"""
from random import random
def 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, n
def 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, winsb
def 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 false
def 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, scoreb
def 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进行体育竞技分析(预测球队成绩)

三.运用pyinstaller打包应用程序,使之可运行

win+cmd打开命令行

1.安装pyinstaller库

pip install pyinstaller

安装完成后就可以使用了,下面介绍pyinstaller的部分使用方法

-f, –onefile 打包一个单个文件,如果你的代码都写在一个.py文件的话,可以用这个,如果是多个.py文件就别用

-d, –onedir 打包多个文件,在dist中生成很多依赖文件,适合以框架形式编写工具代码,我个人比较推荐这样,代码易于维护

-k, –tk 在部署时包含 tcl/tk

-a, –ascii 不包含编码.在支持unicode的python版本上默认包含所有的编码.

-d, –debug 产生debug版本的可执行文件

-w,–windowed,–noconsole 使用windows子系统执行.当程序启动的时候不会打开命令行(只对windows有效)

-c,–nowindowed,–console

2.打开命令行使用

输入

pyinstaller -f c:\#py文件地址

图例

 使用Python进行体育竞技分析(预测球队成绩)

最后回到根目录上会看到dist文件夹,里面有个exe文件,直接运行即可,如图

 使用Python进行体育竞技分析(预测球队成绩)

四.模拟体育竞技分析之篮球

假设谁先获得100分谁胜利

代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from random import random
def 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, n
def 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, winsb
def gameover(a,b):
  return a==100 or b==100
def 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, scoreb
def 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进行体育竞技分析(预测球队成绩)

总结

以上所述是小编给大家介绍的使用python进行体育竞技分析(预测球队成绩),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:http://www.cnblogs.com/moyulin/p/Yulinmo_6.html

延伸 · 阅读

精彩推荐