Kaggle competition: Mercari Price Suggestion Challenge
positive_text = train[train['sentiment'] == 'positive']['selected_text']
negative_text = train[train['sentiment'] == 'negative']['selected_text']
neutral_text = train[train['sentiment'] == 'neutral']['selected_text']<categorify>
data = pd.read_csv('/kaggle/input/submission/prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
def clean_text(text):

 text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub('https?://\S+|www\.\S+', '', text)
 text = re.sub('<.*?>+', '', text)
 text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
 text = re.sub('
', '', text)
 '(new lines)
 text = re.sub('\w*\d\w*', '', text)
 return text
def text_preprocessing(text):

 tokenizer = nltk.tokenize.RegexpTokenizer(r'\w+')
 nopunc = clean_text(text)
 tokenized_text = tokenizer.tokenize(nopunc)
 remove_stopwords = [w for w in tokenized_text if w not in stopwords.words('english')]
combined_text = ' '.join(tokenized_text)
 return combined_text<feature_engineering>
data = pd.read_csv('/kaggle/input/linreg/lin_reg_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
positive_text_clean = positive_text.apply(lambda x: text_preprocessing(x))
negative_text_clean = negative_text.apply(lambda x: text_preprocessing(x))
neutral_text_clean = neutral_text.apply(lambda x: text_preprocessing(x))<define_variables>
data = pd.read_csv('/kaggle/input/supportvector/svm_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
PATH = '.. /input/tf-roberta/'
tokenizer = tokenizers.ByteLevelBPETokenizer(
 vocab_file=PATH+'vocab-roberta-base.json',
merges_file=PATH+'merges-roberta-base.txt',
lowercase=True,
add_prefix_space=True
)<categorify>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
MAX_LEN = 0
for text in train['text']:
input_ids = tokenizer.encode(text, add_special_tokens=True)
 MAX_LEN = max(MAX_LEN, len(input_ids))
print('Max length: ', MAX_LEN )<categorify>
data = pd.read_csv('/kaggle/input/randomforest/random_forest_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
sent = ['positive','negative','neutral']
sent_id =[]
for i in sent:
sent_id.append(tokenizer.encode(i ).ids[0])
 sentiment_id = dict(zip(sent,sent_id))
sentiment_id<categorify>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
ct = train.shape[0]
input_ids = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids = np.zeros(( ct,MAX_LEN),dtype='int32')
 start_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 end_tokens = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(train.shape[0]):
text1 = " "+" ".join(train.loc[k,'text'].split())
 text2 = " ".join(train.loc[k,'selected_text'].split())
 idx = text1.find(text2)
 chars = np.zeros(( len(text1)))
 chars[idx:idx+len(text2)]=1
if text1[idx-1]==' ':
chars[idx-1] = 1
enc = tokenizer.encode(text1)
 offsets = []; idx=0
for t in enc.ids:
w = tokenizer.decode([t])
 offsets.append(( idx,idx+len(w)))
 idx += len(w)
 toks = []
for i,(a,b)in enumerate(offsets):
sm = np.sum(chars[a:b])
 if sm>0: toks.append(i)
 s_tok = sentiment_id[train.loc[k,'sentiment']]
input_ids[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask[k,:len(enc.ids)+5] = 1
if len(toks)>0:
start_tokens[k,toks[0]+1] = 1
end_tokens[k,toks[-1]+1] = 1<categorify>
data = pd.read_csv('/kaggle/input/decisiontree/decision_tree_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
ct = test.shape[0]
input_ids_t = np.ones(( ct,MAX_LEN),dtype='int32')
 attention_mask_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 token_type_ids_t = np.zeros(( ct,MAX_LEN),dtype='int32')
 for k in range(test.shape[0]):
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 s_tok = sentiment_id[test.loc[k,'sentiment']]
input_ids_t[k,:len(enc.ids)+5] = [0] + enc.ids + [2,2] + [s_tok] + [2]
attention_mask_t[k,:len(enc.ids)+5] = 1<load_pretrained>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
def build_model() :
ids = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 att = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 tok = tf.keras.layers.Input(( MAX_LEN,), dtype=tf.int32)
 config = RobertaConfig.from_pretrained(PATH+'config-roberta-base.json')
 bert_model = TFRobertaModel.from_pretrained(PATH+'pretrained-roberta-base.h5',config=config)
 x = bert_model(ids,attention_mask=att,token_type_ids=tok)

x1 = tf.keras.layers.Dropout(0.1 )(x[0])
 x1 = tf.keras.layers.Conv1D(1,1 )(x1)
 x1 = tf.keras.layers.Flatten()(x1)
 x1 = tf.keras.layers.Activation('softmax' )(x1)
 x2 = tf.keras.layers.Dropout(0.1 )(x[0])
 x2 = tf.keras.layers.Conv1D(1,1 )(x2)
 x2 = tf.keras.layers.Flatten()(x2)
 x2 = tf.keras.layers.Activation('softmax' )(x2)
 model = tf.keras.models.Model(inputs=[ids, att, tok], outputs=[x1,x2])
 optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer)
 return model<split>
data = pd.read_csv('/kaggle/input/lstmcnn/lstm_cnn_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
def jaccard(str1, str2):
a = set(str1.lower().split())
 b = set(str2.lower().split())
 if(len(a)==0)&(len(b)==0): return 0.5
c = a.intersection(b)
 return float(len(c)) /(len(a)+ len(b)- len(c)) 
<define_variables>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
jac = []; VER='v0'; DISPLAY=1
oof_start = np.zeros(( input_ids.shape[0],MAX_LEN))
oof_end = np.zeros(( input_ids.shape[0],MAX_LEN))
preds_start = np.zeros(( input_ids_t.shape[0],MAX_LEN))
preds_end = np.zeros(( input_ids_t.shape[0],MAX_LEN))
skf = StratifiedKFold(n_splits=5,shuffle=True,random_state=777)
 for fold,(idxT,idxV)in enumerate(skf.split(input_ids,train.sentiment.values)) :
print('
print('
print('
K.clear_session()
model = build_model()
sv = tf.keras.callbacks.ModelCheckpoint(
 '%s-roberta-%i.h5'%(VER,fold), monitor='val_loss', verbose=1, save_best_only=True,
save_weights_only=True, mode='auto', save_freq='epoch')
 model.fit([input_ids[idxT,], attention_mask[idxT,], token_type_ids[idxT,]], [start_tokens[idxT,], end_tokens[idxT,]],
epochs=5, batch_size=32, verbose=DISPLAY, callbacks=[sv],
validation_data=([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],
[start_tokens[idxV,], end_tokens[idxV,]]))
print('Loading model...')
 model.load_weights('%s-roberta-%i.h5'%(VER,fold))
print('Predicting OOF...')
 oof_start[idxV,],oof_end[idxV,] = model.predict([input_ids[idxV,],attention_mask[idxV,],token_type_ids[idxV,]],verbose=DISPLAY)
 print('Predicting Test...')
 preds = model.predict([input_ids_t,attention_mask_t,token_type_ids_t],verbose=DISPLAY)
 preds_start += preds[0]/skf.n_splits
preds_end += preds[1]/skf.n_splits
all = []
for k in idxV:
a = np.argmax(oof_start[k,])
 b = np.argmax(oof_end[k,])
 if a>b:
st = train.loc[k,'text']
else:
text1 = " "+" ".join(train.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 all.append(jaccard(st,train.loc[k,'selected_text']))
jac.append(np.mean(all))
print('>>>> FOLD %i Jaccard ='%(fold+1),np.mean(all))
print()<categorify>
data = pd.read_csv('/kaggle/input/ridgereg/ridge_reg_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
result= []
for k in range(input_ids_t.shape[0]):
a = np.argmax(preds_start[k,])
 b = np.argmax(preds_end[k,])
 if a>b:
st = test.loc[k,'text']
else:
text1 = " "+" ".join(test.loc[k,'text'].split())
 enc = tokenizer.encode(text1)
 st = tokenizer.decode(enc.ids[a-1:b])
 result.append(st)
 result[:5]<save_to_csv>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
test['selected_text'] = result
test[['textID','selected_text']].to_csv('submission.csv',index=False)
 pd.read_csv('submission.csv' ).head(10 )<load_from_csv>
data = pd.read_csv('/kaggle/input/stacking/stacking_prediction_stg2.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
train_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')
 test_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv')
 sub_df = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv')
 train_df=train_df[train_df['sentiment'] != 'neutral']
print(train_df.info())
 train_df['len1'] = train_df['selected_text'].apply(lambda x:len(str(x ).split(' ')))
 print(train_df.sentiment.value_counts())
 train_df=train_df[train_df.len1<=18]
print(train_df.info())
 print(train_df.sentiment.value_counts())
 train_df=train_df.drop(['len1'],axis=1)
<feature_engineering>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
def rev_sentence(sentence):
words = sentence.split(' ')
 reverse_sentence = ' '.join(reversed(words))
return reverse_sentence
train = np.array(train_df)
 test = np.array(test_df)
 !mkdir -p data
use_cuda = True
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 
<categorify>
data = pd.read_csv('/kaggle/input/ridgeregmodif/ridge_reg_prediction_stg2_modif.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
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'] = []
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
qa_test = do_qa_test(test)
 print('done')
 with open('data/test.json', 'w')as outfile:
json.dump(qa_test, outfile)
 !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<define_variables>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
MODEL_PATH = '/kaggle/input/bert-pretrained-transformers/'
model = QuestionAnsweringModel('bert',
MODEL_PATH,
args={'reprocess_input_data': True,
'overwrite_output_dir': True,
'learning_rate': 3e-5,
'num_train_epochs': 2,
'max_seq_length': 128,
'doc_stride': 64,
'fp16': False,'train_batch_size': 64
},
use_cuda=use_cuda)
 model.train_model('data/train.json')
 predictions = model.predict(qa_test)
 predictions_df = pd.DataFrame.from_dict(predictions)
 sub_df['selected_text'] = predictions_df['answer']
print("File submitted successfully." )<feature_engineering>
data = pd.read_csv('/kaggle/input/svmmodif/svm_prediction_stg2_modif.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
sub_df['selected_text'][test_df['sentiment']=='neutral'] = test_df['text'][test_df['sentiment']=='neutral']
sub_df.to_csv('submission.csv', index=False )<import_modules>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
print(os.listdir(".. /input"))
print(os.listdir(".. /input/jigsaw-toxic-comment-classification-challenge")) 
<load_from_csv>
data = pd.read_csv('/kaggle/input/lgbmmodif/lgbm_prediction_stg2_modif.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
EMBEDDING_FILE = '.. /input/glove840b300dtxt/glove.840B.300d.txt'
train = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/train.csv.zip',compression='zip')
 test = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/test.csv.zip',compression='zip' )<prepare_x_and_y>
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()
train["comment_text"].fillna("fillna")
 test["comment_text"].fillna("fillna")
 X_train = train["comment_text"].str.lower()
y_train = train[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]].values
X_test = test["comment_text"].str.lower()<define_variables>
data = pd.read_csv('/kaggle/input/stackingmodif/stacking_prediction_stg2_modif.csv')
 data.to_csv('/kaggle/working/submission.csv', index=False )
<compute_train_metric><EOS> 
result = pd.read_csv('/kaggle/working/submission.csv')
 result.head()