Kaggle competition: Tweet Sentiment Extraction
cols_to_drop = mis_cols[mis_cols>45].index
cols_to_drop<drop_column>
train_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/train.csv")
 question_p="positive sentence? "
question_n="negative sentence?"
question_neu="neutral sentence? "
housing_df.drop('MiscVal',axis=1,inplace=True )<filter>
def make_question(x):
if x=="positive":
return question_p
elif x=="negative":
return question_n
else:
return question_neu
train_df["sentiment"]=train_df["sentiment"].apply(make_question)
 train_df["sentiment"]
mis_cols[mis_cols<45]<filter>
train_df=train_df.dropna()
Garage_missing = mis_cols[categorical_col[0:4]].index
Garage_missing<categorify>
def answer_index(x):
return x["text"].find(str(x["selected_text"]))
train_df["answer_index"]=train_df.apply(answer_index,axis=1 )
housing_df[Garage_missing]=housing_df[Garage_missing].replace(np.nan,'No Garage' )<filter>
train_data=[]
def make_train_data(df):
for ind in df.index:
main_d=dict()
context=df.loc[ind,"text"].lower()
main_d["context"]=context
qas=[]
inside_qas={}
inside_qas["id"]=df.loc[ind,"textID"]
inside_qas["question"]=df.loc[ind,"sentiment"]
inside_answer=[{"text":df.loc[ind,"selected_text"].lower() ,"answer_start": int(df.loc[ind,"answer_index"])}]
inside_qas["answers"]=inside_answer
inside_qas["is_impossible"]=False
qas.append(inside_qas)
 main_d["qas"]=qas
train_data.append(main_d)
 return train_data
Bsmt_missing = mis_cols[categorical_col[4:9]].index
Bsmt_missing<categorify>
train_data=make_train_data(train_df )
housing_df[Bsmt_missing]=housing_df[Bsmt_missing].replace(np.nan,'No Basement' )<feature_engineering>
with open('/kaggle/working/train.json', 'w')as outfile:
json.dump(train_data, outfile)
housing_df[categorical_col[9]]=housing_df[categorical_col[9]].replace(np.nan,'None' )<feature_engineering>
!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
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': 5e-5,
'num_train_epochs': 2,
'max_seq_length': 128,
'doc_stride': 64,
'fp16': False,
},
use_cuda=True)
housing_df[categorical_col[10]]=housing_df[categorical_col[10]].replace(np.nan,'RL' )<feature_engineering>
model.train_model('/kaggle/working/train.json' )
housing_df[categorical_col[11]]=housing_df[categorical_col[11]].replace(np.nan,'AllPub' )<feature_engineering>
test_data=[]
def make_test_data(df):
for ind in df.index:
main_d=dict()
context=df.loc[ind,"text"].lower()
main_d["context"]=context
qas=[]
inside_qas={}
inside_qas["id"]=df.loc[ind,"textID"]
inside_qas["question"]=df.loc[ind,"sentiment"]
inside_qas["is_impossible"]=False
qas.append(inside_qas)
 main_d["qas"]=qas
test_data.append(main_d)
 return test_data
housing_df[categorical_col[12]]=housing_df[categorical_col[12]].replace(np.nan,'Typ' )<feature_engineering>
test_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/test.csv")
 test_df["sentiment"]=test_df["sentiment"].apply(make_question)
 test_data=make_test_data(test_df)
 test_data_json=json.dumps(test_data)
 with open('/kaggle/working/test.json', 'w')as outfile:
json.dump(test_data, outfile )
housing_df[categorical_col[13]]=housing_df[categorical_col[13]].replace(np.nan,'VinylSd')
 housing_df[categorical_col[14]]=housing_df[categorical_col[14]].replace(np.nan,'VinylSd' )<feature_engineering>
predictions=model.predict(test_data )
<feature_engineering><EOS> 
predictions_df = pd.DataFrame.from_dict(predictions)
 sub_df=pd.read_csv("/kaggle/input/tweet-sentiment-extraction/sample_submission.csv")
 sub_df['selected_text'] = predictions_df['answer']
sub_df.to_csv('submission.csv', index=False )