Kaggle competition: Tweet Sentiment Extraction
<SOS> metric: Jaccard Kaggle data source: tweet-sentiment-extraction<train_on_grid>
batch_size = 16
lr = 5e-5
epochs = 2
max_seq_len = 128
doc_stride = 64
cross_validation = True
K = 2
post_processing = True
dtrain = xgb.DMatrix(X_train, label = y)
 dtest = xgb.DMatrix(X_test)
 params = {"max_depth":2, "eta":0.1}
model = xgb.cv(params, dtrain, num_boost_round=500, early_stopping_rounds=5 )<train_model>
import numpy as np
import pandas as pd
import json
import os
model_xgb = xgb.XGBRegressor(n_estimators=1000, max_depth=2, learning_rate=0.1)
 model_xgb.fit(X_train, y )<predict_on_test>
pd_train = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')
 pd_test = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv' )
xgb_preds = np.expm1(model_xgb.predict(X_test))
lasso_preds = np.expm1(model_lasso.predict(X_test))<create_dataframe>
np_train = np.array(pd_train)
 np_test = np.array(pd_test )
predictions = pd.DataFrame({"xgb":xgb_preds, "lasso":lasso_preds})
 predictions.plot(x = "xgb", y = "lasso", kind = "scatter" )<prepare_output>
def split_data(num_examples, K):
np.random.seed(0)
 idx = np.arange(num_examples)
 np.random.shuffle(idx)
 boundary = num_examples // K
splits = [{} for _ in range(K)]
for i in range(K):
splits[i]['valid_idx'] = idx[i*boundary:(i+1)*boundary]
splits[i]['train_idx'] = np.concatenate(( idx[:i*boundary], idx[(i+1)*boundary:]))
valid = np_train[splits[i]['valid_idx']]
d = {'neutral':0, 'positive':0, 'negative':0}
for line in valid:
d[line[-1]] += 1
print(d)
 return splits
preds = 0.6*lasso_preds + 0.4*xgb_preds<save_to_csv>
splits = split_data(len(np_train), K )
solution = pd.DataFrame({"id":df_test.Id, "SalePrice":preds})
 solution.to_csv("ridge_sol.csv", index = False )<import_modules>
def convert_data(data, directory, filename):
def find_all(input_str, search_str):
l1 = []
length = len(input_str)
 index = 0
while index < length:
i = input_str.find(search_str, index)
 if i == -1:
return l1
l1.append(i)
 index = i + 1
return l1
output = {}
output['version'] = 'v1.0'
output['data'] = []
for line in data:
paragraphs = []
context = line[1]
qas = []
question = line[-1]
qid = line[0]
answers = []
answer = line[2]
if type(context)!= str:
print(context, type(context))
continue
answer_starts = find_all(context, answer)
 for answer_start in answer_starts:
answers.append({'answer_start': answer_start, 'text': answer})
 qas.append({'question': '[MASK]', 'id': qid, 'is_impossible': False, 'answers': answers})
 paragraphs.append({'context': context, 'qas': qas})
 output['data'].append({'title': 'None', 'paragraphs': paragraphs})
 if not os.path.exists(directory):
os.makedirs(directory)
 with open(os.path.join(directory, filename), 'w')as outfile:
json.dump(output, outfile )
import numpy as np
import pandas as pd<load_from_csv>
for i, split in enumerate(splits):
data = np_train[split['train_idx']]
directory = 'split_' + str(i+1)
 filename = 'train.json'
convert_data(data, directory, filename )
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )<define_variables>
data = np_train
directory = 'original'
filename = 'train.json'
convert_data(data, directory, filename)
 data = np_test
filename = 'test.json'
convert_data(data, directory, filename )
women = train_data[train_data['Sex'] == 'female']['Survived']
rate_women = sum(women)/len(women)
 print('% of women who survived:', rate_women )<define_variables>
def run_script(train_file, predict_file, batch_size=16, lr=5e-5, epochs=2, max_seq_len=128, doc_stride=64):
!python /kaggle/input/pytorchtransformers/transformers-2.5.1/examples/run_squad.py \
--model_type distilbert \
--model_name_or_path distilbert-base-uncased \
--cache_dir /kaggle/input/cached-distilbert-base-uncased/cache \
--do_lower_case \
--do_train \
--do_eval \
--train_file=$train_file \
--predict_file=$predict_file \
--overwrite_cache \
--learning_rate=$lr \
--num_train_epochs=$epochs \
--max_seq_length=$max_seq_len \
--doc_stride=$doc_stride \
--output_dir./results \
--overwrite_output_dir \
--per_gpu_eval_batch_size=$batch_size \
--per_gpu_train_batch_size=$batch_size \
--save_steps=100000
men = train_data[train_data.Sex == 'male']['Survived']
rate_men = sum(men)/len(men)
 print('% of men who survived:', rate_men )<groupby>
