<file_sep>import pandas as pd
import numpy as np
import csv
import random 
random.seed(10)
import tqdm

def balanceClass():
	for year in tqdm.tqdm(["2011","2012","2013","2014","2015"]):
		df = pd.read_csv("../Data/players_"+year+".csv")
		df1 = pd.read_csv("../Data/team_"+year+".csv")
		assert len(df.index) == len(df1.index) , "check the Files"
		assert df1['winner'].sum() ==  df["Winner"].sum() , "check the files"
		ones = df1['winner'].sum()
		
		total = len(df1.index)
		delta = ones - (total/2)	
		print("Imbalance before:" + str(delta))
		listRows = []
		for i in range(len(df.index)):
			if int(df.iloc[i,-1:]) == 1:
				listRows.append(i)
		random.shuffle(listRows)
		for i in range(int(delta)):
			#print(i)
			rowNumber = listRows[i]
			#print(rowNumber)
			assert df.loc[rowNumber][-1] == df1.loc[rowNumber][-1] , "check the files"	
			teamData = df1.loc[rowNumber].to_numpy()
			playersData = df.loc[rowNumber].to_numpy()
			whoWonInit = df.loc[rowNumber][-1]
			
				#delta = delta- 1
			dataPlayers = playersData[:460]
			dataPlayersTeam1 = dataPlayers[:230]
			dataPlayersTeam2 = dataPlayers[230:460]
			dataTeams = teamData[:48]
			dataTeamsTeam1 = dataTeams[:24]
			dataTeamsTeam2 = dataTeams[24:48]
			assert len(dataPlayersTeam2) == len(dataPlayersTeam2) , "Error"
			assert len(dataTeamsTeam2) == len(dataTeamsTeam1) , "Error"
			newFirstTeam = dataTeamsTeam2
			lineAfterSecondTeam = np.append(newFirstTeam,dataTeamsTeam1)
			newFirstPlayerTeam = dataPlayersTeam2 
			lineAfterPlayerSecondTeam = np.append(newFirstPlayerTeam,dataPlayersTeam1)
			whoWonInit = not (whoWonInit)
			finalTeamLine = np.append(lineAfterSecondTeam,whoWonInit)
			finalPlayerLine = np.append(lineAfterPlayerSecondTeam,whoWonInit)
			df1.loc[rowNumber] = finalTeamLine
			#print(df1.loc[rowNumber])
			df.loc[rowNumber] = finalPlayerLine
			#print(df1.loc[rowNumber])
		
		#print(df)
		df.to_csv("./Data/playersBal_"+year+".csv",index=False)
		df1.to_csv("./Data/teamBal_"+year+".csv",index=False)
		#print(df['Winner'])
		print("Imbalance Now : " + str(int(df["Winner"].sum()-len(df.index)/2)))
		#print(len(df.index))


balanceClass()


<file_sep>import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
import tqdm
from pathlib import Path
data_folder = Path("../Data1/")
#dataDict = dict()
#dataPDict = dict()

def rowchef():
  datastr = ["SetsPlayed","AttackKills","AttackErrors","AttackTotalAttempts","AttackPCT","Assists","AssistErrors",
                "AssistTotalAttempts","AssistPCT","ServeAces","ServeErrors","ServeTotalAttempts","ServePCT",
                "BlockingDig","BallHandlingErrors","BlockSolos","BlockAssists","BlockErrors","ReceptO","ReceptErrors","ReceptPCT",
                "Block_Efficiency_Player","Ace_Strength"
                
                ]
  finalDatastr = []
  for i in range (20):
    for str1 in datastr:
      finalDatastr.append(str1)
  finalDatastr.append("Winner")
  #print(finalDatastr.shape)
  return finalDatastr

def tablechef(soupObj):
  dataTable = []
  for element in soupObj: 
      sub_data = [] 
      for sub_element in element: 
          try: 
              string = sub_element.text
              string = string[:len(string)-1]
              sub_data.append(string) 
          except: 
              continue
      dataTable.append(sub_data) 
  return pd.DataFrame(data = dataTable)

def weightschef(shape,decay=0.9):
  wt = []
  wt = np.array(wt,dtype=float)
  for i in range(shape):
    wt = wt*(decay)
    wt = np.append(wt,1)
  return wt

import csv
import numpy as np

