Kaggle competition: Tweet Sentiment Extraction
<SOS> metric: Jaccard Kaggle data source: tweet-sentiment-extraction<load_from_csv>
import numpy as np
import pandas as pd
import json
sample_submission = pd.read_csv(".. /input/ykc-cup-1st/sample_submission.csv")
 sample_submission.head()<categorify>
train_data = pd.read_csv('.. /input/tweet-sentiment-extraction/train.csv')
 test_data = pd.read_csv('.. /input/tweet-sentiment-extraction/test.csv')
 submission_data = pd.read_csv('.. /input/tweet-sentiment-extraction/sample_submission.csv')
 train = np.array(train_data)
 test = np.array(test_data)
 !mkdir -p data
cat = ["store_id", "genre_name", "area_name", "day_of_week"]
for c in cat:
le = LabelEncoder()
train[c] = le.fit_transform(train[c].fillna("na"))
test[c] = le.transform(test[c].fillna("na"))
train.head()<concatenate>
%%time
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
train['is_test'] = 0
test['is_test'] = 1
data = pd.concat([train, test], sort=False)
 data.reset_index(drop=True, inplace=True)
 target = ['is_test']
features = ["store_id", "day_of_week", "genre_name", "area_name", "latitude", "longitude"]
print(data.shape)
 data.head()<init_hyperparams>
def do_qa_train(train):
output = []
for line in train:
context = line[1]
qas = []
question = line[-1]
qid = line[0]
answers = []
answer = line[2]
if type(answer)!= str or type(context)!= str or type(question)!= str:
print(context, type(context))
print(answer, type(answer))
print(question, type(question))
continue
answer_starts = find_all(context, answer)
 for answer_start in answer_starts:
answers.append({'answer_start': answer_start, 'text': answer.lower() })
 break
qas.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 output.append({'context': context.lower() , 'qas': qas})
 return output
qa_train = do_qa_train(train)
 with open('data/train.json', 'w')as outfile:
json.dump(qa_train, outfile )
lgb_params = {
'boost': 'gbdt',
'objective': 'binary',
'metric': 'auc',
'verbosity': 1,
}<split>
%%time
def do_qa_test(test):
output = []
for line in test:
context = line[1]
qas = []
question = line[-1]
qid = line[0]
if type(context)!= str or type(question)!= str:
print(context, type(context))
print(answer, type(answer))
print(question, type(question))
continue
answers = []
answers.append({'answer_start': 1000000, 'text': '__None__'})
 qas.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 output.append({'context': context.lower() , 'qas': qas})
 return output
qa_test = do_qa_test(test)
 with open('data/test.json', 'w')as outfile:
json.dump(qa_test, outfile )
kfold = KFold(n_splits=n_split, shuffle = True, random_state=42)
 for i_fold,(train_idx, valid_idx)in enumerate(kfold.split(data)) :
print(f"--------fold {i_fold}-------")
 x_tr = data.loc[train_idx, features]
y_tr = data.loc[train_idx, target]
x_va = data.loc[valid_idx, features]
y_va = data.loc[valid_idx, target]
lgb_train = lgb.Dataset(x_tr, label=y_tr)
 lgb_test = lgb.Dataset(x_va, label=y_va)
 model = lgb.train(
 lgb_params,
lgb_train,
valid_sets=[lgb_train, lgb_test],
valid_names=['train', 'test'],
num_boost_round=1000,
early_stopping_rounds=100,
verbose_eval=100 
)
 valid_preds = model.predict(x_va, num_iteration=model.best_iteration)
 valid_metric = roc_auc_score(y_va, valid_preds)
 print('Valid Metric: {:.5f}'.format(valid_metric))<set_options>
!pip install '/kaggle/input/simple-transformers-pypi/seqeval-0.0.12-py3-none-any.whl' -q
!pip install '/kaggle/input/simple-transformers-pypi/simpletransformers-0.22.1-py3-none-any.whl' -q
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
%%time
MODEL_PATH = '.. /input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'
model_args = {'reprocess_input_data': True,
'overwrite_output_dir': True,
'learning_rate': 5e-5,
'num_train_epochs': 2,
'max_seq_length': 192,
'doc_stride': 64,
'fp16': False
}
model = QuestionAnsweringModel('distilbert',
MODEL_PATH,
args = model_args,
use_cuda = True)
 model.train_model('data/train.json' )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.describe(include="all" ).T<count_missing_values>
%%time
predictions = model.predict(qa_test)
 predictions_df = pd.DataFrame.from_dict(predictions)
 submission_data['selected_text'] = predictions_df['answer']
submission_data.to_csv('submission.csv', index = False)
 print("File submitted successfully." )