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<load_from_csv>
YEAR = 2021
STAGE = 2
NCAAM = True
NCAAW = False
train = pd.read_csv("/kaggle/input/titanic/train.csv")
 train.head()<load_from_csv>
if NCAAM:
DATA_DIR = f'.. /input/ncaam-march-mania-2021/MDataFiles_Stage{STAGE}/'
if NCAAW:
DATA_DIR = f'.. /input/ncaaw-march-mania-2021/WDataFiles_Stage{STAGE}/'
test = pd.read_csv("/kaggle/input/titanic/test.csv")
 test.head()<train_model>
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
from sklearn.model_selection import KFold
import lightgbm as lgb
import xgboost as xgb
from xgboost import XGBClassifier
import gc
print('Train columns with null values:',train.isnull().sum() , sep = '
')
 print("-"*42)
 print('Test/Validation columns with null values:', test.isnull().sum() ,sep = '
')
 print("-"*42 )<feature_engineering>
if YEAR == 2020:
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')
 elif YEAR == 2021:
tourney_result = pd.read_csv(DATA_DIR + 'MNCAATourneyCompactResults.csv')
 tourney_seed = pd.read_csv(DATA_DIR + 'MNCAATourneySeeds.csv' )
test[test['Fare'].isnull() ]
test['Fare'] = test['Fare'].fillna(test['Fare'].mean() )<count_missing_values>
tourney_result = tourney_result.drop(['DayNum', 'WScore', 'LScore', 'WLoc', 'NumOT'], axis=1)
 tourney_result
print('Number of missing values in Fare column for test set:', test.Fare.isnull().sum() )<feature_engineering>
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
train[train['Embarked'].isnull() ]
train['Embarked'] = train['Embarked'].fillna('S' )<count_missing_values>
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
print('Number of missing values in Embarked column for train set:', test.Embarked.isnull().sum() )<categorify>
if YEAR == 2020:
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
 elif YEAR == 2021:
season_result = pd.read_csv(DATA_DIR + 'MRegularSeasonCompactResults.csv' )
y = train["Survived"]
features = ["Sex", "Embarked", "Pclass", "Fare"]
X = pd.get_dummies(train[features])
 X_test = pd.get_dummies(test[features])
 model = RandomForestClassifier(n_estimators=125, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})
 output.to_csv('contract_submission.csv', index=False)
 print("Your submission was successfully saved!" )<load_from_csv>
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
path = '.. /input/covid19-global-forecasting-week-2/'
train = pd.read_csv(path + 'train.csv')
 column_name_map = {
'Country_Region' : 'country',
'Province_State' : 'state',
'Date' : 'date',
'ConfirmedCases' : 'cases',
'Fatalities' : 'deaths',
}
train = train.rename(columns = column_name_map)
 from_date = train['date'].min()
to_date = train['date'].max()
train['state'].fillna('-', inplace = True)
 print(train.dtypes )<load_from_csv>
season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()
season_score
path = '.. /input/covid19-global-forecasting-week-2/'
test = pd.read_csv(path + 'test.csv')
 column_name_map = {
'Country_Region' : 'country',
'Province_State' : 'state',
'Date' : 'date',
'ForecastId' : 'id',
}
test = test.rename(columns = column_name_map)
 test['state'].fillna('-', inplace = True)
 regions = test[['state', 'country']].drop_duplicates()
regions.head()<feature_engineering>
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
def logistic(xs, l, L, k, x_0):
result = []
for x in xs:
xp = k*(x-x_0)
 if xp >= 0:
result.append(l +(L-l)/(1.+ np.exp(-xp)))
 else:
result.append(l +(L-l)* np.exp(xp)/(1.+ np.exp(xp)))
 return result
def date_day_diff(d1, d2):
delta = dt.datetime.strptime(d1, "%Y-%m-%d")- dt.datetime.strptime(d2, "%Y-%m-%d")
 return delta.days<filter>
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
predictions_cases = []
plot = True
for index, region in regions.iterrows() :
predicted = False
st = region['state']
co = region['country']
rdata = train[(train['state']==st)&(train['country']==co)]
rtest = test[(test['state']==st)&(test['country']==co)]
window = rdata[rdata['cases']>=100]['date']
if(window.count() < 10):
window = rdata[rdata['cases']>=10]['date']
if(window.count() >= 10):
start_date = window.min()
rdata = rdata[rdata['date']>=start_date]
t = rdata['date'].values
t = [float(date_day_diff(d, start_date)) for d in t]
y = rdata['cases'].values
try:
bounds =([-1e6, -1e6, 0.001, 0.0], [1e6, 1e6, 0.999, t[-1]+10])
 popt, pcov = curve_fit(logistic, t, y, bounds = bounds)
 residuals = y - logistic(t, *popt)
 ss_res = np.sum(residuals**2)
 ss_tot = np.sum(( y - np.mean(y)) **2)
 rs = 1 -(ss_res / ss_tot)
 if plot:
print(st, co)
 print(popt)
 print('R squared: ', rs)
 T = np.arange(0, 60, 1 ).tolist()
yfit = logistic(T, *popt)
 pylab.plot(t, y, 'o')
 pylab.plot(T, yfit)
 pylab.show()
if rs>=0.95:
for index, rt in rtest.iterrows() :
tdate = rt['date']
prev_max = 0
if(tdate<=to_date):
ca = list(train[(train['date']==tdate)&(train['state']==st)&(train['country']==co)]['cases'].values)[0]
prev_max = max(prev_max, ca)
 else:
ca = logistic([date_day_diff(tdate, start_date)], *popt)
 ca = max(prev_max, ca[0])
 prev_max = ca
predictions_cases.append(( rt['id'], int(ca)))
 predicted = True
except:
pass
if not predicted:
t = rdata['date'].values
start_date = t[0]
t = np.array([float(date_day_diff(d, start_date)) for d in t])
 y = rdata['cases'].values
linreg = LinearRegression()
linreg.fit(t[-10:].reshape(-1, 1), y[-10:])
 m = linreg.coef_[0]
b = linreg.intercept_
if plot:
print(st, co)
 print(linreg.intercept_, linreg.coef_)
 T = np.arange(0, 90, 1 ).tolist()
y_pred = [m*x+b for x in T]
pylab.plot(t, y, 'o')
 pylab.plot(T, y_pred)
 pylab.show()
for index, rt in rtest.iterrows() :
tdate = rt['date']
prev_max = 0
if(tdate<=to_date):
ca = list(train[(train['date']==tdate)&(train['state']==st)&(train['country']==co)]['cases'].values)[0]
prev_max = max(prev_max, ca)
 else:
ca = m*date_day_diff(tdate, start_date)+ b
ca = max(ca, prev_max)
 prev_max = ca
predictions_cases.append(( rt['id'], int(ca)) )<prepare_output>
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
data = {
'ForecastId': [pred[0] for pred in predictions_cases],
'ConfirmedCases': [pred[1] for pred in predictions_cases],
}
df_cases = pd.DataFrame(data, columns = data.keys())
 df_cases.head()<filter>
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']
predictions_deaths = []
plot = True
for index, region in regions.iterrows() :
predicted = False
st = region['state']
co = region['country']
rdata = train[(train['state']==st)&(train['country']==co)]
rtest = test[(test['state']==st)&(test['country']==co)]
window = rdata[rdata['deaths']>=100]['date']
if(window.count() < 10):
window = rdata[rdata['deaths']>=10]['date']
if(window.count() >= 10):
start_date = window.min()
rdata = rdata[rdata['date']>=start_date]
t = rdata['date'].values
t = [float(date_day_diff(d, start_date)) for d in t]
y = rdata['deaths'].values
try:
bounds =([-1e6, -1e6, 0.001, 0.0], [1e6, 1e6, 0.999, t[-1]+16])
 popt, pcov = curve_fit(logistic, t, y, bounds = bounds)
 residuals = y - logistic(t, *popt)
 ss_res = np.sum(residuals**2)
 ss_tot = np.sum(( y - np.mean(y)) **2)
 rs = 1 -(ss_res / ss_tot)
 if plot:
print(st, co)
 print(popt)
 print('R squared: ', rs)
 T = np.arange(0, 60, 1 ).tolist()
yfit = logistic(T, *popt)
 pylab.plot(t, y, 'o')
 pylab.plot(T, yfit)
 pylab.show()
if rs>=0.95:
for index, rt in rtest.iterrows() :
tdate = rt['date']
prev_max = 0
if(tdate<=to_date):
ca = list(train[(train['date']==tdate)&(train['state']==st)&(train['country']==co)]['deaths'].values)[0]
prev_max = max(prev_max, ca)
 else:
ca = logistic([date_day_diff(tdate, start_date)], *popt)
 ca = max(prev_max, ca[0])
 prev_max = ca
predictions_deaths.append(( rt['id'], int(ca)))
 predicted = True
except:
pass
if not predicted:
t = rdata['date'].values
start_date = t[0]
t = np.array([float(date_day_diff(d, start_date)) for d in t])
 y = rdata['deaths'].values
linreg = LinearRegression()
linreg.fit(t[-10:].reshape(-1, 1), y[-10:])
 m = linreg.coef_[0]
b = linreg.intercept_
if plot:
print(st, co)
 print(linreg.intercept_, linreg.coef_)
 T = np.arange(0, 90, 1 ).tolist()
y_pred = [m*x+b for x in T]
pylab.plot(t, y, 'o')
 pylab.plot(T, y_pred)
 pylab.show()
for index, rt in rtest.iterrows() :
tdate = rt['date']
prev_max = 0
if(tdate<=to_date):
ca = list(train[(train['date']==tdate)&(train['state']==st)&(train['country']==co)]['deaths'].values)[0]
prev_max = max(prev_max, ca)
 else:
ca = m*date_day_diff(tdate, start_date)+ b
ca = max(ca, prev_max)
 prev_max = ca
predictions_deaths.append(( rt['id'], int(ca)) )<prepare_output>
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
data = {
'ForecastId': [pred[0] for pred in predictions_deaths],
'Fatalities': [pred[1] for pred in predictions_deaths],
}
df_deaths = pd.DataFrame(data, columns = data.keys())
 df_deaths.head()<save_to_csv>
if YEAR == 2020:
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 elif YEAR == 2021:
test_df = pd.read_csv(DATA_DIR + f'MSampleSubmissionStage{STAGE}.csv' )
df_submission = df_cases.join(df_deaths.set_index('ForecastId'), on = 'ForecastId')
 df_submission.to_csv('submission.csv', index=False)
 df_submission.head()<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
warnings.filterwarnings('ignore')
 train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 PassengerId=test['PassengerId']
all_data = pd.concat([train, test], ignore_index = True )<count_values>
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
train['Survived'].value_counts()<predict_on_test>
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
age_df = all_data[['Age', 'Pclass','Sex','Title']]
age_df=pd.get_dummies(age_df)
 known_age = age_df[age_df.Age.notnull() ].iloc[:,:].values
unknown_age = age_df[age_df.Age.isnull() ].iloc[:,:].values
y = known_age[:, 0]
X = known_age[:, 1:]
rfr = RandomForestRegressor(random_state=0, n_estimators=100, n_jobs=-1)
 rfr.fit(X, y)
 predictedAges = rfr.predict(unknown_age[:, 1::])
 all_data.loc[(all_data.Age.isnull()), 'Age' ] = predictedAges<filter>
X = tourney_result.drop('result', axis=1)
 y = tourney_result.result
all_data[all_data['Embarked'].isnull() ]<feature_engineering>
params_lgb = {'num_leaves': 127,
'min_data_in_leaf': 10,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.01,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'logloss',
"verbosity": 0
}
params_xgb = {'colsample_bytree': 0.8,
'learning_rate': 0.0003,
'max_depth': 31,
'subsample': 1,
'objective':'binary:logistic',
'eval_metric':'logloss',
'min_child_weight':3,
'gamma':0.25,
'n_estimators':5000,
'verbosity':0
}
all_data['Embarked'] = all_data['Embarked'].fillna('C' )<filter>
NFOLDS = 5
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_lgb = np.zeros(test_df.shape[0])
 y_oof_lgb = np.zeros(X.shape[0])
 for fold_n,(train_index, valid_index)in enumerate(splits):
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label=y_train)
 dvalid = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 y_pred_valid = clf.predict(X_valid)
 y_oof_lgb[valid_index] = y_pred_valid