def dataChef(dataDict,dataPDict,file1name="../Data/links_2011.csv",file2name="../Data/innovators_2011.csv",file3name="../Data/swarriors_2011.csv",decay=0.9):

  dflinks = pd.read_csv(file1name)
 
  
  file1 =  open(file2name, 'w', newline='');
  file2 = open(file3name,'w',newline='');
  writer = csv.writer(file1)
  writerP = csv.writer(file2)

  writer.writerow(["SetsPlayed","AttackKills","AttackErrors","AttackTotalAttempts","AttackPCT","Assists","AssistErrors",
                "AssistTotalAttempts","AssistPCT","ServeAces","ServeErrors","ServeTotalAttempts","ServePCT",
                "BlockingDig","BallHandlingErrors","BlockSolos","BlockAssists","BlockErrors","ReceptO","ReceptErrors","ReceptPCT",
                "TotalTeamBlocks", "Set_win_ratio","winrate","SetsPlayed","AttackKills","AttackErrors","AttackTotalAttempts","AttackPCT","Assists","AssistErrors",
                "AssistTotalAttempts","AssistPCT","ServeAces","ServeErrors","ServeTotalAttempts","ServePCT",
                "BlockingDig","BallHandlingErrors","BlockSolos","BlockAssists","BlockErrors","ReceptO","ReceptErrors","ReceptPCT",
                "TotalTeamBlocks", "Set_win_ratio", "winrate",
                  "winner"])

  writerP.writerow(rowchef())

  #len(dflinks.index)
  for i in tqdm.tqdm(range(len(dflinks.index))):
    try:
        res = requests.get(dflinks.loc[i][0])
        soup = BeautifulSoup(res.content,'html.parser')
        
        if "2012" not in file1name:
          table = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[5].find_all('tr')
          settable = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[7].find_all('tr')
          table1 = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[8].find_all('tr')
        else :
          table = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[6].find_all('tr')
          settable = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[8].find_all('tr')
          table1 = (soup.find_all("table", border=0 ,cellspacing=0 ,cellpadding= 2))[9].find_all('tr')

        
        df=tablechef(table)
        #print(list(df.columns))
        if len(df.columns)>23:
          #print(len(df.columns))
          df.drop(df.columns[len(df.columns)-1], axis=1, inplace=True)
        dfset = tablechef(settable)
        team1 = str(dfset.loc[1][0]).strip()
        team2 = str(dfset.loc[3][0]).strip()
        df1 = tablechef(table1)
        if len(df1.columns)>23:
          df1.drop(df1.columns[len(df1.columns)-1], axis=1, inplace=True)

        team1_score = (str(dfset.loc[1][1]).split('('))[1][0]
        team1_score = int(team1_score)
       
        
        team2_score = (str(dfset.loc[3][1]).split('('))[1][0]
        team2_score = int(team2_score)

        team1Array = []
        team2Array = []

        if i>0:

            if (team1.strip() in dataDict.keys()):
                team1Array = np.average(dataDict[team1],0,weightschef(len(dataDict[team1]),decay))
            else:
                team1Array = np.average(dataDict["goldStandard"],0,weightschef(len(dataDict["goldStandard"]),decay))

            if (team2.strip() in dataDict.keys()) :
                team2Array = np.average(dataDict[team2],0,weightschef(len(dataDict[team2]),decay))
            else:
                team2Array = np.average(dataDict["goldStandard"],0,weightschef(len(dataDict["goldStandard"]),decay))

            dataArray = np.append(team1Array,team2Array)
            dataArraywithPrediction = np.append(dataArray,team2_score > team1_score)
                                                    
            # print(dataArraywithPrediction)
            writer.writerow(dataArraywithPrediction)
            playerWriter = []
            team1PlayerCount = 0
            team2PlayerCount = 0

            for tuples in dataPDict.keys():
              if tuples[0]==team1:
                playerWriter.append(np.average(dataPDict[tuples],0,weightschef(len(dataPDict[tuples]),decay)))
                team1PlayerCount += 1
              if team1PlayerCount==10:
                break

            while team1PlayerCount<10:
              playerWriter.append(np.average(dataPDict[("goldStandard","X","X")],0,weightschef(len(dataPDict[("goldStandard","X","X")]),decay)))
              team1PlayerCount+=1

            for tuples in dataPDict.keys():  
              if tuples[0]==team2:
                playerWriter.append(np.average(dataPDict[tuples],0,weightschef(len(dataPDict[tuples]),decay)))
                team2PlayerCount += 1
              if team2PlayerCount==10:
                break

            while team2PlayerCount<10:
              playerWriter.append(np.average(dataPDict[("goldStandard","X","X")],0,weightschef(len(dataPDict[("goldStandard","X","X")]),decay)))
              team2PlayerCount+=1

            
            #print(len(playerWriter))
            playerWriterFlat = np.array(playerWriter,dtype=float).flatten()
            playerWriterFlatWithWinner = np.append(playerWriterFlat,dataArraywithPrediction[-1])
            writerP.writerow(playerWriterFlatWithWinner)
                

        
        set_win_ratio1 = str(team1_score/(team1_score + team2_score))
        set_win_ratio2 = str(team2_score/(team1_score + team2_score))

        line = (df.loc[df[1].str.contains('Total',na=False)]).to_numpy()
        lineClean = np.delete(line,0)
        lineClean1 = np.delete(lineClean,0)
        lineClean2 = np.append(lineClean1,(str(df.iloc[-1][1]).split())[3])
        #print(lineClean2)
        lineClean3 = np.append(lineClean2 , set_win_ratio1)
        #(lineClean3)
        line1 = (df1.loc[df1[1].str.contains('Total',na=False)]).to_numpy()     
        line1Clean = np.delete(line1,0)
        line1Clean1 = np.delete(line1Clean,0)
        #---------CHANGES
        line1Clean2 = np.append(line1Clean1,(str(df1.iloc[-1][1]).split())[3])
        #---------CHANGES
        line1Clean3 = np.append(line1Clean2 , set_win_ratio2)
        line2 = np.append(lineClean3,line1Clean3)
        #print(line2)
        lineAfterTeam2 = np.insert(line2,0,team2);
        lineAfterTeam1 = np.insert(lineAfterTeam2,0,team1);

        newLine1 = np.append(lineAfterTeam1 ,(str(dfset.loc[1][1]).split('('))[1][0])
        newLine2  = np.append(newLine1,(str(dfset.loc[3][1]).split('('))[1][0])
        winnerLine = np.append(newLine2 ,int(int(newLine2[len(newLine2)-1]) > 
                                                    int(newLine2[len(newLine2)-2]))) #team2 wins implies 1    
        #---------CHANGES
        data1 = np.append(winnerLine[2:25],int(winnerLine[-1]==0))
        data1 = data1.astype(float)
        data2 = np.append(winnerLine[25:48],int(winnerLine[-1]==1))
        data2 = data2.astype(float)
        dataDict.setdefault(team1.strip(), []).append(data1)
        dataDict.setdefault(team2.strip(), []).append(data2)
        dataDict.setdefault("goldStandard",[]).append(data1)
        dataDict.setdefault("goldStandard",[]).append(data2)
        for k in range(len(df.index)-3):
          if k<2:
            continue
          player = (df.loc[k]).to_numpy()
          playerStats = player[2:].astype(float)
          if (playerStats[13]+playerStats[15]+playerStats[16]+playerStats[14]+playerStats[17])==0:
              Block_Efficiency=0
          else:
              Block_Efficiency = (playerStats[13]+playerStats[15]+playerStats[16]-playerStats[14]-playerStats[17])/(playerStats[13]+playerStats[15]+playerStats[16]+playerStats[14]+playerStats[17])
          if playerStats[11]==0:
              ace_strength =0
          else:
              ace_strength = playerStats[9]/playerStats[11]
          playerStatsAfterBlockEngineering = np.append(playerStats,Block_Efficiency)
          playerStatsAfterAceEngineering = np.append(playerStatsAfterBlockEngineering,ace_strength)
          dataPDict.setdefault((team1.strip(),player[0].strip(),player[1].strip()),[]).append(playerStatsAfterAceEngineering)
          dataPDict.setdefault(("goldStandard","X","X"),[]).append(playerStatsAfterAceEngineering)

        for k in range(len(df1.index)-3):
          if k<2:
            continue
          player = (df1.loc[k]).to_numpy()
          playerStats = player[2:].astype(float)
          if (playerStats[13]+playerStats[15]+playerStats[16]+playerStats[14]+playerStats[17])==0:
              Block_Efficiency=0
          else:
              Block_Efficiency = (playerStats[13]+playerStats[15]+playerStats[16]-playerStats[14]-playerStats[17])/(playerStats[13]+playerStats[15]+playerStats[16]+playerStats[14]+playerStats[17])
          # --- Ace Strength
          if playerStats[11]==0:
              ace_strength =0
          else:
              ace_strength = playerStats[9]/playerStats[11]
          #----- MADE CHANGES
          playerStatsAfterBlockEngineering = np.append(playerStats,Block_Efficiency)
          playerStatsAfterAceEngineering = np.append(playerStatsAfterBlockEngineering,ace_strength)
          dataPDict.setdefault((team2.strip(),player[0].strip(),player[1].strip()),[]).append(playerStatsAfterAceEngineering)
          dataPDict.setdefault(("goldStandard","X","X"),[]).append(playerStatsAfterAceEngineering)

    except:
        #print(str(i) + " has an ERROR")
        pass #LINK has errors
    
      
      
  file1.close()

