# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# # !pip install fake-useragent
# # !pip install selenium

# if you want to use webdriver on google colab, please install the following package

# # !apt-get update # to update ubuntu to correctly run apt install
# # !apt install chromium-chromedriver
# # !cp /usr/lib/chromium-browser/chromedriver /usr/bin
import fake_useragent
from selenium import webdriver

print( fake_useragent.VERSION )
# -

# ![image.png](attachment:image.png)

# Use Selenium to locate the drop-down menu that appears when the mouse is hovered And write the xpath conditional expression in separate lines：
# ![image.png](attachment:image.png)

# # Collecting the data

# +
ua = fake_useragent.UserAgent() # .random

ua_list = [ua.ie, ua['Internet Explorer'], ua.msie,
           ua.chrome, ua.google, ua['google chrome'],
           ua.firefox, ua.ff,
           ua.safari, 
           ua.opera
          ]

import random
current_ua = random.choice(ua_list)
header = {'UserAgent': current_ua,
          'Connection': 'close'
         }
options = webdriver.ChromeOptions()
options.add_argument( "'" + "user-agent=" + header['UserAgent'] + "'" )
options.add_argument('--disable-gpu') # google document mentioned this attribute can avoid some bugs
# the purpose of the argument --disable-gpu was to enable google-chrome-headless on windows platform. 
# It was needed as SwiftShader fails an assert on Windows in headless mode ### earlier.###
# it doesn't run the script without opening the browser,but this bug ### was fixed.###
# options.add_argument('--headless') is all you need.
 
# The browser does not provide a visualization page. 
# If the system does not support visualization under linux, it will fail to start if you do not add this one
# options.add_argument('--headless')
 
# Solve the prompt that chrome is being controlled by the automatic test software
options.add_experimental_option('excludeSwitches',['enable-automation'])

options.add_experimental_option('useAutomationExtension', False)


# set the browser as developer model, prevent the website identifying that you are using Selenium
driver = webdriver.Chrome( executable_path = 'C:/chromedriver/chromedriver', # last chromedriver is chromedriver.exe
                            options = options
                          )
# #run command("window.navigator.webdriver")in the Console of the inspection
# #result: undefine  # means: regular browser

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    delete navigator.__proto__.webdriver;
                                    """
                        }
                       )

 
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    Object.defineProperty( navigator, 
                                                           'webdriver', 
                                                           {
                                                             get: () => undefined
                                                           }
                                                )
                                    """
                        }
                       )
                       
                       
# browser.set_window_size(1920, 1080)

root = 'https://www.basketball-reference.com/leagues/'
page_link = 'NBA_2016_games.html#schedule'

url = root + page_link

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
import io 
import pandas as pd

wait = WebDriverWait(driver, 2)

time_0 = time.time()
driver.get( url )

# https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html#module-selenium.webdriver.common.by
wait.until( EC.presence_of_element_located(
                                            ( By.ID, 'content' ),
                                          )
          )

time_1 = time.time()
print(time_1-time_0,':\n',driver.current_url)


#import pandas as pd

# /html/body/div[2]/div[5]/div[3]/div[2]/table
# df = pd.read_html(driver.page_source)[0]
# print(df)

                                    # /html/body/div[2]/div[5]/div[2]/div[1]

month_div_list = driver.find_elements_by_xpath('//div[@id="content"]/div[@class="filter"]/div')
month_link_list = []
month_list = []

for m in month_div_list:
    page_link = m.find_element_by_xpath('./a').get_attribute('href')# .text # get_attribute('href')
    month_link_list.append(page_link)
    month_list.append( m.find_element_by_xpath('./a').text )
    
time_2 = time.time()    
print('Get all links:', time_2)
print( month_list )

