Kaggle competition: Natural Language Processing with Disaster Tweets
kfolds = 10
skf = MultilabelStratifiedKFold(n_splits=kfolds,
shuffle=True,
random_state=rand_seed)
 label_counts = np.sum(train_labels.drop("sig_id", axis=1), axis=0)
 y_labels = label_counts.index.tolist()<load_pretrained>
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 train.head(10 )
def get_model(model_path, test_set, transformer):
model = MoAEfficientNet.load_from_checkpoint(
 model_path,
pretrained_model_name=pretrained_model,
training_set=(None, None),
valid_set=(None, None),
test_set=test_set,
transformer=transformer,
drop_rate=drop_rate,
drop_connect_rate=drop_connect_rate,
fc_size=fc_size,
weight_init='goog')
 model.freeze()
model.eval()
return model
def save_pickle(obj, model_output_folder, fold_i, name):
dump(obj, open(f"{model_output_folder}/fold{fold_i}_{name}.pkl", 'wb'),
pickle.HIGHEST_PROTOCOL)
 def load_pickle(model_output_folder, fold_i, name):
return load(open(f"{model_output_folder}/fold{fold_i}_{name}.pkl", 'rb'))<compute_test_metric>
train.count()
def mean_logloss(y_pred, y_true):
logloss =(1 - y_true)* np.log(1 - y_pred +
1e-15)+ y_true * np.log(y_pred + 1e-15)
 return np.mean(-logloss )<compute_test_metric>
test.count()
oof_predictions = np.load(
 glob.glob(f'{model_info["model_path"]}/.. /oof*.npy')[0])
 oof_loss = mean_logloss(oof_predictions, train_labels[train_classes].values)
 print(f"OOF Validation Loss: {oof_loss:.6f}" )<prepare_output>
from nltk.corpus import stopwords
import re
import string
np.mean(kfold_submit_preds )<save_to_csv>
def change_contraction_verb(text):
text = re.sub(r"n't", " not", text)
 text = re.sub(r"'re", " are", text)
 text = re.sub(r"'s", " is", text)
 text = re.sub(r"'d", " would", text)
 text = re.sub(r"'ll", " will", text)
 text = re.sub(r"'t", " not", text)
 text = re.sub(r"'ve", " have", text)
 text = re.sub(r"'m", " am", text)
 text = re.sub(r"won't", "will not", text)
 text = re.sub(r"can't", "can not", text)
 return text
train['text'] = train['text'].apply(lambda x : change_contraction_verb(x))
test['text'] = test['text'].apply(lambda x : change_contraction_verb(x))
train['text'].head(10 )
submission = pd.DataFrame(data=test_features["sig_id"].values,
columns=["sig_id"])
 submission = submission.reindex(columns=["sig_id"] + train_classes)
 submission[train_classes] = kfold_submit_preds.clip(0, 1)
 submission.loc[test_features['cp_type'] == 0, submission.columns[1:]] = 0
submission.to_csv('submission.csv', index=False )<set_options>
def custom_preprocessor(text):

 text = text.lower()
text = re.sub('\[.*?\]', '', text)
 text = re.sub("\\W"," ",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)
 text = re.sub('\w*\d\w*', '', text)
 return text
train['text'] = train['text'].apply(lambda x : custom_preprocessor(x))
test['text'] = test['text'].apply(lambda x : custom_preprocessor(x))
train['text'].head(10 )
torch.cuda.empty_cache()
gc.collect()<install_modules>
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train['text'] = train['text'].apply(lambda x : remove_emoji(x))
test['text'] = test['text'].apply(lambda x : remove_emoji(x))
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/<set_options>
stopwords = stopwords.words('english')
 print(stopwords )
sns.set_style('white')
 sns.set(font_scale=1.2)
<set_options>
count_vectorizer = CountVectorizer(token_pattern=r'\w{1,}', ngram_range=(1, 2), stop_words = stopwords)
 train_vector = count_vectorizer.fit_transform(train['text'])
 test_vector = count_vectorizer.transform(test['text'] )
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(42 )<load_from_csv>
train_vector.toarray()
df_train = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 display(df_train.head(3))
print('train data size', df_train.shape)
 df_train.drop(columns=["sig_id"], inplace=True)
 df_target_ns = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 display(df_target_ns.head(3))
print('train target nonscored size', df_target_ns.shape)
 df_target_s = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 display(df_target_s.head(3))
print('train target scored size', df_target_s.shape)
 df_target_s.drop(columns=["sig_id"], inplace=True)
 df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 display(df_test.head(3))
print('test data size', df_test.shape)
 df_test.drop(columns=["sig_id"], inplace=True)
 df_sample = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 display(df_sample.head(3))
print('sample submission size', df_sample.shape)
 remove_vehicle = False
if remove_vehicle:
kept_index = df_train['cp_type']=='trt_cp'
df_train = df_train.loc[kept_index].reset_index(drop=True)
 df_target_s = df_target_s.loc[kept_index].reset_index(drop=True)
 df_train["cp_type"] =(df_train["cp_type"]=="trt_cp")+ 0
df_train["cp_dose"] =(df_train["cp_dose"]=="D1")+ 0
df_test["cp_type"] =(df_test["cp_type"]=="trt_cp")+ 0
df_test["cp_dose"] =(df_test["cp_dose"]=="D1")+ 0
X_test = df_test.values<choose_model_class>
clf = LogisticRegression()
scores = model_selection.cross_val_score(clf, train_vector, train["target"], cv=5, scoring="accuracy")
 print(scores )
MAX_EPOCH=200
tabnet_params = dict(n_d=24, n_a=24, n_steps=1, gamma=1.3,
lambda_sparse=0, optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=2e-2, weight_decay=1e-5),
mask_type='entmax',
scheduler_params=dict(mode="min",
patience=5,
min_lr=1e-5,
factor=0.9,),
scheduler_fn=torch.optim.lr_scheduler.ReduceLROnPlateau,
verbose=10,
)<import_modules>
print("Accuracy of Model with Cross Validation is: ",scores.mean() * 100 )
class LogitsLogLoss(Metric):

 def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):

 logits = 1 /(1 + np.exp(-y_pred))
aux =(1-y_true)*np.log(1-logits+1e-15)+ y_true*np.log(logits+1e-15)
 return np.mean(-aux )<prepare_x_and_y>
clf.fit(train_vector, train["target"] )
<compute_test_metric><EOS> 
submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 submission["target"] = clf.predict(test_vector)
 submission.to_csv("submission.csv", index=False )