def scrapeData(decay=0.9):
  for i in ["2011","2012","2013","2014","2015"]:
    print(i+" will be scraped now.")
    dataDict = dict()
    dataPDict = dict()
    dataChef(dataDict,dataPDict,"../Data/links_"+i+".csv","../Data/team_"+i+".csv","../Data/players_"+i+".csv",decay)


if __name__ == "__main__":
  scrapeData()

<file_sep>import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
from sklearn.ensemble import RandomForestClassifier

d_2011 = pd.read_csv('../Data/Players_final_2011.csv')
d_2012 = pd.read_csv('../Data/Players_final_2012.csv')
d_2013 = pd.read_csv('../Data/Players_final_2013.csv')
d_2014 = pd.read_csv('../Data/Players_final_2014.csv')
d_2015 = pd.read_csv('../Data/Players_final_2015.csv')

tunedC = 1

def tuning():
    list_years = [d_2011,d_2012,d_2013,d_2014,d_2015]

    print("Logistic Regression Tuning for player Data: ")
    print("Time Series Split  :")


    C = np.logspace(-100, 10, 200)
    maxroc = 0
    minvar = 100000
    tunedC = 1
    for c in C:
        total =[]
        for i in range(4):
            frames=[]
            for j in range(i+1):
                frames.append(list_years[j])
                train = pd.concat(frames)
            test = list_years[i+1]
            X_train = train.drop(['Winner'],axis=1)
            scaler = StandardScaler()
            scaler.fit(X_train)
            X_train = scaler.transform(X_train)
            y_train = train['Winner']
            X_test = test.drop(['Winner'],axis=1)
            X_test = scaler.transform(X_test)
            y_test = test['Winner'] 
            LR = LogisticRegression(C=c,random_state=6,solver='liblinear',max_iter=100000).fit(X_train, y_train)
            y_preds = LR.predict(X_test)
            confusion_matrix(y_test,y_preds)
            roc = roc_auc_score(y_test,y_preds)
            total.append(roc)
          
        if(np.var(total)<minvar and np.mean(total)>0.5):
            minvar = np.var(total)
            print(minvar)
            tunedC = c
        # if(np.mean(total))>maxStat:
        #     maxStat = np.mean(total)
        #     print(maxStat)
        #     tunedC = c
    print("FINAL Results :")   
    print("C : " + str(tunedC))
    #print("RUCAUC : "+ str(maxStat))


