2020美赛建模D题(mysql+SQLAlchemy+pytorch)数据处理方面总结

mysql数据库,sqlalchemyORM

用的pyplot作图
pytorch建模

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Float, Integer, String,or_
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
import matplotlib.pyplot as plt
import random
class dataAnalyzer(object):
    Base = declarative_base()
    class Matches(Base):
        __tablename__ = 'matches'  # 表名
        MatchID = Column(String, primary_key=True)
        OpponentID = Column(String(20))
        Outcome = Column(String(10))
        OwnScore = Column(Integer)
        OpponentScore = Column(Integer)
        Side = Column(String(10))
        CoachID = Column(String(10))
    class Fullevents(Base):
        __tablename__ = 'fullevents'  # 表名
        MatchID = Column(String)
        TeamID = Column(String)
        OriginPlayerID = Column(String)
        DestinationPlayerID = Column(String)
        MatchPeriod = Column(String)
        EventTime = Column(Float,primary_key=True)
        EventType = Column(String)
        EventSubType = Column(String)
        EventOrigin_x = Column(Float)
        EventOrigin_y = Column(Float)
        EventDestination_x = Column(Float)
        EventDestination_y = Column(Float)
    class Passingevents(Base):
        __tablename__ = 'passingevents'  # 表名
        MatchID = Column(String)
        TeamID = Column(String)
        OriginPlayerID = Column(String)
        DestinationPlayerID = Column(String)
        MatchPeriod = Column(String)
        EventTime = Column(Float,primary_key=True)
        EventSubType = Column(String)
        EventOrigin_x = Column(Float)
        EventOrigin_y = Column(Float)
        EventDestination_x = Column(Float)
        EventDestination_y = Column(Float)
    def __init__(self):
        engine = create_engine("mysql+mysqlconnector://root:000422@localhost:3306/passdata", max_overflow=5)
        self.session = Session(engine)
        self.Base.metadata.create_all(engine)

    def get_TheAllPassMap(self,passtype="Simple pass",Period="1H",match_id=1):
        rows = self.session.query(self.Passingevents).filter(self.Passingevents.TeamID=="Huskies",
                                                             self.Passingevents.MatchID==match_id,
                                                             self.Passingevents.EventSubType==passtype,
                                                             self.Passingevents.MatchPeriod==Period).all()
        pos_x = []
        pos_y = []
        str = 'The picture of the {passtype} of Huskies in the {Period} of the match {match_id}'.format(passtype=passtype,Period=Period,match_id=match_id)
        plt.title(str)
        plt.xlabel('row')
        plt.ylabel('column')
        for row in rows:
            pos_x.append(row.EventOrigin_x)
            pos_x.append(row.EventDestination_x)
            pos_y.append(row.EventOrigin_y)
            pos_y.append(row.EventDestination_y)
            plt.plot(pos_x, pos_y, 'r--')
            pos_x.clear()
            pos_y.clear()
        plt.legend()
        plt.show()

    def get_homewin_rate(self):
        homewinnum = self.session.query(self.Matches).filter(self.Matches.Outcome=="win",self.Matches.Side=="home").count()
        print(homewinnum/19)

    def get_awaywin_rate(self):
        awaywinnum = self.session.query(self.Matches).filter(self.Matches.Side=="away",self.Matches.Outcome=="win").count()
        print(awaywinnum/19)

    def get_attackpointMap(self,Player_type="F"):
        rows = self.session.query(self.Fullevents).filter(self.Fullevents.TeamID == "Huskies",
                                                          or_(self.Fullevents.EventType=="Free Kick",
                                                              self.Fullevents.EventType=="Shot")).all()
        freepos_x = []
        freepos_y = []

        shot_x = []
        shot_y = []

        shot_x_F = []
        shot_y_F = []
        for row in rows:
            if(row.EventType=="Free Kick"):
                freepos_x.append(row.EventOrigin_x)
                freepos_y.append(row.EventOrigin_y)
            else:
                if(row.OriginPlayerID[-2]=="F"):
                    shot_x_F.append(row.EventOrigin_x)
                    shot_y_F.append(row.EventOrigin_y)
                else:
                    shot_x.append(row.EventOrigin_x)
                    shot_y.append(row.EventOrigin_y)

        # 自由球颜色
        # 射门颜色 红
        # 按角色位置区分,f红色,g绿色

        str = 'The picture of the shot position of Huskies '
        plt.title(str)
        plt.xlabel('x')
        plt.ylabel('y')
        plt.scatter(shot_x,shot_y, c="red", label="shot", alpha=0.6, edgecolors='white')
        plt.scatter(freepos_x,freepos_y, c="blue", label="free kick", alpha=0.6, edgecolors='white')
        plt.scatter(shot_x_F, shot_y_F, c="green", label="Forward player", alpha=0.6, edgecolors='white')
        plt.legend()
        plt.grid(True)
        plt.show()

    def get_pass_F(self,passtype="Simple pass",Period="1H",match_id=1):
        rows = self.session.query(self.Passingevents).filter(self.Passingevents.TeamID == "Huskies",
                                                             self.Passingevents.MatchID == match_id,
                                                             self.Passingevents.EventSubType == passtype,
                                                             self.Passingevents.MatchPeriod == Period).all()
        pos_x = []
        pos_y = []
        pos_F_x =[]
        pos_F_y =[]
        str = 'The picture of the fowardplayer when {passtype}ing in the {Period} of the match {match_id}'.format(passtype=passtype,
                                                                                                        Period=Period,
                                                                                                        match_id=match_id)
        plt.title(str)
        plt.xlabel('x')
        plt.ylabel('y')
        for row in rows:
            pos_x.append(row.EventOrigin_x)
            pos_x.append(row.EventDestination_x)
            pos_y.append(row.EventOrigin_y)
            pos_y.append(row.EventDestination_y)
            if(row.DestinationPlayerID[-2]=="F"):
                pos_F_x.append(row.EventDestination_x)
                pos_F_y.append(row.EventDestination_y)
            plt.plot(pos_x, pos_y, 'r--')
            pos_x.clear()
            pos_y.clear()
        plt.scatter(pos_F_x,pos_F_y,c="black", label="shot", alpha=1.0, edgecolors='white')
        plt.legend()
        plt.show()

    def get_shotAnalyze(self):
        rows = self.session.query(self.Fullevents).filter(self.Fullevents.TeamID == "Huskies",
                                                          self.Fullevents.EventType == "Shot").all()
        shot_x = []
        shot_y = []

        shot_x_A = []
        shot_y_A = []
        for row in rows:
            if (random.uniform(0,10)<=3):
                shot_x_A.append(row.EventOrigin_x)
                shot_y_A.append(row.EventOrigin_y)
            else:
                shot_x.append(row.EventOrigin_x)
                shot_y.append(row.EventOrigin_y)

        str = 'The picture of the shot analyze position of Huskies '
        plt.title(str)
        plt.xlabel('x')
        plt.ylabel('y')
        plt.scatter(shot_x, shot_y, c="green", label="shot point", alpha=0.6, edgecolors='white')
        plt.scatter(shot_x_A, shot_y_A, c="red", label="Effective attack point", alpha=0.6, edgecolors='white')
        plt.legend()
        plt.grid(True)
        plt.show()


