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>
import os
from pathlib import Path
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import cross_validate, GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
train = pd.read_csv("/kaggle/input/titanic/train.csv", index_col=0)
 test = pd.read_csv("/kaggle/input/titanic/test.csv", index_col=0 )<prepare_x_and_y>
FILEDIR = Path('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament' )
Y = train["Survived"].values<drop_column>
sub = pd.read_csv(FILEDIR / 'MSampleSubmissionStage1_2020.csv', usecols=['ID'])
 id_splited = sub['ID'].str.split('_', expand=True ).astype(int ).rename(columns={0: 'Season', 1: 'Team1', 2: 'Team2'})
 sub = pd.concat([sub, id_splited], axis=1 ).set_index(['Season', 'Team1', 'Team2'] ).sort_index()
train = train.drop(columns=["Name", "Ticket", "Cabin"] )<categorify>
tourney_teams = {}
tourney_teams_all = set()
for season in sub.index.get_level_values('Season' ).drop_duplicates() :
tourney_teams[season] = set()
tourney_teams[season].update(sub.loc[season].index.get_level_values('Team1'))
tourney_teams[season].update(sub.loc[season].index.get_level_values('Team2'))
tourney_teams_all.update(tourney_teams[season])
 {k: len(v)for k, v in tourney_teams.items() }
one_hot_sex = OneHotEncoder(sparse=False)
 sex_encoded = one_hot_sex.fit_transform(train["Sex"].values.reshape(-1, 1))
X_categorical = sex_encoded<categorify>
conferences = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeamConferences.csv')
 conferences = pd.concat(
 [conferences.query('Season == @season and TeamID in @teams')for season, teams in tourney_teams.items() ])
 conferences = conferences.set_index(['Season', 'TeamID'] ).sort_index()
train["Pclass"].describe()
train["Pclass"].hist()
train["Pclass"].isnull().sum()
one_hot_pclass = OneHotEncoder(sparse=False)
 pclass_encoded = one_hot_pclass.fit_transform(train["Pclass"].values.reshape(-1, 1))
X_categorical = np.hstack(( X_categorical, pclass_encoded))<categorify>
coaches = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeamCoaches.csv')
 coaches = pd.concat(
 [coaches.query('Season == @season and TeamID in @team')for season, team in tourney_teams.items() ])
 coaches = coaches[coaches['LastDayNum'] == 154].set_index(['Season', 'TeamID'] ).sort_index() [['CoachName']]
train["Embarked"].isnull().sum()
train["Embarked"].loc[train["Embarked"].isnull() ] = train["Embarked"].value_counts().idxmax()
train["Embarked"].isnull().sum()
train["Embarked"].hist()
one_hot_embarked = OneHotEncoder(sparse=False)
 embarked_encoded = one_hot_embarked.fit_transform(train["Embarked"].values.reshape(-1, 1))
X_categorical = np.hstack(( X_categorical, embarked_encoded))<normalization>
teams = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MTeams.csv', usecols=['TeamID', 'FirstD1Season'])
 teams['FirstD1Season'] = 2020 - teams['FirstD1Season']
teams = pd.concat(
 [teams.query('TeamID in @team' ).assign(Season=season)for season, team in tourney_teams.items() ])
 teams = teams.set_index(['Season', 'TeamID'] ).sort_index()
scaler = StandardScaler()
X_numerical = scaler.fit_transform(X_numerical)
 X = np.hstack(( X_numerical, X_categorical))<train_model>
seeds = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MNCAATourneySeeds.csv')
 seeds = pd.concat(
 [seeds.query('Season == @season and TeamID in @teams')for season, teams in tourney_teams.items() ])
 seeds = seeds.set_index(['Season', 'TeamID'] ).sort_index()
seeds['Region'] = seeds['Seed'].str[0]
seeds['Number'] = seeds['Seed'].str[1:3].astype(int)
 del seeds['Seed']
kf = KFold(20, shuffle=True, random_state=41)
 classifier = GradientBoostingClassifier()
classifier_accuracy = []
for train_index, test_index in kf.split(train):
train_X, train_Y = X[train_index], Y[train_index]
test_X, test_Y = X[test_index], Y[test_index]
classifier.fit(train_X, train_Y)
 predictions = classifier.predict(test_X)
 accuracy = accuracy_score(test_Y, predictions)
 classifier_accuracy.append(accuracy)
 print(sum(classifier_accuracy)/len(classifier_accuracy))<categorify>