def crawlData( month ):
    
    driver.get( month[0] )
    wait.until( EC.presence_of_element_located(
                                                ( By.ID, 'all_schedule' ),
                                              )
              )
    
    share_export='Get table as CSV (for Excel)'
    #                     //*[@id="schedule_sh"]/div/ul/li/div/ul/li[4]/button
    # /html/body/div[2]/div[5]/ div[3]/div[1]/   div/ul/li/div/ul/li[4]/button
    # <button class="tooltip" tip="Get a link directly to this table on this page" type="button">Get table as CSV (for Excel)</button>
    
    share_export_menu = driver.find_element_by_xpath('//div[@id="schedule_sh"]/div[@class="section_heading_text"]')
    hidden_submenu = driver.find_element_by_xpath('//div[@id="schedule_sh"]/div[@class="section_heading_text"]' \
                                                  '/ul/li/div/ul/li/button[contains(text(),"{}")]'.format(share_export)
                                                 )
    
    actions = ActionChains( driver )
    actions.move_to_element( share_export_menu )
    actions.click( hidden_submenu )
    actions.perform()
    
    
    wait.until( EC.presence_of_element_located(
                                                ( By.ID, 'div_schedule' ),
                                              )
              )
    csv = driver.find_element_by_xpath('//div[@id="div_schedule"]/div/pre[@id="csv_schedule"]').text
    


    df = pd.read_fwf( io.StringIO(csv) )
    df.to_csv( month[1] + '.csv', #'temp.csv', 
               header=False, index=False, sep='\n') # sep='\n'
    


    
time_3 = time.time()


from multiprocessing import Pool

pool = Pool(2) 
pool.map( crawlData,
       zip(month_link_list, month_list)
     )
pool.close()
pool.join()
    
driver.quit()
    
    
# -

# ![image.png](attachment:image.png)

# +
ua = fake_useragent.UserAgent() # .random

ua_list = [ua.ie, ua['Internet Explorer'], ua.msie,
           ua.chrome, ua.google, ua['google chrome'],
           ua.firefox, ua.ff,
           ua.safari, 
           ua.opera
          ]

import random
current_ua = random.choice(ua_list)
header = {'UserAgent': current_ua,
          'Connection': 'close'
         }
options = webdriver.ChromeOptions()
options.add_argument( "'" + "user-agent=" + header['UserAgent'] + "'" )
options.add_argument('--disable-gpu') # google document mentioned this attribute can avoid some bugs
# the purpose of the argument --disable-gpu was to enable google-chrome-headless on windows platform. 
# It was needed as SwiftShader fails an assert on Windows in headless mode ### earlier.###
# it doesn't run the script without opening the browser,but this bug ### was fixed.###
# options.add_argument('--headless') is all you need.
 
# The browser does not provide a visualization page. 
# If the system does not support visualization under linux, it will fail to start if you do not add this one
# options.add_argument('--headless')
 
# Solve the prompt that chrome is being controlled by the automatic test software
options.add_experimental_option('excludeSwitches',['enable-automation'])

options.add_experimental_option('useAutomationExtension', False)


# set the browser as developer model, prevent the website identifying that you are using Selenium
driver = webdriver.Chrome( executable_path = 'C:/chromedriver/chromedriver', # last chromedriver is chromedriver.exe
                            options = options
                          )
# #run command("window.navigator.webdriver")in the Console of the inspection
# #result: undefine  # means: regular browser

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    delete navigator.__proto__.webdriver;
                                    """
                        }
                       )

 
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    Object.defineProperty( navigator, 
                                                           'webdriver', 
                                                           {
                                                             get: () => undefined
                                                           }
                                                )
                                    """
                        }
                       )
                       
                       
# browser.set_window_size(1920, 1080)

root = 'https://www.basketball-reference.com/leagues/'
page_link = 'NBA_2016_games.html#schedule'

url = root + page_link

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
import io 
import pandas as pd

wait = WebDriverWait(driver, 2)

time_0 = time.time()
driver.get( url )

# https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html#module-selenium.webdriver.common.by
wait.until( EC.presence_of_element_located(
                                            ( By.ID, 'content' ),
                                          )
          )

time_1 = time.time()
print(time_1-time_0,':\n',driver.current_url)


#import pandas as pd

# /html/body/div[2]/div[5]/div[3]/div[2]/table
# df = pd.read_html(driver.page_source)[0]
# print(df)

                                    # /html/body/div[2]/div[5]/div[2]/div[1]

month_div_list = driver.find_elements_by_xpath('//div[@id="content"]/div[@class="filter"]/div')
month_link_list = []
month_list = []

