python体育竞技分析代码200行_使用Python进行体育竞技分析(预测球队成绩)
使用Python進行體育競技分析(預測球隊成績)
發布時間:2020-09-18 06:38:27
來源:腳本之家
閱讀:69
今天我們用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()
運行結果如下
總結
以上所述是小編給大家介紹的使用Python進行體育競技分析(預測球隊成績),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python体育竞技分析代码200行_使用Python进行体育竞技分析(预测球队成绩)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5 随窗口改变大小_C4D默认界面两个关
- 下一篇: websocket python爬虫_p