def main():
    

    print("{} shape: {}".format(2011,d_2011.shape))
    print("{} shape: {}".format(2012,d_2012.shape))
    print("{} shape: {}".format(2013,d_2013.shape))
    print("{} shape: {}".format(2014,d_2014.shape))
    print("{} shape: {}".format(2014,d_2015.shape))
    tuning()

    list_years = [d_2011,d_2012,d_2013,d_2014,d_2015]

    print("Logistic Regression: ")

    total =0
    for i in range(4):
        frames=[]
        for j in range(i+1):
            frames.append(list_years[j])
        train = pd.concat(frames)
        test = list_years[i+1]
        X_train = train.drop(['Winner'],axis=1)
        scaler = StandardScaler()
        scaler.fit(X_train)
        X_train = scaler.transform(X_train)
        y_train = train['Winner']
        X_test = test.drop(['Winner'],axis=1)
        X_test = scaler.transform(X_test)
        y_test = test['Winner']
        LR = LogisticRegression(C=tunedC,random_state=6,max_iter=100000,solver='liblinear').fit(X_train, y_train)
        y_preds = LR.predict(X_test)
        confusion_matrix(y_test,y_preds)
        roc = roc_auc_score(y_test,y_preds)
        total += roc
        print("{} ROC is {}".format(2012+i,roc))
    
    print(total/4)


