Kaggle competition: Tweet Sentiment Extraction
<data_type_conversions><EOS> 
import pandas as pd
import numpy as np
<SOS> metric: Jaccard Kaggle data source: tweet-sentiment-extraction<merge>
import pandas as pd
import numpy as np
merged_df = pd.merge(test_df, gt_df, on='id')
 merged_df<save_to_csv>
train = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/train.csv')
 test = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/test.csv')
 sample = pd.read_csv('/kaggle/input/tweet-sentiment-extraction/sample_submission.csv' )
subm_df.to_csv('submission.csv', index=False )<load_from_url>
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text )
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization<categorify>
nltk.download('stopwords')
 REPLACE_BY_SPACE_RE = re.compile('[/() {}\[\]\|@,;]')
 BAD_SYMBOLS_RE = re.compile('[^0-9a-z
stop_words = set(stopwords.words('english'))
def clean_text(text):
text = text.lower()
text = REPLACE_BY_SPACE_RE.sub(' ', text)
 text = BAD_SYMBOLS_RE.sub(' ', text)
 text = ' '.join(word for word in text.split() if word not in stop_words)
 return text
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
train = np.array(train)
 test = np.array(test )
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
def search(input_string, search_string):
length = len(input_string)
 start_index = []
length = len(input_string)
 index = 0
while index < length:
i = input_string.find(search_string, index)
 if i == -1:
return start_index
start_index.append(i)
 index = i + 1
return start_index
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<load_from_csv>
def convert_train_to_json(train_set):
outer_list = []
for row in train_set:
qid = row[0]
context = row[1]
answer = row[2]
question = row[-1]
inner_list = []
answers = []
if type(context)!= str or type(answer)!= str or type(question)!= str:
continue
answer_starts = search(context, answer)
 for answer_start in answer_starts:
answers.append({'answer_start': answer_start, 'text': answer.lower() })
 break
inner_list.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 outer_list.append({'context': context.lower() , 'qas': inner_list})
 return outer_list
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<feature_engineering>
train = convert_train_to_json(train )
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<categorify>
def convert_test_to_json(test_set):
outer_list = []
for row in test_set:
qid = row[0]
context = row[1]
question = row[-1]
inner_list = []
if type(context)!= str or type(question)!= str:
continue
answers = []
answers.append({'answer_start': 1000000, 'text': '__None__'})
 inner_list.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 outer_list.append({'context': context.lower() , 'qas': inner_list})
 return outer_list
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values<train_model>
test = convert_test_to_json(test )
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)<predict_on_test>
!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.load_weights('model.h5')
 test_pred = model.predict(test_input )<save_to_csv>
from simpletransformers.question_answering import QuestionAnsweringModel
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
MODEL = '/kaggle/input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'
model = QuestionAnsweringModel('distilbert',
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
},
use_cuda=True
)
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.utils import shuffle
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import Perceptron
from sklearn import svm
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import VotingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import cross_validate
from sklearn.model_selection import GridSearchCV
from sklearn import svm, tree, linear_model, neighbors, naive_bayes, ensemble, discriminant_analysis, gaussian_process
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn import feature_selection
from sklearn import model_selection
from sklearn import metrics
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import seaborn as sns
from pandas.tools.plotting import scatter_matrix<load_from_csv>
model.train_model('data/train.json' )
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 data_df = train_df.append(test_df )<categorify>
pred_df = model.predict(test)
 pred_df = pd.DataFrame.from_dict(pred_df )
data_df['Title'] = data_df['Name']
for name_string in data_df['Name']:
data_df['Title'] = data_df['Name'].str.extract('([A-Za-z]+)\.', expand=True)
 mapping = {'Mlle': 'Miss', 'Major': 'Mr', 'Col': 'Mr', 'Sir': 'Mr', 'Don': 'Mr', 'Mme': 'Miss',
'Jonkheer': 'Mr', 'Lady': 'Mrs', 'Capt': 'Mr', 'Countess': 'Mrs', 'Ms': 'Miss', 'Dona': 'Mrs'}
data_df.replace({'Title': mapping}, inplace=True)
 titles = ['Dr', 'Master', 'Miss', 'Mr', 'Mrs', 'Rev']
for title in titles:
age_to_impute = data_df.groupby('Title')['Age'].median() [titles.index(title)]
data_df.loc[(data_df['Age'].isnull())&(data_df['Title'] == title), 'Age'] = age_to_impute
train_df['Age'] = data_df['Age'][:891]
test_df['Age'] = data_df['Age'][891:]
data_df.drop('Title', axis = 1, inplace = True )<feature_engineering>
sample["selected_text"] = pred_df["answer"]
data_df['Family_Size'] = data_df['Parch'] + data_df['SibSp']
train_df['Family_Size'] = data_df['Family_Size'][:891]
test_df['Family_Size'] = data_df['Family_Size'][891:]<feature_engineering>
sample.to_csv('submission.csv', index=False )
<groupby><EOS> 
print("Everything is successful! Good Luck for the score!" )