Kaggle competition: Hackathon Sentimento_v2
<SOS> metric: categorizationaccuracy Kaggle data source: hackathon-sentimento_v2<merge>
train_with_4 = pd.read_csv('.. /input/train.csv')
 train_with_4['target'] = train_with_4['target']/4
train_with_4.to_csv('train.csv', index = False )
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<categorify>
train_with_1 = pd.read_csv('train.csv')
 train_with_1.head()
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<load_from_csv>
def generate_bigrams(x):
n_grams = set(zip(*[x[i:] for i in range(2)]))
for n_gram in n_grams:
x.append(' '.join(n_gram))
return x
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv' )<concatenate>
generate_bigrams(['This', 'film', 'is', 'terrible'] )
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<groupby>
SEED = 1234
torch.manual_seed(SEED)
 torch.backends.cudnn.deterministic = True
TEXT = data.Field(preprocessing = generate_bigrams)
 TARGET = data.LabelField(dtype = torch.float )
season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()
season_score<merge>
fields_train = [(None, None),(None, None),(None, None),('text', TEXT),('target', TARGET)]
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<drop_column>
train_data = data.TabularDataset(path = 'train.csv',
format = 'csv',
fields = fields_train,
skip_header = True
)
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>
train_data, valid_data = train_data.split(random_state = random.seed(SEED))
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>
MAX_VOCAB_SIZE = 25_000
TEXT.build_vocab(train_data,
max_size = MAX_VOCAB_SIZE,
vectors = "glove.6B.100d",
unk_init = torch.Tensor.normal_)
 TARGET.build_vocab(train_data )
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>
BATCH_SIZE = 64
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 train_iterator = data.Iterator(dataset = train_data, batch_size = BATCH_SIZE,device = device, shuffle = None, train = True, sort_key = lambda x: len(x.text), sort_within_batch = False)
 valid_iterator = data.Iterator(dataset = valid_data, batch_size = BATCH_SIZE,device = device, shuffle = None, train = False, sort_key = lambda x: len(x.text), sort_within_batch = False )
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<load_from_csv>
class FastText(nn.Module):
def __init__(self, vocab_size, embedding_dim, output_dim, pad_idx):
super().__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim, padding_idx=pad_idx)
 self.fc = nn.Linear(embedding_dim, output_dim)
 def forward(self, text):
embedded = self.embedding(text)
 embedded = embedded.permute(1, 0, 2)
 pooled = F.avg_pool2d(embedded,(embedded.shape[1], 1)).squeeze(1)
 return self.fc(pooled )
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv' )<feature_engineering>
INPUT_DIM = len(TEXT.vocab)
 EMBEDDING_DIM = 100
OUTPUT_DIM = 1
PAD_IDX = TEXT.vocab.stoi[TEXT.pad_token]
model = FastText(INPUT_DIM, EMBEDDING_DIM, OUTPUT_DIM, PAD_IDX )
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>
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
 print(f'The model has {count_parameters(model):,} trainable parameters' )
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>
pretrained_embeddings = TEXT.vocab.vectors
model.embedding.weight.data.copy_(pretrained_embeddings )
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<set_options>
UNK_IDX = TEXT.vocab.stoi[TEXT.unk_token]
model.embedding.weight.data[UNK_IDX] = torch.zeros(EMBEDDING_DIM)
 model.embedding.weight.data[PAD_IDX] = torch.zeros(EMBEDDING_DIM )
h2o.init(
 nthreads=-1,
max_mem_size = "8G" )<create_dataframe>
optimizer = optim.Adam(model.parameters() )
train_1=h2o.H2OFrame(tourney_result)
 train_1['result']=train_1['result'].asfactor()<train_model>
criterion = nn.BCEWithLogitsLoss()
model = model.to(device)
 criterion = criterion.to(device )
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 )<train_model>
def binary_accuracy(preds, y):

 rounded_preds = torch.round(torch.sigmoid(preds))
correct =(rounded_preds == y ).float()
acc = correct.sum() / len(correct)
 return acc
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 )<train_model>
def train(model, iterator, optimizer, criterion):
epoch_loss = 0
epoch_acc = 0
model.train()
for batch in iterator:
optimizer.zero_grad()
predictions = model(batch.text ).squeeze(1)
 loss = criterion(predictions, batch.target)
 acc = binary_accuracy(predictions, batch.target)
 loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator )
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)
<create_dataframe>
def evaluate(model, iterator, criterion):
epoch_loss = 0
epoch_acc = 0
model.eval()
with torch.no_grad() :
for batch in iterator:
predictions = model(batch.text ).squeeze(1)
 loss = criterion(predictions, batch.target)
 acc = binary_accuracy(predictions, batch.target)
 epoch_loss += loss.item()
epoch_acc += acc.item()
return epoch_loss / len(iterator), epoch_acc / len(iterator )
test_1=h2o.H2OFrame(test_df )<predict_on_test>
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
 elapsed_secs = int(elapsed_time -(elapsed_mins * 60))
return elapsed_mins, elapsed_secs
pred1=model_xgb.predict(test_1)
 pred2=model_gbm.predict(test_1)
<create_dataframe>
N_EPOCHS = 20
best_valid_loss = float('inf')
 for epoch in range(N_EPOCHS):
start_time = time.time()
train_loss, train_acc = train(model, train_iterator, optimizer, criterion)
 valid_loss, valid_acc = evaluate(model, valid_iterator, criterion)
 end_time = time.time()
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
 if valid_loss < best_valid_loss:
best_valid_loss = valid_loss
torch.save(model.state_dict() , 'tut3-model.pt')
 print(f'Epoch: {epoch+1:02} | Epoch Time: {epoch_mins}m {epoch_secs}s')
 print(f'\tTrain Loss: {train_loss:.3f} | Train Acc: {train_acc*100:.2f}%')
 print(f'\t Val.Loss: {valid_loss:.3f} | Val.Acc: {valid_acc*100:.2f}%' )
pred_df1=pred1.as_data_frame()
pred_df2=pred2.as_data_frame() 
<save_to_csv>
def predict_sentiment(model, sentence):
model.eval()
tokenized = generate_bigrams(sentence.split())
 indexed = [TEXT.vocab.stoi[t] for t in tokenized]
tensor = torch.LongTensor(indexed ).to(device)
 tensor = tensor.unsqueeze(1)
 prediction = torch.sigmoid(model(tensor))
return prediction.item()
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 )<load_from_csv>
preds = []
test_data = pd.read_csv('.. /input/test.csv')
 for i in range(len(test_data)) :
preds.append(( int(predict_sentiment(model, test_data['text'][i])>0.5)) *4)
 ids = test_data['Id']
dict = {'Id': ids, 'target': preds}
df = pd.DataFrame(dict)
 df.to_csv('final_output.csv', index=False )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data_drop = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data['Died'] = 1 - train_data['Survived']
train_data.head()<load_from_csv>
test_data = pd.read_csv('final_output.csv')
 test_data.head()