Kaggle competition: Google Cloud & NCAA® ML Competition 2019-Women's
<SOS> metric: LogLoss Kaggle data source: womens-machine-learning-competition-2019<correct_missing_values>
import numpy as np
import pandas as pd
from sklearn.metrics import log_loss
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
freq_port = train_data.Embarked.dropna().mode() [0]
freq_port<data_type_conversions>
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' )
train_data['Embarked'] = train_data['Embarked'].fillna(freq_port)
 test_data['Embarked'] = test_data['Embarked'].fillna(freq_port )<categorify>
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
train_data['Embarked'] = encoder.fit_transform(train_data['Embarked'])
 train_data.head()<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} )
test_data['Embarked'] = encoder.fit_transform(test_data['Embarked'])
 test_data.head()
df = [train_data, test_data]<data_type_conversions>
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 )
freq_fare = test_data.Fare.dropna().mode() [0]
freq_fare.astype('int')
<feature_engineering>
games['Team1Seed'] = games['IDTeam1'].map(seeds ).fillna(0)
 games['Team2Seed'] = games['IDTeam2'].map(seeds ).fillna(0 )
test_data['Fare'] = test_data['Fare'].fillna(freq_fare)
 test_data.info()<data_type_conversions>
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 )
for row in df:
row.loc[ row['Fare'] <= 7.91, 'Fare'] = 0
row.loc[(row['Fare'] > 7.91)&(row['Fare'] <= 14.454), 'Fare'] = 1
row.loc[(row['Fare'] > 14.454)&(row['Fare'] <= 31), 'Fare'] = 2
row.loc[ row['Fare'] > 31, 'Fare'] = 3
row['Fare'] = row['Fare'].astype(int)
 train_data = train_data.drop(['FareBand'], axis=1)
 combine = [train_data, test_data]
train_data.head(10 )<drop_column>
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 )
train_data = train_data.drop(['Age', 'FamilySize'], axis = 1)
 test_data = test_data.drop(['Age', 'FamilySize'], axis = 1 )<prepare_x_and_y>
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' )
X_train = train_data.drop("Survived", axis=1)
 Y_train = train_data["Survived"]
X_test = test_data.drop("PassengerId", axis=1 ).copy()
X_train.shape, Y_train.shape, X_test.shape<train_model>
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]
random_forest = RandomForestClassifier(n_estimators=100)
 random_forest.fit(X_train, Y_train)
 Y_pred = random_forest.predict(X_test)
 random_forest.score(X_train, Y_train)
 acc_random_forest = round(random_forest.score(X_train, Y_train)* 100, 2)
 acc_random_forest<create_dataframe>
forest = RandomForestRegressor(n_estimators=100, bootstrap=True, n_jobs=-1)
 forest.fit(games[col].fillna(-1), games['Pred'] )
submission = pd.DataFrame({
"PassengerId": test_data["PassengerId"],
"Survived": Y_pred
} )<save_to_csv>
predictions = forest.predict(games[col].fillna(-1)).clip(0,1)
 print('Log Loss:', log_loss(games['Pred'], predictions))
<save_to_csv><EOS> 
sub['Pred'] = forest.predict(sub[col].fillna(-1)).clip(0,1)
 sub[['ID', 'Pred']].to_csv('submission.csv', index=False )