Kaggle competition: Tweet Sentiment Extraction
alphas = [0.000542555]
l1ratio = [0.1, 0.3,0.5, 0.9, 0.95, 0.99, 1]
elastic_cv = ElasticNetCV(cv=5, max_iter=1e7, alphas=alphas, l1_ratio=l1ratio)
 elasticmod = elastic_cv.fit(x_train, y_train.ravel())
 ela_pred=elasticmod.predict(x_test)
 print(f'Root Mean Square Error test = {score(ela_pred)}')
 print(elastic_cv.alpha_)
 print(elastic_cv.l1_ratio_ )<split>
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
def do_qa_train(train):
output = {}
output['version'] = 'v1.0'
output['data'] = []
paragraphs = []
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})
 paragraphs.append({'context': context.lower() , 'qas': qas})
 output['data'].append({'title': 'None', 'paragraphs': paragraphs})
 return paragraphs
def do_qa_test(test):
paragraphs = []
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})
 paragraphs.append({'context': context.lower() , 'qas': qas})
 output['data'].append({'title': 'None', 'paragraphs': paragraphs})
 return paragraphs
use_cuda = False
train_df = read_train()
test_df = read_test()
submission_df_distil = read_submission()
train = np.array(train_df)
 test = np.array(test_df)
 qa_train = do_qa_train(train)
 with open('data/train.json', 'w')as outfile:
json.dump(qa_train, outfile)
 output = {}
output['version'] = 'v1.0'
output['data'] = []
qa_test = do_qa_test(test)
 with open('data/test.json', 'w')as outfile:
json.dump(qa_test, outfile)
def regularization(x,y,modelo=Ridge, scaler=RobustScaler):

 y=np.array(target)
 x_train, x_test, y_train, y_test = train_test_split(x, y,test_size =.3, random_state=0)
 scaler= scaler()
x_train = scaler.fit_transform(x_train)
 x_test = scaler.transform(x_test)
 X_test= scaler.transform(Test)
 if modelo != ElasticNetCV:
if modelo == Ridge:
parameters= {'alpha':[x for x in range(1,101)]}
elif modelo == Lasso:
parameters= {'alpha':[0.0001,0.0009,0.001,0.002,0.003,0.01,0.1,1,10,100]}
model=modelo()
model=ms.GridSearchCV(model, param_grid=parameters, scoring='neg_mean_squared_error', cv=15)
 model.fit(x_train,y_train)
 y_pred= model.predict(x_test)
 print("The best RMSE score achieved with %s is: %s " %(model.best_params_,
score(y_pred)))
 elif modelo == ElasticNetCV:
alphas = [0.000542555]
l1ratio = [0.1, 0.3,0.5, 0.9, 0.95, 0.99, 1]
elastic_cv = ElasticNetCV(cv=5, max_iter=1e7, alphas=alphas, l1_ratio=l1ratio)
 elasticmod = elastic_cv.fit(x_train, y_train)
 ela_pred=elasticmod.predict(x_test)
 print("The best RMSE score achieved with alpha %s and l1_ratio %s is: %s "
%(elastic_cv.alpha_,elastic_cv.l1_ratio_, score(ela_pred)))
<init_hyperparams>
def post_process(submission_df, test_df):
index_to_selected_text = {}
for i, row in test_df.iterrows() :
_id = row[0]
text = row[1]
sentiment = row[2]
if len(text.split(" ")) <= 3 or sentiment == "neutral":
index_to_selected_text[_id] = text
submission_rows = submission_df.to_dict("records")
 new_rows = []
for row in submission_rows:
_id = row['textID']
if _id in index_to_selected_text:
new_row = deepcopy(row)
 new_row['selected_text'] = index_to_selected_text[_id]
else:
new_row = row
new_rows.append(new_row)
 return pd.DataFrame(new_rows)
<choose_model_class><EOS> 
MODEL_PATH = '/kaggle/input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'
model = QuestionAnsweringModel('distilbert',
MODEL_PATH,
args={"reprocess_input_data": True,
"overwrite_output_dir": True,
"learning_rate": 8e-05,
"num_train_epochs": 3,
"max_seq_length": 192,
"weight_decay": 0.001,
"doc_stride": 64,
"save_model_every_epoch": False,
"fp16": False,
"do_lower_case": True,
'max_query_length': 8,
'max_answer_length': 150
},
use_cuda=True)
 model.train_model('data/train.json')
 predictions = model.predict(qa_test)
 predictions_df = pd.DataFrame.from_dict(predictions)
 submission_df_distil['selected_text'] = predictions_df['answer']
submission_df_distil = post_process(submission_df_distil, test_df)
 submission_df_distil.to_csv('data/submission.csv', index=False)
 submission_df_distil.to_csv('submission.csv', index=False)