Kaggle competition: Contradictory, My Dear Watson
<SOS> metric: categorizationaccuracy Kaggle data source: contradictory,-my-dear-watson<load_from_csv>
!pip install -q nlp
!pip install -q wordcloud
y_test = pd.read_csv(".. /input/gender_submission.csv", sep=",")
 cm = confusion_matrix(y_test['Survived'], y_pred2['Predicted'])
 print(cm)
<save_to_csv>
nltk.download("stopwords")
test_csv['Survived'] = y_pred2['Predicted']
test_csv.to_csv('submission_all_keras_nn_0.5.csv', index=False)
<set_options>
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 except ValueError:
strategy = tf.distribute.get_strategy()
print('Number of replicas:', strategy.num_replicas_in_sync )
%matplotlib inline<load_from_csv>
train_csv = pd.read_csv(".. /input/contradictory-my-dear-watson/train.csv")
 test_csv = pd.read_csv(".. /input/contradictory-my-dear-watson/test.csv" )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 train.head()<count_missing_values>
AUTO = tf.data.experimental.AUTOTUNE
MODEL_NAME = "jplu/tf-xlm-roberta-base"
REPLICAS = strategy.num_replicas_in_sync
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_NAME)
 BATCH_SIZE = 16 * REPLICAS
MAX_LEN = 192
EPOCHS = 8
SEED = 48
FONT_DIR = ".. /input/font-dataset/FontScripts/"
np.random.seed(SEED)
 random.seed(SEED )
print(pd.isnull(train ).sum() )<count_values>
def prepare_input_v2(sentences):

 sen_enc = TOKENIZER.batch_encode_plus(sentences,
pad_to_max_length=True,
return_attention_mask=False,
return_token_type_ids=False,
max_length=MAX_LEN)
 return np.array(sen_enc["input_ids"])
 def get_dataset(features, labels=None, labelled=True, batch_size=8, repeat=True, shuffle=True):

 if labelled:
ds = tf.data.Dataset.from_tensor_slices(( features, labels))
else:
ds = tf.data.Dataset.from_tensor_slices(features)
 if repeat:
ds = ds.repeat()
if shuffle:
ds = ds.shuffle(2048)
 ds = ds.batch(batch_size*REPLICAS)
 ds.prefetch(AUTO)
 return ds
def build_model() :

 encoder = TFAutoModel.from_pretrained(MODEL_NAME)
 input_word_ids = tf.keras.layers.Input(shape=(MAX_LEN,), dtype=tf.int32, name="input_word_ids")
 embeddings = encoder(input_word_ids)[0]
x = embeddings[:, 0, :]
output = tf.keras.layers.Dense(3, activation="softmax" )(x)
 model = tf.keras.models.Model(inputs=input_word_ids, outputs=output)
 model.compile(loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.05),
optimizer=tf.keras.optimizers.Adam(lr=1e-5),
metrics=["accuracy"])
 return model
def ratio_languages(df):

 languages = np.unique(df.language)
 total = df.language.value_counts().sum()
ratios = {}
for e in languages:
ratios[e] = round(( df.language.value_counts().loc[e] / total), 2)*100
ratios = sorted(ratios.items() , key=lambda x:(x[1],x[0]), reverse=True)
 languages = []
values = []
for e in ratios:
languages.append(e[0])
 values.append(e[1])
 _, texts, _ = plt.pie(values, explode=[0.2]*(len(values)) , labels=languages, autopct="%.2i%%", radius=2,
rotatelabels=True)
 for e in texts:
e.set_fontsize(15)
 e.set_fontfamily('fantasy')
 plt.show()
def get_lr_callback(batch_size):
lr_start = 0.000001
lr_max = 0.00000125 * batch_size
lr_min = 0.00000001
lr_sus_epoch = 0
lr_decay = 0.80
lr_ramp_ep = 5
lr = lr_start
def lrfn(epoch):
if epoch < lr_ramp_ep:
lr =(lr_max- lr_start)/lr_ramp_ep * epoch + lr_start
elif epoch <(lr_ramp_ep + lr_sus_epoch):
lr = lr_max
else:
lr =(lr_max - lr_min)*lr_decay**(epoch - lr_ramp_ep - lr_sus_epoch)+ lr_min
return lr
lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)
 return lr_callback
def plot_wordcloud(df, col):

 words = " "
font_path = None
fig, ax = plt.subplots(nrows=2, ncols=2)
 fig.set_size_inches(12, 12)
 res = []
for i in range(2):
for j in range(2):
res.append([i,j])
 for i,lang in enumerate(["English",
"Hindi",
"Urdu",
"German" ,
]):
for line in df[df.language==lang][col].values:
tokens = line.split()
tokens = [word.lower() for word in tokens]
words += " ".join(tokens)+" "
fig.add_subplot(ax[res[i][0]][res[i][1]])
 if lang=="Hindi":