for m in month_div_list:
    page_link = m.find_element_by_xpath('./a').get_attribute('href')# .text # get_attribute('href')
    month_link_list.append(page_link)
    month_list.append( m.find_element_by_xpath('./a').text )
    
time_2 = time.time()    
print('Get all links:', time_2)
# month_list

for page_month_index in range( len(month_list) ):
    
    time_3 = time.time()
    
    driver.get( month_link_list[page_month_index] )
    wait.until( EC.presence_of_element_located(
                                                ( By.ID, 'all_schedule' ),
                                              )
              )
    
    share_export='Get table as CSV (for Excel)'
    #                     //*[@id="schedule_sh"]/div/ul/li/div/ul/li[4]/button
    # /html/body/div[2]/div[5]/ div[3]/div[1]/   div/ul/li/div/ul/li[4]/button
    # <button class="tooltip" tip="Get a link directly to this table on this page" type="button">Get table as CSV (for Excel)</button>
    
    share_export_menu = driver.find_element_by_xpath('//div[@id="schedule_sh"]/div[@class="section_heading_text"]')
    hidden_submenu = driver.find_element_by_xpath('//div[@id="schedule_sh"]/div[@class="section_heading_text"]' \
                                                  '/ul/li/div/ul/li/button[contains(text(),"{}")]'.format(share_export)
                                                 )
    
    actions = ActionChains( driver )
    actions.move_to_element( share_export_menu )
    actions.click( hidden_submenu )
    actions.perform()
    
    
    wait.until( EC.presence_of_element_located(
                                                ( By.ID, 'div_schedule' ),
                                              )
              )
    csv = driver.find_element_by_xpath('//div[@id="div_schedule"]/div/pre[@id="csv_schedule"]').text
    

    # Process string format data, save it to csv file 
    df = pd.read_fwf( io.StringIO(csv) )
    df.to_csv( month_list[page_month_index] + '.csv', #'temp.csv', 
               header=False, index=False, sep='\n') # sep='\n'
    
    print(driver.current_url)
driver.quit()    
# -

# ![image.png](attachment:image.png)

# # Combine the data in all CSV files and store them in a file and named it with ‘basketball.csv’

# delete "Mexico" in December.csv and then save it
# ![image.png](attachment:image.png)

# +
df_whole = pd.read_csv( month_list[0] + '.csv',  sep=',', dtype={ 'PTS': 'int',
                                                                  'PTS.1': 'int',
                                                                  'Attend.': 'int',
                                                                  'Notes': "string",
                                                                  'Unnamed: 7': 'object',
                                                                }, 
                        )

for m in range( 1, len(month_list) ):
    cur_m_df = pd.read_csv( month_list[m] + '.csv',  sep=',', dtype={ 'PTS': 'int',
                                                                      'PTS.1': 'int',
                                                                      'Attend.': 'int',
                                                                      'Notes': "string",
                                                                      'Unnamed: 7': 'object',
                                                                    }, 
                          )
    df_whole = pd.merge( df_whole, cur_m_df, how='outer' )

df_whole.shape  
# -

df_whole.to_csv('basketball.csv', index=False)

# # load the dataset using the read_csv function

# +
import pandas as pd

data_filename='basketball.csv'
dataset = pd.read_csv(data_filename)

dataset.head()
# -

# # After looking at the output, we can see a number of problems:
#
# * The date is just a string and not a date object
# * From visually inspecting the results, the headings aren't complete or correct

# +
dataset = pd.read_csv( data_filename, parse_dates=['Date'] )

dataset.columns = ["Date", "Start (ET)", "Visitor Team", "VisitorPts", "Home Team", 
                   "HomePts", "OT?", "Score Type","Attend.", "Notes"]
dataset.head()
# -

dataset = dataset.drop('Attend.', axis=1)
dataset.head()

print(dataset.dtypes)

# # Extracting new features

# specify our class as 1(True) if the home team wins and 0(False) if the visitor team wins
dataset["HomeWin"] = dataset["VisitorPts"] < dataset["HomePts"]
dataset.head()

y_true = dataset["HomeWin"].values

# show the advantage on home team
dataset['HomeWin'].mean()

# +
from collections import defaultdict

won_last = defaultdict(int)
won_last
# -

