Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Women's
<SOS> metric: LogLoss Kaggle data source: womens-machine-learning-competition-2019<categorify>
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import ExtraTreesClassifier
all_set = label_encode(all_set, 'Title')
 compare(all_set, 'Title' )<feature_engineering>
teams = pd.read_csv('.. /input/wdatafiles/WTeams.csv')
 teams2 = pd.read_csv('.. /input/wdatafiles/WTeamSpellings.csv', encoding='latin-1')
 season_cresults = pd.read_csv('.. /input/wdatafiles/WRegularSeasonCompactResults.csv')
 season_dresults = pd.read_csv('.. /input/wdatafiles/WRegularSeasonDetailedResults.csv')
 tourney_cresults = pd.read_csv('.. /input/wdatafiles/WNCAATourneyCompactResults.csv')
 tourney_dresults = pd.read_csv('.. /input/wdatafiles/WNCAATourneyDetailedResults.csv')
 slots = pd.read_csv('.. /input/wdatafiles/WNCAATourneySlots.csv')
 seeds = pd.read_csv('.. /input/wdatafiles/WNCAATourneySeeds.csv')
 seeds = {'_'.join(map(str,[int(k1),k2])) :int(v[1:3])for k1, v, k2 in seeds[['Season', 'Seed', 'TeamID']].values}
seeds = {**seeds, **{k.replace('2018_','2019_'):seeds[k] for k in seeds if '2018_' in k}}
cities = pd.read_csv('.. /input/wdatafiles/WCities.csv')
 gcities = pd.read_csv('.. /input/wdatafiles/WGameCities.csv')
 seasons = pd.read_csv('.. /input/wdatafiles/WSeasons.csv')
 sub = pd.read_csv('.. /input/WSampleSubmissionStage2.csv' )
all_set['FarePerFamilyMember'] = all_set['Fare'] / all_set['Family']
all_set['FareBin'] = pd.qcut(all_set['Fare'], 5)
 all_set['FareCode'] = all_set['FareBin']
all_set = label_encode(all_set, 'FareCode')
 all_set['Fare'] = all_set['Fare'].apply(lambda x: np.log(x)if x > 0 else np.log(3.0))<drop_column>
teams2 = teams2.groupby(by='TeamID', as_index=False)['TeamNameSpelling'].count()
teams2.columns = ['TeamID', 'TeamNameCount']
teams = pd.merge(teams, teams2, how='left', on=['TeamID'])
 del teams2
all_tmp = all_set.drop(columns=['PassengerId', 'Name', 'Ticket', 'Cabin', 'AgeBin', 'AgeCode', 'AgeDist', 'FareBin', 'FareCode'] )<categorify>
season_cresults['ST'] = 'S'
season_dresults['ST'] = 'S'
tourney_cresults['ST'] = 'T'
tourney_dresults['ST'] = 'T'
games = pd.concat(( season_dresults, tourney_dresults), axis=0, ignore_index=True)
 games.reset_index(drop=True, inplace=True)
 games['WLoc'] = games['WLoc'].map({'A': 1, 'H': 2, 'N': 3} )
all_dum = pd.get_dummies(all_tmp, drop_first=True)
 print(all_dum.shape)
 train_set = all_dum[all_dum['Survived'] < 3].copy()
test_set = all_dum[all_dum['Survived'] == 3].copy()<prepare_x_and_y>
games['ID'] = games.apply(lambda r: '_'.join(map(str, [r['Season']]+sorted([r['WTeamID'],r['LTeamID']]))), axis=1)
 games['IDTeams'] = games.apply(lambda r: '_'.join(map(str, sorted([r['WTeamID'],r['LTeamID']]))), axis=1)
 games['Team1'] = games.apply(lambda r: sorted([r['WTeamID'],r['LTeamID']])[0], axis=1)
 games['Team2'] = games.apply(lambda r: sorted([r['WTeamID'],r['LTeamID']])[1], axis=1)
 games['IDTeam1'] = games.apply(lambda r: '_'.join(map(str, [r['Season'], r['Team1']])) , axis=1)
 games['IDTeam2'] = games.apply(lambda r: '_'.join(map(str, [r['Season'], r['Team2']])) , axis=1 )
X = train_set.copy()
try:
X.drop(columns=['Survived'], inplace=True)
 except:
pass
y = train_set['Survived'].copy()
scaler = StandardScaler().fit(X)
 scl_X = scaler.transform(X)
 tX = test_set.copy()
try:
tX.drop(columns=['Survived'], inplace=True)
 except:
pass
scl_tX = scaler.transform(tX )<split>
games['Team1Seed'] = games['IDTeam1'].map(seeds ).fillna(0)
 games['Team2Seed'] = games['IDTeam2'].map(seeds ).fillna(0 )