main()<file_sep>
There is a directory "Data" which holds all the necessary data files generated during scraping, feature enggineering and everything.

This directory has the data scraped when WE scraped from the website.

"Data1" will contain the files that YOU generate while running the code.


We have coded the .py files in such a way that they consume the data from "Data" but write files in directory "Data1".

This is done to maintain uniformity in results as NCAA links time out unpredictably and the data is minutely dynamic in Nature.
For eg., our current dataset has data worth 299 matches. But it is possible, that due to some server issues/timeout errors, while you are executing the scrape
code, only 297 matches get scraped. Hence, this was necessary. This discrepancy is very small but we chose to consume data from the original directory - "Data"
so that you are able to reproduce the same results. We ensure you that same data would be generated via the scrape code, given the website acts the same.
If you want to check results for the newly generated data, please change the "Data" to "Data1" in the .py files. The files have a var named "data_folder", so if you change it, it will directly take the files from "Data1".


\src - contains all the .py files
\Data - contains original data files
\Data1 - files would get created here if you run the .py files

Run getData.py which is a driver for all the scraper functions.
First we scrape the links using scrape_links_from_webpage which generates csv files having all the matchwise hyper-links.
Then using these links we scrape the match data from NCAA using scrape_table_from_links.py
After that the data is balanced using balanceClass.py [Please find the explanation in the report]
Finally, players_final_dataset_generation.py generates the 3 player representatives data for the teams.

phase1_teamdata_allmodels.py
to run: python phase1_teamdata_allmodels.py
This code runs the 3 models about which we have described about in phase1 of the report and outputs their metric. The data for (2011-2015) has been taken from the Data folder for training.

metrics.py has the code to generate the necessary metrics for each test set.

plot_pruning_graph.py generates the ccp_alpha vs Impurities graph

neuralNetworksplayers, dtplayers and logisticRegplayers.py files have the model training code for the player-wise data.

<file_sep>import requests
from bs4 import BeautifulSoup
import re
import csv
import tqdm 
from pathlib import Path
data_folder = Path("../Data/")
def getData(yearNumber):
    URL = f"http://fs.ncaa.org/Docs/stats/w_volleyball_champs_records/{str(2010 + yearNumber)}/d1/html/confstat.htm"
    page = requests.get(URL)
    soup = BeautifulSoup(page.content, 'html.parser')

    breaker = [ "ucla-ill.htm", "tex-ore.htm",  "wiscpnst.htm", "finals3.htm", "final15.htm"]
    printing_on = False
    year = 2010 + yearNumber
    file_to_write = data_folder/f"links_{str(year)}.csv"
    with open(file_to_write, "w", newline = '') as f:
        csvwriter = csv.writer(f)
        csvwriter.writerow(["URL's"])
        for a in soup.find_all('a', href=True):
            if(a['href'] == breaker[yearNumber - 1]):
                printing_on = True
            if(printing_on):
                #print(a['href'])
                url = f"http://fs.ncaa.org/Docs/stats/w_volleyball_champs_records/{year}/d1/html/"
                k = a['href']
                b = [url + (str(k))]
                csvwriter.writerow(b)
            else:
                pass

if __name__ == "__main__":
    for k in tqdm.tqdm(range(5)):
        getData(k+1)<file_sep>from sklearn.tree import DecisionTreeClassifier
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def plot_pruning_graph(X_train, y_train, clf):
    # clf is the model you are using
    # Would be declared in the main function like :
    # clf = DecisionTreeClassifier(random_state=0, criterion="entropy", ccp_alpha=0.01,
    #                              class_weight='balanced',
    #                              splitter = "random"
    path = clf.cost_complexity_pruning_path(X_train, y_train)
    ccp_alphas, impurities = path.ccp_alphas, path.impurities
    fig, ax = plt.subplots()
    ax.plot(ccp_alphas[:-1], impurities[:-1], marker='o', drawstyle="steps-post", color = 'black', ls = '--')
    ax.set_xlabel("effective alpha")
    ax.set_ylabel("total impurity of leaves")
    ax.set_title("Total Impurity vs effective alpha for training set")
    #Gets stored
    fig.savefig("ImpurityVSalpha.png")<file_sep>import scrape_links_from_webpage as linkscrape
