nba球队
一、实验名称:用决策树预测获胜球队 1、实验目的: 用决策树预测获胜球队 2、实验内容: 用决策树预测获胜球队 3、实验环境(设备、软件、实验数据): 设备:笔记本电脑 软件:Python idle 实验数据:leagues_NBA_2013_standings_, leagues_NBA_2014_games_ 3、实验步骤: 1. 加载库,读取数据并进行处理; import pandas as pd import os from _selection import train_test_split from _selection import cross_val_score import numpy as np from collections import defaultdict from import DecisionTreeClassifier from cessing import LabelEncoder from cessing import OneHotEncoder # 添加文件路径,路径最好不要出现中文 dataFolder = "’G:\Python36\leagues_NBA_2014_games_’"; data_filename = (dataFolder, "basketball", "leagues_NBA_2014_games_"); with open(data_filename) as data_filename: #读取数据的同时,将第一二列合并为一列 dataset = _csv(data_filename, parse_dates=[[0,1]] #我们可以通过下面这个方法对数据进行查看 #[:5] #对数据重新命名头部 s = [“Date”, “Visitor Team”, “VisitorPts”, “Home Team”, “HomePts”, “OT?”, “Notes”] #对数据进行排序并重新建立索引。当然也可以不进行排序,这个影响并不大。 #sort()排序函数已经在pandas0.20以后被弃用,一旦使用将报错,只能使用
sort_values()/sort_index() _values(by=["Date"], ascending=True, inplace=True) dataset = _index(drop=True) 2. 创建字典,储存比赛结果,抽取数据特征; #创建(默认)字典,用于存储球队上次比赛的结果。该语句作用为当字典内不存在查找的key时,返回括号内(factory_function)类型的默认值;int默认值0 won_last = defaultdict(bool) #新增一列HomeWin,该列判断对应的数据是否主场胜利。将是否主场胜利作为预测结果 dataset[“HomeWin”] = dataset[“VisitorPts”] < dataset[“HomePts”] y_true = dataset[“HomeWin”].values #创建两个能帮助我们进行预测的特征,分别是这两支队伍上场比赛的胜负情况。赢得上场比赛,大致可以说明该球队水平较高。 dataset["HomeLastWin"] = 0 dataset["VisitorLastWin"] = 0 for index, row in ws(): home_team = row["Home Team"] visitor_team = row["Visitor Team"] row["HomeLastWin"] = won_last[home_team] row["VisitorLastWin"] = won_last[visitor_team] [index] = row #用当前比赛(遍历到的那一行数据所表示的比赛)的结果更新两支球队上场比赛的获胜情况,以便下次再遍历到这两支球队时使用。 won_last[home_team] = row["HomeWin"] won_last[visitor_team] = not row["HomeWin"] 3. 从scikit-learn库中导入DecisionTreeClassifier类,创建决策树; #我们再次设定random_state的值为14 clf = DecisionTreeClassifier(random_state=14) X_previouswins = dataset[["HomeLastWin", "VisitorLastWin"]].values # 交叉检验 #X_train, X_test, y_train, y_test = train_test_split(X_previouswins, y_true, random_state=14) #(X_train,y_train) #将数据导入分类器,并通过交叉检验。 scores = cross_val_score(clf, X_previouswins, y_true, scoring='accuracy',cv=3) print(“初次使用决策树Accuracy: {0:.1f}%".format((scores) * 100)) 输出结果:
4. 导入新的数据集,创建一个新特征HomeTeamRanksHigher,并更新其值; standings_filename = (dataFolder, "basketball","leagues_NBA_2013_standings_"); with open (standings_filename) as standings_filename: standings = _csv(standings_filename,skiprows=[0,]); dataset["HomeTeamRanksHigher"] = 0 for index, row in ws(): home_team = row["Home Team"] visitor_team = row["Visitor Team"] if home_team == "New Orleans Pelicans": home_team = "New Orleans Hornets" elif visitor_team == "New Orleans Pelicans": visitor_team = "New Orleans Hornets" # series搜索,[]里面可以通过条件选择出对应行主场排名 home_rank = standings[standings["Team"] ==home_team]["Rk"].values[0] # 客场排名 visitor_rank = standings[standings["Team"] ==visitor_team]["Rk"].values[0] # 判断一个队伍在主场获胜的排名和客场获胜的排名 row["HomeTeamRanksHigher"] = bool(home_rank > visitor_rank) [index] = row 5. 新增加HomeTeamRanksHigher特征,再次进行交叉检验; X_homehigher = dataset[["HomeLastWin","VisitorLastWin","HomeTeamRanksHigher"]].values # 建立决策树 clf = DecisionTreeClassifier(random_state=14) scores = cross_val_score(clf, X_homehigher, y_true,scoring='accuracy',cv=3) print("增加新特征值Accuracy: {0:.1f}%".format((scores) * 100)) 输出结果: 6. 统计两支球队上场比赛的情况,创建HomeTeamWonLast特征值,并更新其值,再创建决策树并使用cross_val_score函数测试结果; #创建字典,保存上场比赛的获胜队伍,在数据框中建立新特征。 last_match_winner = defaultdict(int) dataset["HomeTeamWonLast"] = 0 for index, row in ws(): home_team = row["Home Team"] visitor_team = row["Visitor Team"]
teams = tuple(sorted([home_team, visitor_team])) row["HomeTeamWonLast"] = 1 if last_match_winner[teams] == row["Home Team"] else 0 [index] = row winner = row["Home Team"] if row["HomeWin"] else row["Visitor Team"] last_match_winner[teams] = winner X_lastwinner = dataset[["HomeLastWin", "VisitorLastWin", "HomeTeamRanksHigher", "HomeTeamWonLast"]].values clf = DecisionTreeClassifier(random_state=14) scores = cross_val_score(clf, X_lastwinner, y_true, scoring='accuracy', cv=3) print("再次增加特征Accuracy: {0:.1f}%".format((scores) * 100)) 输出结果: 7. 引入LabelEncoder转换器,将主场球队名称转化为整型并抽取所有比赛的主客场球队的球队名并将其组合起来,形成一个矩阵,引入OneHotEncoder转换器,通过将整数转换为二进制来消除球队编号之间的相似情况,创建决策树并使用cross_val_score函数测试结果; encoding = LabelEncoder() t=(dataset["Home Team"].values) home_teams = orm(dataset["Home Team"].values) visitor_teams = orm(dataset["Visitor Team"].values) X_teams = ([home_teams, visitor_teams]).T onehot = OneHotEncoder(categories='auto') k=(X_teams) X_teams_expanded = orm(X_teams).todense() # 创建决策树对象 clf = DecisionTreeClassifier(random_state=14) scores = cross_val_score(clf, X_teams_expanded, y_true,scoring='accuracy',cv=3) print("整合新数据Accuracy: {0:.1f}%".format((scores) * 100)) 输出结果: 5、总结及心得体会: 通过这次实验,学习了使用决策树进行分类,还学会了使用不同的决策树分类器,此外,了解到一次预测的好坏不仅依靠好的数据,还要结合实际情况使用不同的分类器,
并不断调整特征值以及算法的参数,才能不断优化预测的结果。
nba球队