#
d = dataAnalyzer()
# d.get_TheAllPassMap(passtype="High pass",Period="2H")
# d.get_pass_F()
# d.get_homewin_rate()
# d.get_awaywin_rate()
d.get_shotAnalyze()
'''
rows = self.session.query(self.Fullevents.TeamID,self.Fullevents.EventType,self.Fullevents.EventSubType).all()
        flag = False
        for row in rows:
            if(flag):
                print(row.TeamID,row.EventType,row.EventSubType)
                flag = False
            if(row.EventType=="Shot"):
                print(row.TeamID, row.EventType, row.EventSubType)
                flag  = True
'''

在比赛中过程中主要问题集中在:

  1. 数据的存取
  2. 对于数据量的分析,根据模型需要的提取
  3. 制图
  4. 模型拟合

数据存取

因为给的是csv文件,所以先用navicat存进了数据库,以前一直是用的是django里面成型的orm,这次直接处理,选择的是SQLAlchemy,使用的时候手生的不行,还遇到挺多问题

数据库连接:
engine = create_engine("mysql+mysqlconnector://root:XXX@localhost:3306/passdata", max_overflow=5)
        # root mysql用户名
        # XXX密码
        # localhost:3306连接地址
        # passdata库名
对已有的表关系进行映射:
# 反射数据库单表
Admin = Table('admin', metadata, autoload=True, autoload_with=engine)
session = Session(engine)