dataset["HomeLastWin"] = 0
dataset["VisitorLastWin"] = 0
dataset.head()

# +
for row_index, row in dataset.iterrows(): #  (row_index, The data of the row as a Series), ..., ...    
    home_team = row["Home Team"]
    visitor_team = row["Visitor Team"]
    
    row["HomeLastWin"] = won_last[home_team] # won_last[home_team] : whether the home team won the previous game
    
    dataset.at[ row_index, "HomeLastWin" ] = won_last[home_team]
    dataset.at[ row_index, "VisitorLastWin" ] = won_last[visitor_team]
    
    won_last[home_team] = int( row["HomeWin"] )
    won_last[visitor_team] = 1-int( row["HomeWin"] )

dataset.head(n=10)    
# -

dataset.iloc[1000:1005]

# +
from sklearn.tree import DecisionTreeClassifier

clf = DecisionTreeClassifier( random_state=14 ) #  default=”gini”
# -

X_previous_wins = dataset[ ["HomeLastWin", "VisitorLastWin"] ].values
X_previous_wins

# +
from sklearn.model_selection import cross_val_score
import numpy as np

# Accuracy = (TP+TN)/(TP+FP+FN+TN)
#                         to use the default 5-fold cross validation
scores = cross_val_score( clf, X_previous_wins, y_true, scoring='accuracy' )
print( "Accuracy: {0:.1f}%".format( np.mean(scores)*100 ) )
# -

# # Sports outcome prediction

# ![image.png](attachment:image.png)

# +
ua = fake_useragent.UserAgent() # .random

ua_list = [ua.ie, ua['Internet Explorer'], ua.msie,
           ua.chrome, ua.google, ua['google chrome'],
           ua.firefox, ua.ff,
           ua.safari, 
           ua.opera
          ]

import random
current_ua = random.choice(ua_list)
header = {'UserAgent': current_ua,
          'Connection': 'close'
         }
options = webdriver.ChromeOptions()
options.add_argument( "'" + "user-agent=" + header['UserAgent'] + "'" )
options.add_argument('--disable-gpu') # google document mentioned this attribute can avoid some bugs
# the purpose of the argument --disable-gpu was to enable google-chrome-headless on windows platform. 
# It was needed as SwiftShader fails an assert on Windows in headless mode ### earlier.###
# it doesn't run the script without opening the browser,but this bug ### was fixed.###
# options.add_argument('--headless') is all you need.
 
# The browser does not provide a visualization page. 
# If the system does not support visualization under linux, it will fail to start if you do not add this one
# options.add_argument('--headless')
 
# Solve the prompt that chrome is being controlled by the automatic test software
options.add_experimental_option('excludeSwitches',['enable-automation'])

options.add_experimental_option('useAutomationExtension', False)


# set the browser as developer model, prevent the website identifying that you are using Selenium
driver = webdriver.Chrome( executable_path = 'C:/chromedriver/chromedriver', # last chromedriver is chromedriver.exe
                            options = options
                          )
