Kaggle competition: Google Cloud & NCAA® ML Competition 2020-NCAAM
for dataset in combine_data:
dataset['Age']=dataset.groupby(['Sex','Pclass'])['Age'].apply(lambda x : x.fillna(x.median()))<feature_engineering>
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
import matplotlib.pyplot as plt
from sklearn import preprocessing
from sklearn.model_selection import RandomizedSearchCV
from sklearn.ensemble import RandomForestRegressor
train['Embarked']=train["Embarked"].fillna('C' )<feature_engineering>
Tourney_Compact_Results = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneyCompactResults.csv')
 Tourney_Seeds = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv' )
fill_fare = test[(test['Pclass']==3)&(test['Sex']==0)]['Fare'].median()
test['Fare']=test['Fare'].fillna(fill_fare )<feature_engineering>
RegularSeason_Compact_Results = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
 MSeasons = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MSeasons.csv')
 MTeams=pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MTeams.csv' )
for dataset in combine_data:
dataset['Age']=dataset['Age'].astype(int)
 for dataset in combine_data:
dataset.loc[ dataset['Age'] <= 16, 'Age'] = 0
dataset.loc[(dataset['Age'] > 16)&(dataset['Age'] <= 32), 'Age'] = 1
dataset.loc[(dataset['Age'] > 32)&(dataset['Age'] <= 48), 'Age'] = 2
dataset.loc[(dataset['Age'] > 48)&(dataset['Age'] <= 64), 'Age'] = 3
dataset.loc[ dataset['Age'] > 64, 'Age'] = 4 
<feature_engineering>
Tourney_Results_Compact=pd.merge(Tourney_Compact_Results, Tourney_Seeds, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 Tourney_Results_Compact.rename(columns={'Seed':'WinningSeed'},inplace=True)
 Tourney_Results_Compact=Tourney_Results_Compact.drop(['TeamID'],axis=1)
 Tourney_Results_Compact = pd.merge(Tourney_Results_Compact, Tourney_Seeds, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 Tourney_Results_Compact.rename(columns={'Seed':'LoosingSeed'}, inplace=True)
 Tourney_Results_Compact=Tourney_Results_Compact.drop(['TeamID','NumOT','WLoc'],axis=1)
 Tourney_Results_Compact
for dataset in combine_data:
dataset['is_alone']=0
dataset.loc[dataset['family_members']==1,'is_alone'] = 1<categorify>
Tourney_Results_Compact=Tourney_Results_Compact.drop(['WScore','LScore'],axis=1)
 Tourney_Results_Compact.head()
map_embarked = {'S':0,'C':1,'Q':2}
for dataset in combine_data:
dataset['Embarked']=dataset['Embarked'].map(map_embarked)
 train.head(2 )<drop_column>
Tourney_Results_Compact['WinningSeed'] = Tourney_Results_Compact['WinningSeed'].str.extract('(\d+)', expand=True)
 Tourney_Results_Compact['LoosingSeed'] = Tourney_Results_Compact['LoosingSeed'].str.extract('(\d+)', expand=True)
 Tourney_Results_Compact.WinningSeed = pd.to_numeric(Tourney_Results_Compact.WinningSeed, errors='coerce')
 Tourney_Results_Compact.LoosingSeed = pd.to_numeric(Tourney_Results_Compact.LoosingSeed, errors='coerce' )
train = train.drop(['PassengerId','Name','Ticket','Fare','Cabin','SibSp','Parch'],axis=1)
 test = test.drop(['PassengerId','Name','Ticket','Fare','Cabin','SibSp','Parch'],axis=1 )<split>
season_winning_team = RegularSeason_Compact_Results[['Season', 'WTeamID', 'WScore']]
season_losing_team = RegularSeason_Compact_Results[['Season', 'LTeamID', 'LScore']]
season_winning_team.rename(columns={'WTeamID':'TeamID','WScore':'Score'}, inplace=True)
 season_losing_team.rename(columns={'LTeamID':'TeamID','LScore':'Score'}, inplace=True)
 RegularSeason_Compact_Results = pd.concat(( season_winning_team, season_losing_team)).reset_index(drop=True)
 RegularSeason_Compact_Results
X=train.drop('Survived',axis=1)
 y=train['Survived']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=100 )<compute_train_metric>
RegularSeason_Compact_Results_Final = RegularSeason_Compact_Results.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()
RegularSeason_Compact_Results_Final
log_model=LogisticRegression()
log_model.fit(X_train,y_train)
 log_model_pred = log_model.predict(X_test)
 acc_log_model = round(log_model.score(X_train,y_train)*100,2)
 print('Accuracy of Logestic Model:',acc_log_model)
 print('--*'*10)
 print(classification_report(y_test,log_model_pred))
print('--*'*10)
 print(confusion_matrix(y_test,log_model_pred))<choose_model_class>
Tourney_Results_Compact = pd.merge(Tourney_Results_Compact, RegularSeason_Compact_Results_Final, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 Tourney_Results_Compact.rename(columns={'Score':'WScoreTotal'}, inplace=True)
 Tourney_Results_Compact
tree_model=DecisionTreeClassifier()
tree_model.fit(X_train,y_train)
 tree_model_pred = tree_model.predict(X_test)
 acc_tree_model = round(tree_model.score(X_train,y_train)*100,2)
 print('Accuracy of tree Model:',acc_tree_model)
 print('--*'*10)
 print(classification_report(y_test,tree_model_pred))
print('--*'*10)
 print(confusion_matrix(y_test,tree_model_pred))<compute_train_metric>
Tourney_Results_Compact = Tourney_Results_Compact.drop('TeamID', axis=1)
 Tourney_Results_Compact = pd.merge(Tourney_Results_Compact, RegularSeason_Compact_Results_Final, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 Tourney_Results_Compact.rename(columns={'Score':'LScoreTotal'}, inplace=True)
 Tourney_Results_Compact = Tourney_Results_Compact.drop('TeamID', axis=1)
 Tourney_Results_Compact.to_csv('Tourney_Win_Results_Train.csv', index=False)
 Tourney_Results_Compact=Tourney_Results_Compact[Tourney_Results_Compact['Season'] < 2015]
Tourney_Results_Compact
rf_model=RandomForestClassifier()
rf_model.fit(X_train,y_train)
 rf_model_pred = rf_model.predict(X_test)
 acc_rf_model = round(rf_model.score(X_train,y_train)*100,2)
 print('Accuracy of Random Foresrt Model:',acc_rf_model)
 print('--*'*10)
 print(classification_report(y_test,rf_model_pred))
print('--*'*10)
 print(confusion_matrix(y_test,rf_model_pred))<choose_model_class>
Tourney_Win_Results=Tourney_Results_Compact.drop(['Season','WTeamID','LTeamID','DayNum'],axis=1)
 Tourney_Win_Results
final_log = LogisticRegression()
final_log.fit(X,y)
 final_log_pred = final_log.predict(test)
 final_tree_model=DecisionTreeClassifier()
final_tree_model.fit(X,y)
 final_tree_model_pred = final_tree_model.predict(test)
 final_rf=RandomForestClassifier()
final_rf.fit(X,y)
 final_rf_pred = final_rf.predict(test)
<load_from_csv>
Tourney_Win_Results.rename(columns={'WinningSeed':'Seed1', 'LoosingSeed':'Seed2', 'WScoreTotal':'ScoreT1', 'LScoreTotal':'ScoreT2'}, inplace=True )
result= pd.read_csv('.. /input/titanic/gender_submission.csv')
 result=result.set_index('PassengerId')
 result.head(3 )<save_to_csv>
tourney_lose_result = Tourney_Win_Results.copy()
tourney_lose_result['Seed1'] = Tourney_Win_Results['Seed2']
tourney_lose_result['Seed2'] = Tourney_Win_Results['Seed1']
tourney_lose_result['ScoreT1'] = Tourney_Win_Results['ScoreT2']
tourney_lose_result['ScoreT2'] = Tourney_Win_Results['ScoreT1']
tourney_lose_result
result['Survived']= final_log_pred
result.to_csv('output_log.csv')
 result['Survived']= final_tree_model_pred
result.to_csv('output_tree.csv')
 result['Survived']= final_rf_pred
result.to_csv('output_rf.csv')
<load_from_csv>
Tourney_Win_Results['Seed_diff'] = Tourney_Win_Results['Seed1'] - Tourney_Win_Results['Seed2']
Tourney_Win_Results['ScoreT_diff'] = Tourney_Win_Results['ScoreT1'] - Tourney_Win_Results['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']
train_data = pd.read_csv("/kaggle/input/titanic/train.csv", index_col='PassengerId')
 train_data.head()<load_from_csv>
Tourney_Win_Results['result'] = 1
tourney_lose_result['result'] = 0
tourney_result_Final = pd.concat(( Tourney_Win_Results, tourney_lose_result)).reset_index(drop=True)
 tourney_result_Final.to_csv('Tourneyvalidate.csv', index=False )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv", index_col='PassengerId')
 test_data.head()<count_missing_values>
tourney_result_Final1 = tourney_result_Final[[
'Seed1', 'Seed2', 'ScoreT1', 'ScoreT2', 'Seed_diff', 'ScoreT_diff', 'result']]
cols_with_missing = [col for col in train_data.columns if train_data[col].isnull().any() ]
cols_with_missing<define_variables>
tourney_result_Final1.loc[lambda x:(x['Seed1'].isin([14,15,16])) &(x['Seed2'].isin([1,2,3])) ,'result'
] = 0
cat =(train_data.dtypes == 'object')
 object_cols = list(cat[cat].index)
 object_cols<count_missing_values>
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )
test_cols_with_missing = [col for col in test_data.columns if test_data[col].isnull().any() ]
test_cols_with_missing<filter>
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
test_cat =(test_data.dtypes == 'object')
 test_object_cols = list(test_cat[test_cat].index)
 test_object_cols<count_unique_values>
test_df = pd.merge(test_df, Tourney_Seeds, 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_Seeds, 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 )
object_nunique = list(map(lambda col: train_data[col].nunique() , object_cols))
d = dict(zip(object_cols, object_nunique))
object_nunique_test = list(map(lambda col: test_data[col].nunique() , test_object_cols))
d_test = dict(zip(test_object_cols, object_nunique_test))
sorted(d.items() , key=lambda x: x[1] )<sort_values>
test_df = pd.merge(test_df, RegularSeason_Compact_Results_Final, 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, RegularSeason_Compact_Results_Final, 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_df.to_csv('test_df_Test.csv', index=False )
sorted(d.items() , key=lambda x: x[1] )<categorify>
test_df['Seed1'] = test_df['Seed1'].str.extract('(\d+)', expand=True)
 test_df['Seed2'] = test_df['Seed2'].str.extract('(\d+)', expand=True)
 test_df.Seed1 = pd.to_numeric(test_df.Seed1, errors='coerce')
 test_df.Seed2 = pd.to_numeric(test_df.Seed2, errors='coerce' )
low_cardinality_cols = [col for col in object_cols if train_data[col].nunique() < 10]
high_cardinality_cols = list(set(object_cols)-set(low_cardinality_cols))
print('Categorical columns that will be one-hot encoded:', low_cardinality_cols)
 print('
Categorical columns that will be dropped from the dataset:', high_cardinality_cols )<define_variables>
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
object_cols = [col for col in train_data.columns if train_data[col].dtype == "object"]
good_label_cols = [col for col in object_cols if
set(train_data[col])== set(test_data[col])]
bad_label_cols = list(set(object_cols)-set(good_label_cols))
print('Categorical columns that will be label encoded:', good_label_cols)
 print('
Categorical columns that will be dropped from the dataset:', bad_label_cols )<split>
X = tourney_result_Final1.drop('result', axis=1)
 y = tourney_result_Final1.result
X = train_data
X_test = test_data
X.dropna(axis=0, subset=['Survived'], inplace=True)
 y = X.Survived
X.drop(['Survived'], axis=1, inplace=True)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y,
train_size=0.8, test_size=0.2,
random_state=0)
 cols_with_missing = [col for col in X.columns
if X[col].isnull().any() ]
categorical_cols = [cname for cname in X_train.columns if
X_train[cname].nunique() < 10 and
X_train[cname].dtype == "object"]
numerical_cols = [cname for cname in X_train.columns if
X_train[cname].dtype in ['int64', 'float64']] 
<categorify>
df = pd.concat([X, test_df], axis=0, sort=False ).reset_index(drop=True)
 df_log = pd.DataFrame(
 preprocessing.MinMaxScaler().fit_transform(df),
columns=df.columns,
index=df.index 
)
 train_log, test_log = df_log.iloc[:len(X),:], df_log.iloc[len(X):,:].reset_index(drop=True )
numerical_transformer = SimpleImputer(strategy='mean')
 categorical_transformer = Pipeline(steps=[ 
('imputer', SimpleImputer(strategy='most_frequent')) , 
('onehot', OneHotEncoder(handle_unknown='ignore'))
])
 preprocessor = ColumnTransformer(
 transformers=[ 
('num', numerical_transformer, numerical_cols), 
('cat', categorical_transformer, categorical_cols)
 ] )<choose_model_class>
logreg = LogisticRegression()
logreg.fit(train_log, y)
 coeff_logreg = pd.DataFrame(train_log.columns.delete(0))
coeff_logreg.columns = ['feature']
coeff_logreg["score_logreg"] = pd.Series(logreg.coef_[0])
 coeff_logreg.sort_values(by='score_logreg', ascending=False )
model = RandomForestClassifier(n_estimators=100, criterion='mae', max_leaf_nodes=100, random_state=0 )<compute_train_metric>
y_logreg_train = logreg.predict(train_log)
 y_logreg_pred = logreg.predict_proba(test_log )
def get_score(n_estimators, max_leaf_nodes):
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor), 
('model', RandomForestClassifier(n_estimators=n_estimators,max_leaf_nodes=max_leaf_nodes, random_state=0))
])
 scores = -1 * cross_val_score(my_pipeline, X, y,
cv=3,
scoring='neg_mean_absolute_error')
 return scores.mean()<define_variables>
clf = RandomForestClassifier(n_estimators=200,max_depth=90,min_samples_leaf=300,min_samples_split=200,max_features=5)
 clf.fit(train_log, y)
 clf_probs = clf.predict_proba(test_log )
k=100
results = {}
for i in range(1,9):
for k in range(1,8):
results[50*i, 50*k] = get_score(50*i,50*k)
 results<set_options>
y_pred_df_random = pd.DataFrame(clf_probs)
 y_pred_1 = y_pred_df_random.iloc[:,[1]]
y_pred_df_random
%matplotlib inline
plt.plot(list(results.keys()),list(results.values()))
plt.show()<find_best_model_class>
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
my_pipeline = Pipeline(steps=[('preprocessor', preprocessor), 
('model', RandomForestClassifier(n_estimators=400,max_leaf_nodes=50,random_state=0))
])
 scores = -1 * cross_val_score(my_pipeline, X, y,
cv=3,
scoring='neg_mean_absolute_error')
 my_pipeline.fit(X, y)
 preds = my_pipeline.predict(X_test)
<save_to_csv>
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = y_pred_1
submission_df
output = pd.DataFrame({'PassengerId': X_test.index,
'Survived': preds})
 output.to_csv('submission_final_5.csv', index=False )<train_model>
submission_df.to_csv('submission_New6.csv', index=False )