import scrape_table_from_links as tablescrape
import tqdm
import argparse
from balanceClass import balanceClass
import players_final_dataset_generation as makePlayers

def main(decay=0.9):
  print("Data Scraping begins")
  print("Webscarper is scraping all the Match Links from 2011-2015")
  for k in tqdm.tqdm(range(5)):
    linkscrape.getData(k+1)
  print("All links scarped and dumped into links_$year.csv files . ")
  tablescrape.scrapeData(decay)
  print("All tables scraped and processed to trainable format.")
  print("Lets balance the Data now")
  balanceClass()
  print("Balancing algorithm finished execution")
  makePlayers()

if __name__ == "__main__":
  parser = argparse.ArgumentParser(description=' Add the decay')
  parser.add_argument('--decay', type=float, default = 0.9,
                    help='an integer for the decay constant')
  args = parser.parse_args()
  decay = args.decay  

  main(decay)
  <file_sep>import tensorflow 
import keras.layers.advanced_activations
from numpy.random import seed
seed(1)
from sklearn.preprocessing import StandardScaler
tensorflow.random.set_seed(2)
from sklearn.metrics import confusion_matrix
import pandas as pd
from tensorflow.keras import regularizers
from keras.layers.core import Dropout
from tensorflow.keras import layers
from tensorflow.keras import initializers
from sklearn.metrics import roc_auc_score
from numpy.random import seed
seed(1)
d_2011 = pd.read_csv('../Data/Players_final_2011.csv')
d_2012 = pd.read_csv('../Data/Players_final_2012.csv')
d_2013 = pd.read_csv('../Data/Players_final_2013.csv')
d_2014 = pd.read_csv('../Data/Players_final_2014.csv')
d_2015 = pd.read_csv('../Data/Players_final_2015.csv')

