Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Women's
<SOS> metric: LogLoss Kaggle data source: womens-machine-learning-competition-2019<train_model>
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import AdaBoostClassifier
fit_X = np.zeros(( train_y.shape[0], len(best_estimators)))
 fit_X = pd.DataFrame(fit_X)
 pred_X = np.zeros(( val_y.shape[0], len(best_estimators)))
 pred_X = pd.DataFrame(pred_X)
 test_X = np.zeros(( scl_tX.shape[0], len(best_estimators)))
 test_X = pd.DataFrame(test_X)
 print("Fitting models.")
 cols = list()
for i,(name, m)in enumerate(estimator_list):
print("%s..." % name, end=" ", flush=False)
 fit_X.iloc[:, i] = m.predict_proba(train_X)[:, 1]
pred_X.iloc[:, i] = m.predict_proba(val_X)[:, 1]
test_X.iloc[:, i] = m.predict_proba(scl_tX)[:, 1]
cols.append(name)
 print("done")
 fit_X.columns = cols
pred_X.columns = cols
test_X.columns = cols<train_on_grid>
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/WSampleSubmissionStage1.csv' )
meta_estimator = GradientBoostingClassifier()
meta_params = {'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)}
meta_estimator = RandomizedSearchCV(GradientBoostingClassifier() , meta_params, cv=5)
 meta_estimator.fit(fit_X, train_y)
 score = meta_estimator.score(pred_X, val_y)
 print('MetaEstimator =', score)
 if score > best_score:
best_model = 2
best_score = score<predict_on_test>
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
pred_y = meta_estimator.predict(pred_X)
 target_names = ['DEAD', 'SURVIVED']
print(classification_report(val_y, pred_y,target_names=target_names))<train_model>
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} )
models = ['RandomForestClassifier', 'VotingClassifier', 'MetaEstimator']
print('Best model = ', models[best_model] )<save_to_csv>
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 )
if best_model == 0:
predictions = rfc.predict(scl_tX)
 elif best_model == 1:
predictions = vclf.predict(scl_tX)
 else:
predictions = meta_estimator.predict(test_X)
 PassengerId = test_df['PassengerId'].values
results = pd.DataFrame({ 'PassengerId': PassengerId, 'Survived': predictions })
 results.to_csv('results.csv', index=False )<load_from_csv>
games['Team1Seed'] = games['IDTeam1'].map(seeds ).fillna(0)
 games['Team2Seed'] = games['IDTeam2'].map(seeds ).fillna(0 )
gender_data = pd.read_csv('/kaggle/input/titanic/gender_submission.csv')
 train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<drop_column>
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 )
train_data = train_data.drop(['Ticket', 'Cabin'], axis = 1)
 test_data = test_data.drop(['Ticket', 'Cabin'], axis = 1 )<feature_engineering>
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 )
df = [train_data, test_data]
for row in df:
row['Title'] = row.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_data['Title'], train_data['Sex'] )<feature_engineering>
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' )
for row in df:
row['Title'] = row['Title'].replace(['Lady', 'Countess','Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 row['Title'] = row['Title'].replace('Mlle', 'Miss')
 row['Title'] = row['Title'].replace('Ms', 'Miss')
 row['Title'] = row['Title'].replace('Mme', 'Mrs')
 train_data[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
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]
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
for row in df:
row['Title'] = row['Title'].map(title_mapping)
 row['Title'] = row['Title'].fillna(0 )<drop_column>
model = AdaBoostClassifier(n_estimators=200, learning_rate=1.4)
 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))
<define_variables><EOS> 
sub['Pred'] = model.predict(sub[col].fillna(-1)).clip(0,1)
 sub[['ID', 'Pred']].to_csv('submission.csv', index=False )