font_path = FONT_DIR + "Hindi.ttf"
if lang=="French":
font_path = FONT_DIR + "French.ttf"
if lang=="Russian":
font_path= FONT_DIR + "Russian.ttf"
if lang=="Arabic":
font_path = FONT_DIR + "Arabic.ttf"
if lang=="Chinese":
font_path = FONT_DIR + "Chinese.otf"
if lang=="Swahili":
font_path = FONT_DIR + "Swahili.ttf"
if lang=="Urdu":
font_path = FONT_DIR + "Urdu.ttf"
if lang=="Vietnamese":
font_path = FONT_DIR + "Vietnamese.ttf"
if lang=="Greek":
font_path = FONT_DIR + "Greek.ttf"
if lang=="Thai":
font_path = FONT_DIR + "Thai.ttf"
if lang=="Spanish":
font_path = FONT_DIR + "Spanish.ttf"
if lang=="German":
font_path = FONT_DIR + "German.ttf"
if lang=="Turkish":
font_path = FONT_DIR + "Turkish.ttf"
if lang=="Bulgarian":
font_path = FONT_DIR + "Bulgarian.ttf"
s_words = STOPWORDS
wordcloud = WordCloud(font_path=font_path, width=800, height=800,
background_color="black",
min_font_size=10,
stopwords=s_words ).generate(words)
 ax[res[i][0]][res[i][1]].imshow(wordcloud)
 ax[res[i][0]][res[i][1]].axis("off")
 ax[res[i][0]][res[i][1]].set_title(f"Language: {lang}", fontsize=14 )
print("Percentage of females who survived:", train["Survived"][train["Sex"] == 'female'].value_counts(normalize = True)[1]*100)
 print("Percentage of males who survived:", train["Survived"][train["Sex"] == 'male'].value_counts(normalize = True)[1]*100)
<drop_column>
print(train_csv["language"].value_counts())
 print()
ratio_languages(train_csv )
train = train.drop(['Cabin'], axis = 1)
 test = test.drop(['Cabin'], axis = 1)
<drop_column>
xnli = nlp.load_dataset(path="xnli")
 buff = {}
buff["premise"] = []
buff["hypothesis"] = []
buff["label"] = []
buff["language"] = []
uniq_lang = set()
for e in xnli["test"]:
for i in e["hypothesis"]["language"]:
uniq_lang.add(i)
 language_map = {}
for e in uniq_lang:
language_map[e] = test_csv.loc[test_csv.lang_abv==e, "language"].iloc[0]
for x in xnli['test']:
label = x['label']
for idx, lang in enumerate(x['hypothesis']['language']):
if lang=="en":
continue
hypothesis = x['hypothesis']['translation'][idx]
premise = x['premise'][lang]
buff['premise'].append(premise)
 buff['hypothesis'].append(hypothesis)
 buff['label'].append(label)
 buff['language'].append(language_map[lang])
 xnli_df = pd.DataFrame(buff )
train = train.drop(['Ticket'], axis = 1)
 test = test.drop(['Ticket'], axis = 1 )<categorify>
xnli_df["language"].value_counts()
print("Number of people embarking in Southampton(S):")
 southampton = train[train["Embarked"] == "S"].shape[0]
print(southampton)
 print("Number of people embarking in Cherbourg(C):")
 cherbourg = train[train["Embarked"] == "C"].shape[0]
print(cherbourg)
 print("Number of people embarking in Queenstown(Q):")
 queenstown = train[train["Embarked"] == "Q"].shape[0]
print(queenstown)
 train = train.fillna({"Embarked": "S"} )<feature_engineering>
new_df = pd.concat([train_df, xnli_df], axis=0)
 new_df.sample(5 )
for dataset in combine:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Capt', 'Col',
'Don', 'Dr', 'Major', 'Rev', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace(['Countess', 'Lady', 'Sir'], 'Royal')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 train[['Title', 'Survived']].groupby(['Title'], as_index=False ).mean()<categorify>
pd.merge(new_df, test_csv, how="inner" )
title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Royal": 5, "Rare": 6}
for dataset in combine:
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
 train.head()<categorify>
new_df = new_df.merge(pd.merge(new_df, test_csv, how="inner"), how="left", indicator=True)
 new_df = new_df[new_df._merge=="left_only"]
new_df = new_df.drop(["id", "lang_abv", "_merge"], axis=1)
 new_df.info()
