Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<prepare_output>
model_type = 'distilbert'
model_name = 'distilbert-base-uncased'
with_kfold = False
weight = [0.43, 0.57]
dataset = 'DATA2'
n_splits = 1
seed = 42
model_args = {'fp16': False,
'train_batch_size': 4,
'gradient_accumulation_steps': 2,
'do_lower_case': True,
'learning_rate': 1e-05,
'overwrite_output_dir': True,
'manual_seed': seed,
'num_train_epochs': 2}
results = pd.DataFrame(grid_search.cv_results_)
 results.head(10 )<compute_test_metric>
!pip install --upgrade transformers
!pip install simpletransformers
scores_mean = np.array(results.mean_test_score ).reshape(-1)
 scores_std = np.array(results.std_test_score ).reshape(-1)
 print("mean CV scores for each fold {} ".format(scores_mean))
print("std CV scores for each fold {} ".format(scores_std)) 
<set_options>
warnings.simplefilter('ignore')
 pd.set_option('max_rows', 100)
 pd.set_option('max_colwidth', 2000 )
gc.collect()<predict_on_test>
random.seed(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
pred_lr = grid_search.predict_proba(test_df)[:,1] 
<save_to_csv>
if dataset == 'DATA1':
train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')[['text', 'target']]
test_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')[['text']]
elif(dataset == 'DATA2')or(dataset == 'DATA2b'):
train_data = pd.read_csv('.. /input/nlp-with-disaster-tweets-cleaning-data/train_data_cleaning.csv')[['text', 'target']]
test_data = pd.read_csv('.. /input/nlp-with-disaster-tweets-cleaning-data/test_data_cleaning.csv')[['text']]
sample_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )
sub_df = pd.DataFrame()
sub_df["id"] = test_id
sub_df["target"] = pred_lr
sub_df.to_csv("baseline_lr.csv", index=False )<import_modules>
def subtext_repeation_in_df(df, col, subtext, num):
text = "".join(df[col])
 result = pd.DataFrame(columns = ['subtext', 'count'])
 i = 0
if(len(df)> 0)and(len(subtext)> 0):
for c in subtext:
for j in range(num):
cs = c*(j+1)
 result.loc[i,'count'] = text.count(cs)
 if c == ' ':
cs = cs.replace(' ','<space>')
 result.loc[i,'subtext'] = cs
i += 1
print('Number of all data is', len(df))
result = result[result['count'] > 0].reset_index(drop=True)
 display(result.sort_values(by='subtext'))
print('Text examples')
 problem_examples = pd.DataFrame(columns = ['problem_examples'])
 problem_examples['problem_examples'] = ''
for i in range(len(result)) :
problem_examples.loc[i,'problem_examples'] = df[df[col].str.find(result.loc[i,'subtext'])>-1].reset_index(drop=True ).loc[0, col]
problem_examples = problem_examples.drop_duplicates()
display(problem_examples )
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
import xgboost as xgb
from sklearn.linear_model import ElasticNet
from sklearn.ensemble import AdaBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.linear_model import LogisticRegression<load_from_csv>
print('Statistics for punctuation marks repetition in training data')
 subtext_repeation_in_df(train_data, 'text', list(string.punctuation), 10 )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 sub = pd.read_csv(".. /input/sample_submission.csv")
 sub['id'] = test['id']<prepare_x_and_y>
print('Statistics for punctuation marks repetition in test data')
 subtext_repeation_in_df(test_data, 'text', list(string.punctuation), 10 )
train_X = train.drop(['id','target'], axis=1)
 train_y = train['target']
test = test.drop(labels = 'id', axis = 1 )<prepare_x_and_y>
if not with_kfold:
model = ClassificationModel(model_type, model_name, args=model_args, weight=weight)
 model.train_model(train_data)
 result, model_outputs, wrong_predictions = model.eval_model(train_data, acc=sklearn.metrics.accuracy_score)
 y_preds, _, = model.predict(test_data['text'])
 pred_train, _ = model.predict(train_data['text'] )
x = train.drop(labels = 'target', axis=1)
 y = train["target"]<import_modules>
if with_kfold:
kf = KFold(n_splits=n_splits, random_state=seed, shuffle=True)
 results = []