if cross_validation:
for i in range(1, K+1):
train_file = "split_" + str(i)+ "/train.json"
predict_file = "original/train.json"
run_script(train_file, predict_file, batch_size, lr, epochs, max_seq_len, doc_stride)
 !mv "results/predictions_.json" "results/predictions_"$i".json"
train_data[['Sex', 'Survived']].groupby(['Sex'] ).mean()<groupby>
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c))
train_data[['Pclass', 'Survived']].groupby(['Pclass'] ).mean()<define_variables>
def evaluate(splits, np_train, post_processing=False):
K = len(splits)
 predictions = [json.load(open('results/predictions_' + str(i+1)+ '.json', 'r')) for i in range(K)]
train_score = [{'neutral':[], 'positive':[], 'negative':[], 'total':[]} for _ in range(K+1)]
valid_score = [{'neutral':[], 'positive':[], 'negative':[], 'total':[]} for _ in range(K+1)]
for train_idx, line in enumerate(np_train):
text_id = line[0]
text = line[1]
answer = line[2]
sentiment = line[-1]
if type(text)!= str:
continue
for i, prediction in enumerate(predictions):
if text_id not in prediction:
print('key error:', text_id)
 continue
else:
if post_processing and(sentiment == 'neutral' or len(text.split())<= 0):
score = jaccard(answer, text)
 else:
score = jaccard(answer, prediction[text_id])
 if train_idx in splits[i]['valid_idx']:
valid_score[i][sentiment].append(score)
 valid_score[i]['total'].append(score)
 valid_score[K][sentiment].append(score)
 valid_score[K]['total'].append(score)
 else:
train_score[i][sentiment].append(score)
 train_score[i]['total'].append(score)
 train_score[K][sentiment].append(score)
 train_score[K]['total'].append(score)
 for i, score_dict in enumerate([train_score, valid_score]):
if i == 0:
print('train score
')
 else:
print('valid score
')
 for j in range(K+1):
for sentiment in ['neutral', 'positive', 'negative', 'total']:
score = np.array(score_dict[j][sentiment])
 if j < K:
print('split', j+1)
 else:
print('all data')
 print(sentiment + ' - ' + str(len(score)) + ' examples, average score: ' + str(score.mean()))
print()
women_count = 0
women_survived_count = 0
for idx, row in train_data.iterrows() :
if row['Sex'] == 'female':
women_count += 1
if row['Survived'] == 1:
women_survived_count += 1
women_survived_count / women_count<define_variables>
if cross_validation:
evaluate(splits, np_train, post_processing )
count1w = 0
count1m = 0
count2w = 0
count2m = 0
count3w = 0
count3m = 0
countm = 0
countw = 0
for idx, row in train_data.iterrows() :
if row['Pclass'] == 1:
if row['Sex'] == 'female':
count1w += 1
else:
count1m += 1
elif row['Pclass'] == 2:
if row['Sex'] == 'female':
count2w += 1
else:
count2m += 1
else:
if row['Sex'] == 'female':
count3w += 1
else:
count3m += 1
if row['Sex'] == 'male':
countm += 1
else:
countw += 1
print("Class 1 Men:",count1m/countm)
 print("Class 1 Women:",count1w/countw)
 print("Class 2 Men:",count2m/countm)
 print("Class 2 Women:",count2w/countw)
 print("Class 3 Men:",count3m/countm)
 print("Class 3 Women:",count3w/countw)
<define_variables>
train_file = "original/train.json"
predict_file = "original/test.json"
run_script(train_file, predict_file, batch_size, lr, epochs, max_seq_len, doc_stride)
 !mv results/predictions_.json results/test_predictions.json
predictions = []
for idx, row in test_data.iterrows() :
if row['Sex'] == 'female':
if row['Pclass'] == 1 or row['Pclass'] == 2 or row['Age'] < 25.0:
predictions.append(1)
 else:
predictions.append(0)
 else:
if row['Age'] < 18.0 and row['Pclass'] == 1:
predictions.append(1)
 else:
predictions.append(0)
 print(predictions[:10] )<feature_engineering>
predictions = json.load(open('results/test_predictions.json', 'r'))
submission = pd.read_csv(open('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv', 'r'))
for i in range(len(submission)) :
id_ = submission['textID'][i]
if post_processing and(pd_test['sentiment'][i] == 'neutral' or len(pd_test['text'][i].split())<= 0):
submission.loc[i, 'selected_text'] = pd_test['text'][i]
else:
submission.loc[i, 'selected_text'] = predictions[id_]
<save_to_csv><EOS> 
submission.to_csv('submission.csv', index=False )