y_preds_lgb += clf.predict(test_df)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()
all_data[all_data['Fare'].isnull() ]<feature_engineering>
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_xgb = np.zeros(test_df.shape[0])
 y_oof_xgb = np.zeros(X.shape[0])
 for fold_n,(train_index, valid_index)in enumerate(splits):
X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_set = xgb.DMatrix(X_train, y_train)
 val_set = xgb.DMatrix(X_valid, y_valid)
 test_set = xgb.DMatrix(test_df)
 clf = xgb.train(params_xgb, train_set,num_boost_round=5000, evals=[(train_set, 'train'),(val_set, 'val')], early_stopping_rounds=100, verbose_eval=100)
 y_preds_xgb += clf.predict(test_set)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()
fare=all_data[(all_data['Embarked'] == "S")&(all_data['Pclass'] == 3)].Fare.median()
all_data['Fare']=all_data['Fare'].fillna(fare )<feature_engineering>
if YEAR == 2020:
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 elif YEAR == 2021:
submission_df = pd.read_csv(DATA_DIR + f'MSampleSubmissionStage{STAGE}.csv')
 submission_df['Pred'] = 0.9*y_preds_lgb + 0.1*y_preds_xgb
submission_df
<create_dataframe><EOS> 
submission_df.to_csv('submission.csv', index=False )