wrong_predictions = []
y_preds = np.zeros(test_data.shape[0])
 pred_train = np.zeros(train_data.shape[0])
 first_fold = True
for train_index, val_index in kf.split(train_data):
train_df = train_data.iloc[train_index]
val_df = train_data.iloc[val_index]
model = ClassificationModel(model_type, model_name, args=model_args)
 model.train_model(train_df)
 result, model_outputs_fold, wrong_predictions_fold = model.eval_model(val_df, acc=sklearn.metrics.accuracy_score)
 pred_train[val_index], _ = model.predict(val_df['text'].reset_index(drop=True))
if first_fold:
model_outputs = model_outputs_fold
first_fold = False
else: model_outputs = np.vstack(( model_outputs,model_outputs_fold))
wrong_predictions += wrong_predictions_fold
results.append(result['acc'])
 y_pred, _ = model.predict(test_data['text'])
 y_preds += y_pred / n_splits
from sklearn.model_selection import train_test_split 
<train_model>
if with_kfold:
for i, result in enumerate(results, 1):
print(f"Fold-{i}: {result}")
 acc = np.mean(results)
 print(f"{n_splits}-fold CV accuracy result: Mean: {acc} Standard deviation:{np.std(results)}" )
d_train = lgb.Dataset(train_X, label=train_y)
 params = {}
params['alpha'] = 0.2
params['l1_ratio'] = 0.31
params['precompute'] = True
params['selection'] = 'random'
params['tol'] = 0.001
params['random_state'] = 2
clf = lgb.train(params, d_train, 100 )<predict_on_test>
y_preds[:] = y_preds[:]>=0.5
y_preds = y_preds.astype(int)
 np.mean(y_preds )
sub["target"] = abs(clf.predict(test))<save_to_csv>
sample_submission["target"] = y_preds
sample_submission.to_csv("submission.csv", index=False)
 y_preds[:20]
sub.to_csv("sub.csv",index= False)
<load_from_csv>
stop_words = list(STOPWORDS)+ list('0123456789')+ ['rt', 'amp', 'us', 'will', 'via', 'dont', 'cant', 'u', 'work', 'im',
'got', 'back', 'first', 'one', 'two', 'know', 'going', 'time', 'go', 'may', 'youtube', 'say', 'day', 'love',
'still', 'see', 'watch', 'make', 'think', 'even', 'right', 'left', 'take', 'want', 'http', 'https', 'co']
train=pd.read_csv('.. /input/train.csv')
 test=pd.read_csv('.. /input/test.csv')
 sub=pd.read_csv('.. /input/sample_submission.csv')
<drop_column>
outliers = pd.DataFrame(columns = ['text', 'label'])
 for i in range(len(wrong_predictions)) :
outliers.loc[i, 'text'] = wrong_predictions[i].text_a
outliers.loc[i, 'label'] = wrong_predictions[i].label
train_feat=train.drop(columns=['id','target'],axis=1)
 train_feat.head()<drop_column>
print('Statistics for punctuation marks repetition in outliers')
 subtext_repeation_in_df(outliers, 'text', list(string.punctuation), 10 )
test_feat=test.drop(columns='id',axis=1)
 test_feat.head()<create_dataframe>
num_outliers_per_cent = round(len(outliers)*100/len(test_data), 1 )
target=pd.DataFrame(pred3 ).rename(columns={0:'target'})
 sub_id=sub[['id']]
submission=pd.concat([sub_id,target],axis=1)
 submission 
<save_to_csv>
acc_round = round(acc,3)
 print('acc =', acc, '=', acc_round )
submission.to_csv('sub.csv',index=False )<set_options>
if n_splits == 1:
n_splits_res = ""
else: n_splits_res = f"n_splits = {n_splits}, "
<set_options><EOS> 
print(f"Model - {model_type}, {model_name}")
 print(f"* {dataset} - Commit __(LB = 0._____): lr = {model_args['learning_rate']}, {n_splits_res}num_epochs = {model_args['num_train_epochs']}, seed = {seed}, acc = {acc_round}, num_outliers = {len(outliers)}({num_outliers_per_cent}%), weight = {weight}" )