用Python進行體育競技分析(預測球隊成績)
今天我們用python進行體育競技分析,預測球隊成績
一.體育競技分析的IPO模式:
輸入I(input):兩個球員的能力值,模拟比賽的次數(其中,運動員的能力值,可以通過發球方赢得本回合的機率來表示,
一個能力值為0.8的球員,在他發球時,有80%的可能性赢得1分)
處理P(process):模拟比賽過程
輸出O(output):兩個球員獲勝的機率
該體育競技程式,我們采用自頂向下的設計方法。
自頂向下的設計是一種解決複雜問題的行之有效的方法。其步驟如下
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiETPwJWZ3ZCMwcTP39zZuBnLuVzRjV3ZU1EMJR1T6VFVNBDMD1EeNpXTxkFVOlXRU5EeVRUT1UERNlHMT5UeBR0T6lFVNZXVE1UNFRUT5hzUOlXQE9keZRVT2NmMiNnSywEd5ITW110MaZHetlVdO1GT0UERNl3YXJGc5kHT20ESjBjUIF2Lc12bj5SYphXa5VWen5WY35iclN3Ztl2Lc9CX6MHc0RHaiojIsJye.png)
自頂向下設計的基本思想,如下圖:
二.我們首先采用兵乓球的比賽規則
一局比賽中,先得11分的一方為勝方,如果10平後,則比對方多得兩分為勝方
一場比賽中,采用7局四勝的方式
代碼如下:
# -*- 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()
運作結果如下:
三.運用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檔案位址
圖例
最後回到根目錄上會看到dist檔案夾,裡面有個exe檔案,直接運作即可,如圖
四.模拟體育競技分析之籃球
假設誰先獲得100分誰勝利
代碼如下
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()
運作結果如下
posted on 2019-05-16 00:04 小莫1999 閱讀(...) 評論(...) 編輯 收藏