train_X, val_X, train_y, val_y = train_test_split(scl_X, y, test_size=0.33, random_state=42, stratify=y )<train_model>
games['ScoreDiff'] = games['WScore'] - games['LScore']
games['Pred'] = games.apply(lambda r: 1.if sorted([r['WTeamID'],r['LTeamID']])[0]==r['WTeamID'] else 0., axis=1)
 games['ScoreDiffNorm'] = games.apply(lambda r: r['ScoreDiff'] * -1 if r['Pred'] == 0.else r['ScoreDiff'], axis=1)
 games['SeedDiff'] = games['Team1Seed'] - games['Team2Seed']
games = games.fillna(-1 )
rfc = RandomForestClassifier(n_estimators=100)
 rfc.fit(train_X, train_y)
 score = rfc.score(val_X, val_y)
 print('RandomForestClassifier =', score)
 pred_y = rfc.predict(val_X)
 target_names = ['DEAD', 'SURVIVED']
print(classification_report(val_y, pred_y,target_names=target_names))<find_best_params>
sub['WLoc'] = 3
sub['Season'] = sub['ID'].map(lambda x: x.split('_')[0])
 sub['Season'] = sub['ID'].map(lambda x: x.split('_')[0])
 sub['Season'] = sub['Season'].astype(int)
 sub['Team1'] = sub['ID'].map(lambda x: x.split('_')[1])
 sub['Team2'] = sub['ID'].map(lambda x: x.split('_')[2])
 sub['IDTeams'] = sub.apply(lambda r: '_'.join(map(str, [r['Team1'], r['Team2']])) , axis=1)
 sub['IDTeam1'] = sub.apply(lambda r: '_'.join(map(str, [r['Season'], r['Team1']])) , axis=1)
 sub['IDTeam2'] = sub.apply(lambda r: '_'.join(map(str, [r['Season'], r['Team2']])) , axis=1)
 sub['Team1Seed'] = sub['IDTeam1'].map(seeds ).fillna(0)
 sub['Team2Seed'] = sub['IDTeam2'].map(seeds ).fillna(0)
 sub['SeedDiff'] = sub['Team1Seed'] - sub['Team2Seed']
sub = sub.fillna(-1 )
best_model = 0
best_score = score<find_best_params>
games = pd.merge(games, gb, how='left', left_on='IDTeams', right_on='IDTeams_c_score')
 sub = pd.merge(sub, gb, how='left', left_on='IDTeams', right_on='IDTeams_c_score' )
clfs = []
clfs.append(( 'ada', AdaBoostClassifier))
clfs.append(( 'bag', BaggingClassifier))
clfs.append(( 'rnd', RandomForestClassifier))
clfs.append(( 'knn', KNeighborsClassifier))
clfs.append(( 'mlp', MLPClassifier))
clfs.append(( 'ext', ExtraTreesClassifier))
clfs.append(( 'log', LogisticRegression))
clfs.append(( 'gbm', GradientBoostingClassifier))
params = []
params.append({'n_estimators': np.arange(10,500,10), 'learning_rate':[float(x/100.) for x in np.arange(1,10)]})
 params.append({'n_estimators': np.arange(10,500,10)})
 params.append({'n_estimators': np.arange(10,500,10)})
 params.append({'n_neighbors': np.arange(3,15)})
 params.append({'hidden_layer_sizes': [(100,),(200,),(300,),(400,),(500,)]})
 params.append({'n_estimators': np.arange(10,200,10)})
 params.append({'max_iter': np.arange(10,500,10)})
 params.append({'n_estimators': np.arange(10,500,10), 'learning_rate':[float(x/100.) for x in np.arange(1,10)], 'max_depth':np.arange(3,10)} )<train_on_grid>
col = [c for c in games.columns if c not in ['ID', 'DayNum', 'ST', 'Team1', 'Team2', 'IDTeams', 'IDTeam1', 'IDTeam2', 'WTeamID', 'WScore', 'LTeamID', 'LScore', 'NumOT', 'Pred', 'ScoreDiff', 'ScoreDiffNorm', 'WLoc'] + c_score_col]
best_estimators = []
best_params = []
estimators_weights = []
k = len(params)
 for idx in range(len(clfs)) :
gs = RandomizedSearchCV(clfs[idx][1]() , params[idx], cv=5)
 gs.fit(train_X, train_y)
 estimators_weights.append(gs.score(val_X, val_y))
best_estimators.append(gs.best_estimator_)
 best_params.append(gs.best_params_)
 print(k, clfs[idx][0], gs.best_params_)
 k -= 1<define_variables>
model = ExtraTreesClassifier(n_estimators=200)
 model.fit(games[col].fillna(-1), games['Pred'])
 predictions = model.predict(games[col].fillna(-1)).clip(0,1)
 print('Log Loss:', log_loss(games['Pred'], predictions))
estimator_list = list(zip([name for(name, model)in clfs], best_estimators))<train_model>
sub['Pred'] = model.predict(sub[col].fillna(-1)).clip(1,0)
 sub[['ID', 'Pred']].to_csv('submission_et.csv', index=False )
<predict_on_test><EOS> 
FileLink('./submission_et.csv' )