Kaggle competition: IESB - 2019
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<prepare_output>
df = pd.read_csv(".. /input/train.csv")
 dfTeste = pd.read_csv(".. /input/test.csv" )
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<feature_engineering>
y = 'nota_mat'
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']<concatenate>
train, test = train_test_split(df, random_state=42)
 train.shape, test.shape
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<prepare_x_and_y>
df['codigo_mun'] = df['codigo_mun'].str.replace('ID_ID_','')
 df['comissionados_por_servidor'] = df['comissionados_por_servidor'].str.replace('%','')
 df['area']=df['area'].str.replace(',','')
 df['ranking_igm']=df['ranking_igm'].str.replace('º','')
 df['densidade_dem']=df['densidade_dem'].str.replace(',','')
 dfTeste['codigo_mun'] = dfTeste['codigo_mun'].str.replace('ID_ID_','')
 dfTeste['comissionados_por_servidor'] = dfTeste['comissionados_por_servidor'].str.replace('%','')
 dfTeste['area']=dfTeste['area'].str.replace(',','')
 dfTeste['ranking_igm']=dfTeste['ranking_igm'].str.replace('º','')
 dfTeste['densidade_dem']=dfTeste['densidade_dem'].str.replace(',','')
X_train=tourney_result.drop('result',axis=1)
 y_train=tourney_result.result<import_modules>
df['codigo_mun']=df['codigo_mun'].values.astype('int64')
 df['area']=df['area'].values.astype('float64')
 df['densidade_dem']=df['densidade_dem'].values.astype('float64')
 dfTeste['codigo_mun']=dfTeste['codigo_mun'].values.astype('int64')
 dfTeste['area']=dfTeste['area'].values.astype('float64')
 dfTeste['densidade_dem']=dfTeste['densidade_dem'].values.astype('float64')
 df.head()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical, normalize
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score<choose_model_class>
for c in['regiao', 'estado', 'porte']:
df[c] = df[c].astype('category' ).cat.codes
for c in['regiao', 'estado', 'porte']:
dfTeste[c] = dfTeste[c].astype('category' ).cat.codes
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128, input_shape=(6,), activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(125, activation='relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1, activation='sigmoid')
 ] )<choose_model_class>
for c in['densidade_dem', 'perc_pop_econ_ativa', 'exp_vida','exp_anos_estudo','gasto_pc_saude','hab_p_medico','gasto_pc_educacao','exp_anos_estudo','idhm']:
df[c] = df[c].fillna(( df[c].mean()))
for c in['densidade_dem', 'perc_pop_econ_ativa', 'exp_vida','exp_anos_estudo','gasto_pc_saude','hab_p_medico','gasto_pc_educacao','exp_anos_estudo','idhm']:
dfTeste[c] = dfTeste[c].fillna(( dfTeste[c].mean()))
model.compile(loss='binary_crossentropy',
optimizer=Adam(0.0001),
metrics=['acc'] )<train_model>
feats = ['exp_anos_estudo']
history = model.fit(X_train, y_train,epochs=150,verbose=1 )<load_from_csv>
train, test = train_test_split(df, random_state=42)
 train.shape, test.shape
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )<feature_engineering>
rf = RandomForestRegressor(random_state=42, n_jobs=-1,n_estimators=30000,min_samples_leaf=650)
 y='nota_mat'
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<merge>
rf.fit(df[feats], df[y] )
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<feature_engineering>
from sklearn.metrics import mean_squared_error
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<import_modules>
valid_preds = rf.predict(test[feats])
 mean_squared_error(test[y], valid_preds)**(1/2)
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<init_hyperparams>
dfTeste[y]=rf.predict(dfTeste[feats])
params_lgb = {'num_leaves': 400,
'min_child_weight': 0.034,
'feature_fraction': 0.379,
'bagging_fraction': 0.418,
'min_data_in_leaf': 106,
'objective': 'binary',
'max_depth': 50,
'learning_rate': 0.0068,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'logloss',
"verbosity": -1,
'reg_alpha': 0.3899,
'reg_lambda': 0.648,
'random_state': 47,
}
params_xgb = {'colsample_bytree': 0.8,
'learning_rate': 0.0004,
'max_depth': 31,
'subsample': 1,
'objective':'binary:logistic',
'eval_metric':'logloss',
'min_child_weight':3,
'gamma':0.25,
'n_estimators':5000
}<split>
dfTeste[['codigo_mun', y]].to_csv('rf3.csv', index=False )