mr_age = train[train["Title"] == 1]["AgeGroup"].mode()
miss_age = train[train["Title"] == 2]["AgeGroup"].mode()
mrs_age = train[train["Title"] == 3]["AgeGroup"].mode()
master_age = train[train["Title"] == 4]["AgeGroup"].mode()
royal_age = train[train["Title"] == 5]["AgeGroup"].mode()
rare_age = train[train["Title"] == 6]["AgeGroup"].mode()
age_title_mapping = {1: "Young Adult", 2: "Student", 3: "Adult", 4: "Baby", 5: "Adult", 6: "Adult"}
for x in range(len(train["AgeGroup"])) :
if train["AgeGroup"][x] == "Unknown":
train["AgeGroup"][x] = age_title_mapping[train["Title"][x]]
for x in range(len(test["AgeGroup"])) :
if test["AgeGroup"][x] == "Unknown":
test["AgeGroup"][x] = age_title_mapping[test["Title"][x]]<categorify>
pd.merge(new_df, test_csv, how="inner" )
age_mapping = {'Baby': 1, 'Child': 2, 'Teenager': 3, 'Student': 4, 'Young Adult': 5, 'Adult': 6, 'Senior': 7}
train['AgeGroup'] = train['AgeGroup'].map(age_mapping)
 test['AgeGroup'] = test['AgeGroup'].map(age_mapping)
 train.head()<categorify>
ratio_languages(new_df )
train = train.drop(['Age'], axis = 1)
 test = test.drop(['Age'], axis = 1)
 train = train.drop(['Name'], axis = 1)
 test = test.drop(['Name'], axis = 1)
 sex_mapping = {"male": 0, "female": 1}
train['Sex'] = train['Sex'].map(sex_mapping)
 test['Sex'] = test['Sex'].map(sex_mapping)
 train.head() 
<categorify>
new_df.language.value_counts()
embarked_mapping = {"S": 1, "C": 2, "Q": 3}
train['Embarked'] = train['Embarked'].map(embarked_mapping)
 test['Embarked'] = test['Embarked'].map(embarked_mapping)
 train.head() 
<split>
X, y = new_df[["premise", "hypothesis"]], new_df.label
predictors = train.drop(['Survived', 'PassengerId'], axis=1)
 target = train["Survived"]
x_train, x_val, y_train, y_val = train_test_split(predictors, target, test_size = 0.22, random_state = 0 )<compute_train_metric>
X["language_label"] = new_df.language.astype(str)+ "_" + new_df.label.astype(str )
svc = SVC()
svc.fit(x_train, y_train)
 y_pred = svc.predict(x_val)
 acc_svc = round(accuracy_score(y_pred, y_val)* 100, 2)
 print(acc_svc )<save_to_csv>
print("Splitting Data...")
 x_train, x_test, y_train, y_test = train_test_split(X, y, stratify=X.language_label, test_size=0.2, random_state=SEED)
 y_train = tf.one_hot(y_train, depth=3)
 y_test = tf.one_hot(y_test, depth=3)
 print("Prepairing Input...")
 train_input = prepare_input_v2(x_train[["premise", "hypothesis"]].values.tolist())
 valid_input = prepare_input_v2(x_test[["premise", "hypothesis"]].values.tolist())
 print("Preparing Dataset...")
 train_dataset = get_dataset(train_input, y_train, labelled=True, batch_size=BATCH_SIZE, repeat=True,
shuffle=True)
 valid_dataset = get_dataset(valid_input, y_test, labelled=True, batch_size=BATCH_SIZE//REPLICAS, repeat=False,
shuffle=False)
 print("Downloading and Building Model...")
 with strategy.scope() :
model = build_model()
lr_callback = get_lr_callback(BATCH_SIZE)
 checkpoint = tf.keras.callbacks.ModelCheckpoint("XLM-R-base.h5", save_weights_only=True,
save_best_only=True, save_freq="epoch", monitor="val_loss",
mode="min")
 print("Training...")
 model.fit(train_dataset,
steps_per_epoch= x_train.shape[0]/BATCH_SIZE,
validation_data=valid_dataset,
epochs=EPOCHS,
callbacks=[lr_callback, checkpoint] )
ids = test['PassengerId']
predictions = svc.predict(test.drop('PassengerId', axis=1))
output = pd.DataFrame({ 'PassengerId' : ids, 'Survived': predictions })
 output.to_csv('submission.csv', index=False )<set_options>
test_input = prepare_input_v2(test_csv[["premise", "hypothesis"]].values.tolist())
 test_dataset = get_dataset(test_input, None, labelled=False, batch_size=BATCH_SIZE, repeat=False, shuffle=False )
%config InlineBackend.figure_format = 'retina'
%matplotlib inline<load_from_csv>
preds = model.predict(test_dataset )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
preds = preds.argmax(axis=1 )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<concatenate>
submission = pd.read_csv(".. /input/contradictory-my-dear-watson/sample_submission.csv")
 submission.head()
all_data = pd.concat(( train.loc[:,'MSSubClass':'SaleCondition'],
test.loc[:,'MSSubClass':'SaleCondition']))<feature_engineering>
submission["prediction"] = preds
<categorify><EOS> 
submission.to_csv("submission.csv", header=True, index=False )