regular = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MRegularSeasonDetailedResults.csv')
 regular = regular.drop(columns=['DayNum', 'LTeamID'])
 regular = pd.concat(
 [regular.query('Season == @season and WTeamID in @teams')for season, teams in tourney_teams.items() ])
 regular = regular.groupby(['Season', 'WTeamID'] ).sum()
regular = regular.rename_axis(index=['Season', 'TeamID'] )
test_sex_encoded = one_hot_sex.transform(test["Sex"].values.reshape(-1, 1))
test["Age"].loc[test["Age"].isnull() ] = mean_age
test_pclass_encoded = one_hot_pclass.transform(test["Pclass"].values.reshape(-1, 1))
test["Embarked"].loc[test["Embarked"].isnull() ] = train["Embarked"].value_counts().idxmax()
test_embarked_encoded = one_hot_embarked.transform(test["Embarked"].values.reshape(-1, 1))
test["Fare"].loc[test["Fare"].isnull() ] = train["Fare"].mean()
test_X_categorical = np.hstack(( test_sex_encoded, test_pclass_encoded, test_embarked_encoded))
test_X_numerical = np.hstack(( test["Age"].values.reshape(-1, 1), test["SibSp"].values.reshape(-1, 1), test["Parch"].values.reshape(-1, 1), test["Fare"].values.reshape(-1, 1)))
 test_X_numerical = scaler.transform(test_X_numerical)
 test_X = np.hstack(( test_X_numerical, test_X_categorical))<predict_on_test>
ctcsr = pd.concat([coaches, teams, conferences, seeds, regular], axis=1 )
classifier.fit(train_X, train_Y)
 predictions = classifier.predict(test_X )<save_to_csv>
