1. 程式人生 > >用Python進行體育競技分析(預測球隊成績)

用Python進行體育競技分析(預測球隊成績)

今天我們用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 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()

執行結果如下

 

&n