# #run command("window.navigator.webdriver")in the Console of the inspection
# #result: undefine  # means: regular browser

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    delete navigator.__proto__.webdriver;
                                    """
                        }
                       )

 
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", 
                        {
                          "source": """
                                    Object.defineProperty( navigator, 
                                                           'webdriver', 
                                                           {
                                                             get: () => undefined
                                                           }
                                                )
                                    """
                        }
                       )
                       
                       
# browser.set_window_size(1920, 1080)

root = 'https://www.basketball-reference.com/leagues/'
page_link = 'NBA_2015_standings.html'

url = root + page_link

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
import io 
import pandas as pd

wait = WebDriverWait(driver, 2)

driver.get( url )

# https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html#module-selenium.webdriver.common.by
wait.until( EC.presence_of_element_located(
                                            ( By.ID, 'all_expanded_standings' ),
                                          )
          )
    
share_export='Get table as CSV (for Excel)'
    #                     //*[@id="schedule_sh"]/div/ul/li/div/ul/li[4]/button
    # /html/body/div[2]/div[5]/ div[3]/div[1]/   div/ul/li/div/ul/li[4]/button
    # <button class="tooltip" tip="Get a link directly to this table on this page" type="button">Get table as CSV (for Excel)</button>
    
share_export_menu = driver.find_element_by_xpath('//div[@id="expanded_standings_sh"]/div[@class="section_heading_text"]')
hidden_submenu = driver.find_element_by_xpath('//div[@id="expanded_standings_sh"]/div[@class="section_heading_text"]' \
                                                  '/ul/li/div/ul/li/button[contains(text(),"{}")]'.format(share_export)
                                                 )
    
actions = ActionChains( driver )
actions.move_to_element( share_export_menu )
actions.click( hidden_submenu )
actions.perform()
    
    
wait.until( EC.presence_of_element_located(
                                            ( By.ID, 'div_expanded_standings' ),
                                          )
          )
csv = driver.find_element_by_xpath('//div[@id="div_expanded_standings"]/div/pre[@id="csv_expanded_standings"]').text
    
standings_fileName="standings.csv"

df = pd.read_fwf( io.StringIO(csv) )
df.to_csv( standings_fileName, #'temp.csv', 
        header=False, index=False, sep='\n') # sep='\n'
      
print(driver.current_url)

    
driver.quit()
    
# -

# ![image.png](attachment:image.png)

# +
import os

standings = pd.read_csv( "standings.csv", skiprows=1 )
standings.head()

# +
dataset["HomeTeamRanksHigher"] = 0

for index, row in dataset.sort_values(by="Date").iterrows():
    home_team = row["Home Team"]
    visitor_team = row["Visitor Team"]
    
    home_rank = standings[ standings["Team"] == home_team ]["Rk"].values[0]
    visitor_rank = standings[ standings["Team"] == visitor_team ]["Rk"].values[0]
    dataset.at[index, "HomeTeamRanksHigher"] = int( home_rank < visitor_rank )
    
dataset.head(n=10)    
# -

X_homehigher = dataset[ ["HomeTeamRanksHigher", "HomeLastWin", "VisitorLastWin"] ].values
X_homehigher

# +
clf = DecisionTreeClassifier( random_state=14 , criterion='entropy' ) #  default=”gini”

scores = cross_val_score( clf, X_homehigher, y_true, scoring="accuracy" )

print( "Accuracy: {0:.1f}%".format( np.mean(scores)*100 ) )
# -

# This now scores 61.8 percent even better than our previous result, and now better than just using wheather the teams were winner on previous game to do predictions on every time. Can we do better?

# +
last_match_winner = defaultdict( int )
dataset["HomeTeamWonLast"] = 0

for index, row in dataset.iterrows():
    home_team = row["Home Team"]
    visitor_team = row["Visitor Team"]
    
    teams = tuple( sorted( [home_team, visitor_team] ) )
    
    # We look up in our dictionary to see who won the last encounter between 
    # the two teams. Then, we update the row in the dataset data frame:
    if last_match_winner[teams] : # == row["Home Team"]
        home_team_won_last = 1
    else: 
        home_team_won_last = 0
    
    dataset.at[index, "HomeTeamWonLast"] = home_team_won_last
    
    # Who won this match?
    if row["HomeWin"]:
        winner = row["Home Team"]
    else:
        winner = row["Visitor Team"]
    
    last_match_winner[teams] = winner
# -

# This feature works much like our previous rank-based feature. However, instead of looking up the ranks, this features creates a tuple called teams, and then stores the previous result in a dictionary( last_match_winner = defaultdict( int ) ). When those two teams play each other next, it recreates this tuple, and looks up the previous result. Our code doesn't differentiate between home games and visitor games, which might be a useful improvement to look at implementing. 

dataset.iloc[15:20]

# Memphis Grizzlies (Visitor) won the 17th match against Indiana Pacers (home)

dataset.iloc[345:405]

# Houston Rockets (home) won the 348th match against Los Angeles Lakers (Visitor), so the 384th Houston Rockets (Visitor) and Los Angeles Lakers (home) record (HomeTeamWonLast) value is 0.
#
# Indiana Pacers lost in the 17th matchup against Memphis Grizzlies, so in the 401st Memphis Grizzlies(home) and Indiana Pacers (visitor) record (HomeTeamWonLast) value is 1.

# +
X_lastwinner = dataset[ ["HomeTeamWonLast",# The result of the last match between the two teams
                         "HomeTeamRanksHigher", 
                         "HomeLastWin",    # Results of the last match against other teams
                         "VisitorLastWin", # Results of the last match against other teams
                        ] ]
# the Gini index and the cross-entropy are quite similar numerically
clf = DecisionTreeClassifier( random_state=14, criterion="entropy" )
scores = cross_val_score( clf, X_lastwinner, y_true, scoring = "accuracy")

print( "Accuracy: {0:.1f}%".format( np.mean(scores)*100) )
# -

# While decision trees are capable of learning from categorical features, the implementation in scikit-learn requires those features to be encoded as numbers and features, instead of string values. We can use the LabelEncoder transformer to convert the string-based team names into assigned integer values. The code is as follows:

# +
from sklearn.preprocessing import LabelEncoder

encoding = LabelEncoder()
encoding.fit( dataset["Home Team"].values )

home_teams = encoding.transform( dataset["Home Team"].values )
visitor_teams = encoding.transform( dataset["Visitor Team"].values )

X_teams = np.vstack( [home_teams,   # [ 4,  0,  9, ...,  9,  5,  9]
                      visitor_teams # [ 5,  8, 18, ...,  5,  9,  5]
                     ] 
                   ).T
X_teams
# -

# These integers can be fed into the Decision Tree, but they will still be interpreted as continuous features by DecisionTreeClassifier. For example, teams may be allocated integers from 0 to 16. *The algorithm will see teams 1 and 2 as being similar, while teams 4 and 10 will be very different*--but this makes no sense as all. All of the teams are different from each other--two teams are either the same or they are not!

# using OneHotEncoder

# +
from sklearn.preprocessing import OneHotEncoder

onehot = OneHotEncoder()
X_teams = onehot.fit_transform( X_teams ).todense()

X_teams[-3:]
# -

# [9,5]: Represents the combination of Cleveland Cavaliers (as a visitor team, encoded 9) and Golden State Warriors (as a home team, encoded 5) as a category for OneHotEncoder encoding, and its encoding value is not same with [5,9], Golden State Warriors (as a visitor team, encoded 5) and Cleveland Cavaliers (as a home team, encoded 9) as a new combination, the encoding value of OneHotEncoder is different

dataset[-3:]

# +
clf = DecisionTreeClassifier( random_state=14 )
scores = cross_val_score( clf, X_teams, y_true, scoring='accuracy') # default cv=5

print( 'Accuracy: {0:.1f}%'.format(np.mean(scores)*100) )

# +
from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier( random_state=14) # default criterion='gini', n_estimators=100
score = cross_val_score( clf, X_teams, y_true, scoring="accuracy" )# default cv=5

print( "Accuracy: {0:.1f}%".format( np.mean(scores)*100 ) )

# +
X_all = np.hstack( [X_lastwinner, X_teams] )
clf = RandomForestClassifier( random_state=14 ) # default criterion='gini', n_estimators=100
scores = cross_val_score( clf, X_all, y_true, scoring="accuracy" )

print( "Accuracy: {0:.1f}%".format(np.mean(scores)*100) )

# +
X_all = np.hstack( [X_lastwinner, X_teams] )
clf = RandomForestClassifier( random_state=14, n_estimators=500 ) # default criterion='gini', n_estimators=100
scores = cross_val_score( clf, X_all, y_true, scoring="accuracy" )

print( "Accuracy: {0:.1f}%".format(np.mean(scores)*100) )

# +
from sklearn.model_selection import GridSearchCV

parameter_space = {
    "max_features":[2,10,'auto'],
    "n_estimators":[100,200],
    "criterion":["gini","entropy"],
    "min_samples_leaf":[2,4,6]
}

clf = RandomForestClassifier( random_state=14 )
grid = GridSearchCV( clf, parameter_space )
grid.fit( X_all, y_true )

print( "Accuracy: {0:.1f}%".format(grid.best_score_*100) )
# -

# we can print out the best model that was found in the grid search. The code is as follows

print( grid.best_estimator_ )



StarcoderdataJupyterScriptsDedupFiltered