Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAM
<SOS> metric: logloss Kaggle data source: google-cloud-ncaa-march-madness-2020-division-1-mens-tournament<import_modules>
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV
import numpy as np
import pandas as pd
import tensorflow as tf
import os
import math
from IPython.display import clear_output
import matplotlib.pyplot as plt
from sklearn.utils import shuffle<load_from_csv>
tourney_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 tourney_seed = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv' )
training_data_raw = pd.read_csv("/kaggle/input/titanic/train.csv")
 testing_data_raw = pd.read_csv("/kaggle/input/titanic/test.csv")
 print(training_data_raw.head())
 print(testing_data_raw.head() )<create_dataframe>
tourney_result = tourney_result.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], axis=1)
 tourney_result
training_data = training_data_raw[['Sex','Name','Age','Pclass','SibSp','Parch','Fare','Survived']]
testing_data = testing_data_raw[['Sex','Name','Age','Pclass','SibSp','Parch','Fare']]<count_missing_values>
tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'WSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Seed':'LSeed'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result
print(( training_data[['Name','Sex','Age','Pclass','SibSp','Parch','Fare','Survived']].isnull() ).sum())
 print(( testing_data[['Name','Sex','Age','Pclass','SibSp','Parch','Fare']].isnull() ).sum() )<feature_engineering>
def get_seed(x):
return int(x[1:3])
 tourney_result['WSeed'] = tourney_result['WSeed'].map(lambda x: get_seed(x))
tourney_result['LSeed'] = tourney_result['LSeed'].map(lambda x: get_seed(x))
tourney_result
def handle_nans(dataframe):
dataframe['HasNAN'] = 0
for column in dataframe:
if dataframe[column].dtypes != 'int32' and dataframe[column].dtypes != 'float32' and \
dataframe[column].dtypes != 'int64' and dataframe[column].dtypes != 'float64':
continue
for row, value in dataframe[column].iteritems() :
if math.isnan(value):
dataframe.loc[row,'HasNAN'] = 1
if dataframe[column].isnull().sum() != 0:
temp = testing_data[column]
median = temp.append(training_data[column] ).median()
dataframe[column] = dataframe[column].fillna(median)
 return dataframe 
<categorify>
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv' )
training_data = handle_nans(training_data)
 testing_data = handle_nans(testing_data)
<categorify>
season_win_result = season_result[['Season', 'WTeamID', 'WScore']]
season_lose_result = season_result[['Season', 'LTeamID', 'LScore']]
season_win_result.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)
 season_lose_result.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)
 season_result = pd.concat(( season_win_result, season_lose_result)).reset_index(drop=True)
 season_result
def get_title(name):
first_part_name = name.split(",")[1]
title = first_part_name.split(" ")[1]
return title
def age_class_dummies(dataframe):
dataframe['Sex'] = pd.factorize(dataframe['Sex'])[0]
dataframe['Name'] = dataframe['Name'].apply(get_title)
 dataframe['Name'] = pd.factorize(dataframe['Name'])[0]
dataframe = dataframe.rename(columns={'Name':'Title'})
 dataframe = pd.get_dummies(dataframe, columns=['Title'])
 dataframe = dataframe.astype('int32')
 return dataframe
training_data = age_class_dummies(training_data)
 testing_data = age_class_dummies(testing_data)
 for column in training_data:
if(column not in testing_data)and(column != 'Survived'):
print("Column not in testing data: ", column)
 testing_data[column] = 0
for column in testing_data:
if column not in training_data:
print("Column not in training data: ", column)
 training_data[column] = 0
training_data = training_data.reindex(sorted(training_data.columns), axis=1)
 testing_data = testing_data.reindex(sorted(testing_data.columns), axis=1)
<categorify>
season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()
season_score
print(len(training_data))
validation_split = int(len(training_data)/ 2)
 def shuffle_data() :
global train_x, train_y, validation_x, validation_y, test_x
shuffled_training_data = shuffle(training_data)
 shuffled_training_data.reset_index(inplace=True, drop=True)
 train_x = shuffled_training_data.drop(columns=['Survived'] ).to_numpy()
train_y = shuffled_training_data['Survived'].to_numpy()
validation_x = train_x[validation_split + 1:]
validation_y = train_y[validation_split + 1:]
train_x = train_x[0:validation_split]
train_y = train_y[0:validation_split]
test_x = testing_data.to_numpy()<import_modules>
tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'WScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'LScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result
from xgboost import XGBRegressor, XGBClassifier
from sklearn.metrics import mean_absolute_error<find_best_model_class>
tourney_win_result = tourney_result.drop(['Season', 'WTeamID', 'LTeamID'], axis=1)
 tourney_win_result.rename(columns={'WSeed':'Seed1', 'LSeed':'Seed2', 'WScoreT':'ScoreT1', 'LScoreT':'ScoreT2'}, inplace=True)
 tourney_win_result
loss_log = pd.DataFrame(columns=['loss'])
 best_model = None
best_score = 99
for i in range(0,50):
shuffle_data()
model = XGBClassifier(n_estimators=2000, learning_rate=0.001)
 model.fit(train_x, train_y, early_stopping_rounds=150,
eval_set=[(train_x, train_y)], verbose=False)
 predictions = model.predict(validation_x)
 mae = mean_absolute_error(predictions, validation_y)
 if mae < best_score:
best_score = mae
loss_log.loc[len(loss_log)] = [mae]
loss_log.plot(figsize=[20,4])
 plt.show()<predict_on_test>
tourney_lose_result = tourney_win_result.copy()
tourney_lose_result['Seed1'] = tourney_win_result['Seed2']
tourney_lose_result['Seed2'] = tourney_win_result['Seed1']
tourney_lose_result['ScoreT1'] = tourney_win_result['ScoreT2']
tourney_lose_result['ScoreT2'] = tourney_win_result['ScoreT1']
tourney_lose_result
test_y = model.predict(test_x)
 test_y = test_y.flatten()<save_to_csv>
tourney_win_result['Seed_diff'] = tourney_win_result['Seed1'] - tourney_win_result['Seed2']
tourney_win_result['ScoreT_diff'] = tourney_win_result['ScoreT1'] - tourney_win_result['ScoreT2']
tourney_lose_result['Seed_diff'] = tourney_lose_result['Seed1'] - tourney_lose_result['Seed2']
tourney_lose_result['ScoreT_diff'] = tourney_lose_result['ScoreT1'] - tourney_lose_result['ScoreT2']
final_prediction = pd.DataFrame(columns=['PassengerId','Survived'])
 final_prediction['Survived'] = test_y
final_prediction['PassengerId'] = testing_data_raw['PassengerId']
print(final_prediction.head())
 final_prediction.to_csv('submission.csv', index=False )<load_from_csv>
tourney_win_result['result'] = 1
tourney_lose_result['result'] = 0
tourney_result = pd.concat(( tourney_win_result, tourney_lose_result)).reset_index(drop=True)
 tourney_result
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv')
 features = ["Pclass", "Sex", "SibSp", "Parch"]
X_train = pd.get_dummies(train_data[features])
 y_train = train_data["Survived"]
final_X_test = pd.get_dummies(test_data[features])
 classifier = XGBClassifier(n_estimators=750,learning_rate=0.02,max_depth=3)
 classifier.fit(X_train, y_train)
 predictions = classifier.predict(final_X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False )<import_modules>
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import RandomizedSearchCV
from sklearn.preprocessing import LabelEncoder<load_from_csv>
test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))
test_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))
test_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))
test_df
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()<load_from_csv>
test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test.head()<load_from_csv>
test_df['Seed1'] = test_df['Seed1'].map(lambda x: get_seed(x))
test_df['Seed2'] = test_df['Seed2'].map(lambda x: get_seed(x))
test_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2']
test_df['ScoreT_diff'] = test_df['ScoreT1'] - test_df['ScoreT2']
test_df = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)
 test_df
df_sub = pd.read_csv("/kaggle/input/titanic/gender_submission.csv")
 df_sub.head()<prepare_x_and_y>
h2o.init(
 nthreads=-1,
max_mem_size = "8G" )
X, y = train.drop(['PassengerId','Name','Survived'], axis = 1), train[['Survived']] 
<count_missing_values>
train_1=h2o.H2OFrame(tourney_result)
 train_1['result']=train_1['result'].asfactor()
X.isnull().sum()<data_type_conversions>
param = {
"ntrees" : 2000
, "max_depth" : 20
, "learn_rate" : 0.02
, "sample_rate" : 0.7
, "col_sample_rate_per_tree" : 0.9
, "min_rows" : 5
, "seed": 4241
, "score_tree_interval": 100,"stopping_metric" :"MSE","nfolds":8,"fold_assignment":"AUTO","keep_cross_validation_predictions" : True,"booster":"dart"
}
model_xgb = H2OXGBoostEstimator(**param)
 model_xgb.train(x = train_list, y = 'result', training_frame = train_1 )
X.Age = X.Age.fillna(X.Age.median())
 X.Cabin = X.Cabin.fillna(X.Cabin.mode())
 X.Embarked = X.Embarked.fillna(X.Embarked.mode())
<count_missing_values>
param={
"ntrees" : 1000
, "max_depth" : 20
, "learn_rate" : 0.02
, "sample_rate" : 0.7
, "col_sample_rate_per_tree" : 0.9
, "min_rows" : 5
, "seed": 4241
, "score_tree_interval": 100,"stopping_metric" :"MSE","nfolds":8,"fold_assignment":"AUTO","keep_cross_validation_predictions" : True
}
model_gbm = H2OGradientBoostingEstimator(**param)
 model_gbm.train(x = train_list, y = 'result', training_frame = train_1 )
X.isnull().sum()<drop_column>
stack = H2OStackedEnsembleEstimator(model_id="ensemble11",
training_frame=train_1,
base_models=[model_xgb.model_id,model_gbm.model_id],metalearner_algorithm="glm")
 stack.train(x=train_list, y="result", training_frame=train_1)
X_test = test.drop(['PassengerId', 'Name'], axis = 1 )<count_missing_values>
test_1=h2o.H2OFrame(test_df )
X_test.isnull().sum()<data_type_conversions>
pred1=model_xgb.predict(test_1)
 pred2=model_gbm.predict(test_1)
X_test.Cabin = X_test.Cabin.astype('str')
 X_test.Fare = X_test.Fare.astype('float' )<count_missing_values>
pred_df1=pred1.as_data_frame()
pred_df2=pred2.as_data_frame()
<count_missing_values><EOS> 
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = pred_df2['p1']*0.9+pred_df1['p1']*0.1
submission_df
submission_df.to_csv('submission.csv', index=False )