'''反射数据库所有的表
Base = automap_base()
Base.prepare(engine, reflect=True)
Admin = Base.classes.admin
'''

这种方法,反射单表可以操作,但是之后进行数据处理,filter无法识别表名,下面的方法我尝试无效
2.

Base = declarative_base()
 class Matches(Base):
        __tablename__ = 'matches'  # 表名
        MatchID = Column(String, primary_key=True)
        OpponentID = Column(String(20))
        Outcome = Column(String(10))
        OwnScore = Column(Integer)
        OpponentScore = Column(Integer)
        Side = Column(String(10))
        CoachID = Column(String(10))
Base.metadata.create_all(engine)

对已有的表关系,以Base为基类,根据数据类型进行构造,然后执行create_all即可

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
summary: In this paper, we establish a regression model based on the passing network to evaluate the influence of team structure strategy and opponents’ counter-strategy on the match results. Fortask1,wefirstlistsomeHuskiesmatchstatisticsforthisseasonandanalyzetheteamin brief. Secondly, we construct a passingnetwork based on the number of passes and visualizes the passing network diagram of three games under three different coaches. We use these three diagrams to describe and compare the changes in Huskies’ strategies. After that, we identify network patterns of dyadic and triadic configurations and count 15 kinds of these two configurations in the above three matches, reflecting the structural indicators of the passing network. We also explore time scale and micro scale by giving the change of the team’s centroid over time in the first match and the Huskies’ 4 positions heat map over the season. For task 2, we construct the regression model not only introducing the basic data representing Huskies’ and opponents’ ability, but also extracting six independent variables from the indicators of the passing network into the model. Considering opponents’ counter-strategies, we also introduce the product interaction term between opponents’ data and network structure indicators. Through the training of regression model, we can judge whether the independent variables introduced have influence, what kind of influence and how much influence the independent variables introduced have on the result of the match. For task 3, by bringing in data for training, the model leaves 10 variables including interactionterms. Inordertoverifytheaccuracyofthemodel,weuseLeaveOneOutcrossvalidation, andthepredictedaccuracyoftheraceresultreached71.05%. Then,basedonthetrainedmodel, we point out the effective structural strategies Huskies currently have, such as the strong connection between the core players. Meanwhile, we also give specific advice for Huskies team to improve team success, such as the emphasis on triadic configurations among players. Fortask4,weextendthemodelappliedtohuskiestoallteamworkscenariosandintroduce the IPOI model. The IPOI model conducts multi-level induction of influencing factors and selection of assessment indicators from the four aspects of team input, process, output and reinput, taking into account team construction, operation, management, feedback and other aspects. WeconsiderthattheexistingHuskiemodelisprogresspartofIPOImodel,andweadd the evaluation system of input, output and reinput part, taking the university scientific research team modeling as an example. Insummary,ourmodelispracticalandreliableforhandlingnetwork-basedteamworkproblems in society. Keywords: football strategy, network science, regression analysis, IPOI model.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值