print("{} shape: {}".format(2011,d_2011.shape))
print("{} shape: {}".format(2012,d_2012.shape))
print("{} shape: {}".format(2013,d_2013.shape))
print("{} shape: {}".format(2014,d_2014.shape))
print("{} shape: {}".format(2014,d_2015.shape))
def main():
  model_nn = keras.Sequential([
      keras.layers.Dense(units=20,activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.0001,l2=0.0001)),
      Dropout(0.25),
      keras.layers.Dense(units=25,activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.0001,l2=0.0001)),
      Dropout(0.25),
      keras.layers.Dense(units=30,activation='relu',kernel_regularizer = regularizers.l1_l2(l1=0.0001,l2=0.0001)),
      Dropout(0.25),
      keras.layers.Dense(units=1,activation='sigmoid')
  ])

  list_years = [d_2011,d_2012,d_2013,d_2014,d_2015]
  roc_l = []
  print("ANN Neural Network:")
  tunedParameters = {}
  # parameters={'batch_size':[4,8],
  #            'epochs':[50,100],
  #            'optimizer':['adam']}


  # maxScore = 0
  # for batchSize in parameters['batch_size']:
  #   for epoch in parameters['epochs']:
  #     for optim in parameters['optimizer']:
  #       keras.backend.clear_session()
  #       model_nn.compile(loss='binary_crossentropy', optimizer=optim, metrics=['accuracy'])
  #       total =0
  #       for i in range(4):
  #           frames=[]
  #           for j in range(i+1):
  #               frames.append(list_years[j])
  #           train = pd.concat(frames)
  #           test = list_years[i+1]
  #           X_train = train.drop(['Winner'],axis=1)
  #           scaler = StandardScaler()
  #           scaler.fit(X_train)
  #           X_train = scaler.transform(X_train)
  #           y_train = train['Winner']
  #           X_test = list_years[i+1].drop(['Winner'], axis = 1)
  #           X_test = scaler.transform(X_test)
  #           y_test = list_years[i+1]['Winner']
            
  #           history = model_nn.fit(X_train,y_train,epochs=epoch,batch_size= batchSize,shuffle = False)
  #           y_preds = model_nn.predict(X_test)
  #           for i in range(len(y_preds)):
  #               if y_preds[i]>=0.5:
  #                   y_preds[i]=1
  #               else:
  #                   y_preds[i]=0
  #           confusion_matrix(y_test,y_preds)
  #           roc = roc_auc_score(y_test,y_preds)
  #           roc_l.append(roc)
  #           total += roc

  #       # for i in range(4):
  #       #   print("{} ROC is {}".format(2012+i,roc_l[i]))
  #       print("Average: {}".format(total/4))
  #       if total/4 > maxScore:
  #         maxScore = total/4
  #         print(maxScore)
  #         tunedParameters['batch_size'] = batchSize
  #         tunedParameters['epochs'] = epoch
  #         tunedParameters['optimizer'] = optim

  tunedParameters['batch_size'] = 8
  tunedParameters['epochs'] = 50
  tunedParameters['optimizer'] = 'adam'




  list_years = [d_2011,d_2012,d_2013,d_2014,d_2015]
  roc_l = []
  cf_l = []
  print("ANN Neural Network:")

  keras.backend.clear_session()
  model_nn.compile(loss='binary_crossentropy', optimizer=tunedParameters['optimizer'], metrics=['accuracy'])


  total =0
  for i in range(4):
      frames=[]
      for j in range(i+1):
          frames.append(list_years[j])
      train = pd.concat(frames)
      test = list_years[i+1]
      X_train = train.drop(['Winner'],axis=1)
      scaler = StandardScaler()
      scaler.fit(X_train)
      X_train = scaler.transform(X_train)
      y_train = train['Winner']
      X_test = list_years[i+1].drop(['Winner'], axis = 1)
      X_test = scaler.transform(X_test)
      y_test = list_years[i+1]['Winner']
      
      history = model_nn.fit(X_train,y_train,epochs=tunedParameters['epochs'],batch_size = tunedParameters['batch_size'] ,shuffle=False)
      y_preds = model_nn.predict(X_test)
      #y_preds = model_nn.predict(X_train)
      for i in range(len(y_preds)):
          if y_preds[i]>=0.5:
              y_preds[i]=1
          else:
              y_preds[i]=0
      cf_l.append(confusion_matrix(y_test,y_preds))
      roc = roc_auc_score(y_test,y_preds)
      roc_l.append(roc)
      total += roc

  for i in range(4):
    print("{} ROC is {}".format(2012+i,roc_l[i]))
    print(cf_l[i])
  print("Average: {}".format(total/4))

if __name__ == "__main__":
  main()<file_sep>import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import confusion_matrix
from sklearn.metrics import roc_auc_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import f1_score
from sklearn.metrics import roc_curve
from matplotlib import pyplot
import seaborn as sn
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import plot_confusion_matrix

%matplotlib inline

def metrics(y_true,y_pred,y_probs):
    roc = roc_auc_score(y_true,y_probs)
    print("The ROC AUC Score is: {}".format(roc))
    cf = confusion_matrix(y_true,y_preds)
    print("The Confusion Matrix is:")
    print(cf)
    f1score = f1_score(y_true, y_preds)
    print("The F1 score is: {}".format(f1score))
    fpr,tpr,_ = roc_curve(y_true,y_preds)
    print("ROC_AUC curve")
    pyplot.plot(fpr, tpr, marker='.', label='ROC_AUC')


def metrics_combined(comb_y_true,comb_y_pred,comb_y_probs):
  roc =0
  f1score =0
  roc_l=[]
  f1_score_l=[]
  cf_total=0
  for i in range(4):
    roc += roc_auc_score(comb_y_true[i],comb_y_probs[i])
    roc_l.append(roc_auc_score(comb_y_true[i],comb_y_probs[i]))
    f1score += f1_score(comb_y_true[i],comb_y_pred[i])
    f1_score_l.append(f1_score(comb_y_true[i],comb_y_pred[i]))
    cf = confusion_matrix(comb_y_true[i],comb_y_pred[i])
    cf_total += cf
  
  print("Mean ROC score:{}".format(roc/4))
  print("Mean F1 score{}".format(f1score/4))
  print("Confusion Matrix for all:")
  print(cf_total)

  
  x = list(range(1,5))  
  plt.plot(x,roc_l,label='ROC')
  plt.plot(x,f1_score_l,label='F1_score')
  plt.ylabel('Split Vs ROC')
  plt.show()