result = pd.read_csv(FILEDIR / 'MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 result = result[result['Season'] >= 2015].set_index(['Season', 'WTeamID', 'LTeamID'] )
data = []
for(passenger_id, passenger_data), prediction in zip(test.iterrows() , predictions):
data.append([passenger_id, prediction])
 series = pd.DataFrame(data, columns=["PassengerID", "Survived"] ).set_index("PassengerID")
 series.to_csv("predictions.csv" )<define_variables>
merged_teams = pd.concat(
 [ctcsr.loc[[(season, wteam),(season, lteam)], :] for season, wteam, lteam, in result.index])
 team1 = merged_teams.iloc[::2, :].reset_index('TeamID')
 team2 = merged_teams.iloc[1::2, :].reset_index('TeamID')
 merged_teams = pd.concat([
pd.concat([team1.add_suffix('1'), team2.add_suffix('2')], axis=1 ).assign(Res=1),
pd.concat([team2.add_suffix('1'), team1.add_suffix('2')], axis=1 ).assign(Res=0),
] ).reset_index().set_index(['Season', 'TeamID1', 'TeamID2'] ).sort_index()
inputFolderPath = '/kaggle/input/data-series-summarization-project-v3/'
outputFolderPath = '/kaggle/working/'
filename = 'synthetic_size50k_len256_znorm.bin'
inputFilePath = inputFolderPath + filename<categorify>
x_columns = merged_teams.columns[merged_teams.columns != 'Res']
X = merged_teams[x_columns]
for column in X.select_dtypes(include='number'):
X[column] = MinMaxScaler().fit_transform(X[column].to_numpy().reshape(-1,1))
X = pd.get_dummies(X, columns=x_columns[X.dtypes == 'object'] )
def sum32(inputFilePath):

 summary_filepath = outputFolderPath + filename + '_sum32'
time_series50k = np.fromfile(inputFilePath, dtype=np.float32 ).reshape(-1, 256)
 summary50k = []
for time_series in time_series50k:
summary50k.append(time_series[0])
 summary50knp = np.array(summary50k,dtype=np.float32)
 summary50knp.tofile(summary_filepath)
 return summary_filepath
def rec32(summary_filepath):

 reconstructed_filepath = summary_filepath + '_rec32'
summary50k = np.fromfile(summary_filepath, dtype=np.float32)
 reconstructed50k = []
for summary in summary50k:
reconstructed50k.append([summary]*256)
 reconstructed50knp = np.array(reconstructed50k,dtype=np.float32)
 reconstructed50knp.tofile(reconstructed_filepath)
 return reconstructed_filepath
def sum64(inputFilePath):

 summary_filepath = outputFolderPath + filename + '_sum64'
time_series50k = np.fromfile(inputFilePath, dtype=np.float32 ).reshape(-1, 256)
 summary50k = []
for time_series in time_series50k:
summary50k.append(time_series[0])
 summary50k.append(time_series[0])
 summary50knp = np.array(summary50k,dtype=np.float32)
 summary50knp.tofile(summary_filepath)
 return summary_filepath
def rec64(summary_filepath):

 reconstructed_filepath = summary_filepath + '_rec64'
summary50k = np.fromfile(summary_filepath, dtype=np.float32 ).reshape(-1, 2)
 reconstructed50k = []
for summary in summary50k:
reconstructed50k.append([summary[0]]*256)
 reconstructed50knp = np.array(reconstructed50k,dtype=np.float32)
 reconstructed50knp.tofile(reconstructed_filepath)
 return reconstructed_filepath
def sum128(inputFilePath):

 summary_filepath = outputFolderPath + filename + '_sum128'
time_series50k = np.fromfile(inputFilePath, dtype=np.float32 ).reshape(-1, 256)
 summary50k = []
for time_series in time_series50k:
summary50k.append(time_series[0])
 summary50k.append(time_series[0])
 summary50k.append(time_series[0])
 summary50k.append(time_series[0])
 summary50knp = np.array(summary50k,dtype=np.float32)
 summary50knp.tofile(summary_filepath)
 return summary_filepath
def rec128(summary_filepath):

 reconstructed_filepath = summary_filepath + '_rec128'
summary50k = np.fromfile(summary_filepath, dtype=np.float32 ).reshape(-1, 4)
 reconstructed50k = []
for summary in summary50k:
reconstructed50k.append([summary[0]]*256)
 reconstructed50knp = np.array(reconstructed50k,dtype=np.float32)
 reconstructed50knp.tofile(reconstructed_filepath)
 return reconstructed_filepath 
<save_to_csv>
y = merged_teams['Res']
s32= sum32(inputFilePath)
 r32 = rec32(s32)
 pred32=np.fromfile(r32, dtype=np.float32)
 s64= sum64(inputFilePath)
 r64 = rec64(s64)
 pred64=np.fromfile(r64, dtype=np.float32)
 s128= sum128(inputFilePath)
 r128 = rec128(s128)
 pred128=np.fromfile(r128, dtype=np.float32)
 output = []
globalCsvIndex = 0
for i in range(len(pred32)) :
output.append([globalCsvIndex,pred32[i]])
 globalCsvIndex = globalCsvIndex+1
for i in range(len(pred64)) :
output.append([globalCsvIndex,pred64[i]])
 globalCsvIndex = globalCsvIndex+1
for i in range(len(pred128)) :
output.append([globalCsvIndex,pred128[i]])
 globalCsvIndex = globalCsvIndex+1
with open('submission.csv', 'w', newline='')as file:
writer = csv.writer(file)
 writer.writerow(['id','expected'])
 writer.writerows(output )<set_options>
clfs = {}
clfs['SVC'] = {
'instance': SVC(probability=True),
'params': [
{'kernel': ['linear'], 'C': [0.01, 0.05, 0.1, 0.5, 1]},
{'kernel': ['rbf'], 'C': [1, 10, 50, 100, 250], 'gamma': [0.1, 0.2, 0.3]}
]
}
clfs['RandomForestClassifier'] = {
'instance': RandomForestClassifier(n_jobs=-1),
'params': {
'n_estimators': [25, 50, 100],
'criterion': ['gini', 'entropy'],
'max_depth': [10, 25, 50, None]
}
}
clfs['LogisticRegression'] = {
'instance': LogisticRegression(max_iter=500, n_jobs=-1),
'params': [
{'penalty': ['l2'], 'C': [0.1, 0.5, 1, 5, 10]},
{'penalty': ['l1'], 'solver': ['liblinear', 'saga'], 'C': [0.1, 0.5, 1, 5, 10]},
{'penalty': ['elasticnet'], 'C': [0.1, 0.5, 1, 5, 10], 'l1_ratio': [0.1, 0.3, 0.5, 0.7, 0.9]}
]
}
%matplotlib inline
sns.set_style('darkgrid')
<load_from_csv>
for clf_name, clf in clfs.items() :
print('<{}>'.format(clf_name))
print(' training...'.format(clf_name))
gs = GridSearchCV(clf['instance'], param_grid=clf['params'], cv=5, n_jobs=-1)
 gs.fit(X, y)
 clfs[clf_name]['best_estimator'] = gs.best_estimator_
print(' best_score: {:.3f}'.format(gs.best_score_))
print(' best_params: {}'.format(gs.best_params_))
train=pd.read_csv('.. /input/titanic/train.csv')
 test=pd.read_csv('.. /input/titanic/test.csv' )<concatenate>
vote = VotingClassifier(
 estimators=[(clf_name, clf['best_estimator'])for clf_name, clf in clfs.items() ],
voting='soft',
n_jobs=-1 
)
 vote.fit(X, y)
 vote.estimators_
all_data = pd.concat([train.drop(['Survived'],axis=1),test])
 combine_data = [train,test]<sort_values>
for clf_name, clf in clfs.items() :
score = accuracy_score(y, clf['best_estimator'].predict(X))
print(clf_name, score)
 print('Vote', accuracy_score(y, vote.predict(X)) )
vacant_data = all_data.isnull().sum().sort_values(ascending=False)
 total_data = all_data.isnull().count().sort_values(ascending=False)
 vacant_percent =(vacant_data/total_data)
 missing_df = pd.concat([vacant_data,vacant_percent],axis=1,keys=['Total','Percent'])
 missing_df.head()<feature_engineering>
predict_proba = pd.DataFrame(
 {clf_name: clf['best_estimator'].predict_proba(X)[:, 1] for clf_name, clf in clfs.items() },
index=X.index)
 predict_proba['Vote'] = vote.predict_proba(X)[:, 1]
_ = predict_proba.plot(kind='hist', bins=50, grid=True, alpha=0.5, figsize=(16,8))
for dataset in combine_data:
dataset['family_members']= dataset['SibSp']+dataset['Parch']+1
train.head()<feature_engineering>
columns = predict_proba.columns
for column in columns:
sub[column] = 0.5
mask = [idx for idx in sub.index if idx in X.index]
sub.loc[mask, columns] = predict_proba.loc[mask, columns]
for column in columns:
sub[['ID', column]].rename(columns={column: 'pred'} ).to_csv('predict_proba-{}.csv'.format(column), index=False )
for dataset in combine_data:
dataset['Title']=dataset['Name'].str.extract('([A-Za-z]+)\.')
 common_title = []
for title in train['Title'].unique() :
if train[train['Title'] == title]['Title'].count() > 20:
common_title.append(title)
 print(common_title)
 def id_rare(cols):
if cols in common_title:
return cols
else:
return 'Rare'
for dataset in combine_data:
dataset['Title']=dataset['Title'].apply(id_rare)
 title_mapping = {'Mrs':1,'Miss':2,'Master':3,'Mr':4,'Rare':5}
for dataset in combine_data:
dataset['Title']=dataset['Title'].map(title_mapping)
 train.head()<categorify>
predict = pd.DataFrame(
 {clf_name: clf['best_estimator'].predict(X)for clf_name, clf in clfs.items() },
index=X.index)
 predict['Vote'] = vote.predict(X)
 columns = predict.columns
for column in columns:
sub[column] = 0.5
mask = [idx for idx in sub.index if idx in X.index]
sub.loc[mask, columns] = predict.loc[mask, columns]
for column in columns:
sub[['ID', column]].rename(columns={column: 'pred'} ).to_csv('predict-{}.csv'.format(column), index=False )
map_sex = {'male':0,'female':1}
for dataset in combine_data:
dataset['Sex']=dataset['Sex'].map(map_sex )<feature_engineering>
target_name = 'predict_proba-RandomForestClassifier.csv'
new_name = 'final-submission.csv'
shutil.copy(target_name, new_name )