Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<create_dataframe>
!pip3 install ktrain==0.2.2
lgb_train = lgb.Dataset(data=X_train, label=y_train)
 lgb_test = lgb.Dataset(data=X_test, label=y_test )<train_model>
from sklearn.model_selection import train_test_split
params = {'task': 'train', 'boosting_type': 'gbdt', 'objective': 'binary', 'metric': 'auc',
'learning_rate': 0.01, 'num_leaves': 48, 'num_iteration': 5000, 'verbose': 0 ,
'colsample_bytree':.8, 'subsample':.9, 'max_depth':7, 'reg_alpha':.1, 'reg_lambda':.1,
'min_split_gain':.01, 'min_child_weight':1}
model = lgb.train(params, lgb_train, valid_sets=lgb_test, early_stopping_rounds=150, verbose_eval=200 )<predict_on_test>
from sklearn.model_selection import train_test_split
lgb_preds = model.predict(df_test )<feature_engineering>
train_df = pd.read_csv(".. /input/nlp-getting-started/train.csv" )
df_test['TARGET'] = lgb_preds<save_to_csv>
random_seed = 12342
x_train, x_val, y_train, y_val = train_test_split(train_df['text'], train_df['target'], shuffle=True, test_size = 0.2, random_state=random_seed, stratify=train_df['target'] )
lgb_pred = df_test[['SK_ID_CURR', 'TARGET']].to_csv('LGB_prediction2.csv', index=False )<import_modules>
import ktrain
from ktrain import text
import pandas as pd
import numpy as np<import_modules>
( x_train_bert, y_train_bert),(x_val_bert, y_val_bert), preproc = text.texts_from_array(x_train=x_train, y_train=y_train,
x_test = x_val, y_test=y_val,
class_names= ["0", "1"],
preprocess_mode='bert',
maxlen=65,
max_features=35000 )
import pandas as pd
import numpy as np<data_type_conversions>
model = text.text_classifier('bert', train_data=(x_train_bert, y_train_bert), preproc=preproc)
 learner = ktrain.get_learner(model, train_data=(x_train_bert, y_train_bert), val_data=(x_val_bert, y_val_bert), batch_size=16 )
def return_size(df):

 return round(sys.getsizeof(df)/ 1e9, 2)
 def convert_types(df):
print(f'Original size of data: {return_size(df)} gb.')
 for c in df:
if df[c].dtype == 'object':
df[c] = df[c].astype('category')
 print(f'New size of data: {return_size(df)} gb.')
 return df<load_from_csv>
learner.lr_find()
app_train = pd.read_csv('.. /input/application_train.csv' ).replace({365243: np.nan})
 app_test = pd.read_csv('.. /input/application_test.csv' ).replace({365243: np.nan})
 bureau = pd.read_csv('.. /input/bureau.csv' ).replace({365243: np.nan})
 bureau_balance = pd.read_csv('.. /input/bureau_balance.csv' ).replace({365243: np.nan})
 app_test['TARGET'] = np.nan
app = app_train.append(app_test, ignore_index = True, sort = True)
 app = convert_types(app)
 bureau = convert_types(bureau)
 bureau_balance = convert_types(bureau_balance)
 gc.enable()
del app_train, app_test
gc.collect()<create_dataframe>
learner.autofit(1e-5 )
def agg_numeric(df, parent_var, df_name):

 for col in df:
if col != parent_var and 'SK_ID' in col:
df = df.drop(columns = col)
 parent_ids = df[parent_var].copy()
numeric_df = df.select_dtypes('number' ).copy()
numeric_df[parent_var] = parent_ids
agg = numeric_df.groupby(parent_var ).agg(['count', 'mean', 'max', 'min', 'sum'])
 columns = []
for var in agg.columns.levels[0]:
if var != parent_var:
for stat in agg.columns.levels[1]:
columns.append('%s_%s_%s' %(df_name, var, stat))
agg.columns = columns
_, idx = np.unique(agg, axis = 1, return_index=True)
 agg = agg.iloc[:, idx]
return agg<create_dataframe>
learner.validate(val_data=(x_val_bert, y_val_bert), class_names=['No Disaster', 'Disaster'] )
def agg_categorical(df, parent_var, df_name):

 categorical = pd.get_dummies(df.select_dtypes('category'))
categorical[parent_var] = df[parent_var]
categorical = categorical.groupby(parent_var ).agg(['sum', 'count', 'mean'])
 column_names = []
for var in categorical.columns.levels[0]:
for stat in ['sum', 'count', 'mean']:
column_names.append('%s_%s_%s' %(df_name, var, stat))
categorical.columns = column_names
_, idx = np.unique(categorical, axis = 1, return_index = True)
 categorical = categorical.iloc[:, idx]
return categorical<merge>
predictor = ktrain.get_predictor(learner.model, preproc )
def agg_child(df, parent_var, df_name):

 df_agg = agg_numeric(df, parent_var, df_name)
 df_agg_cat = agg_categorical(df, parent_var, df_name)
 df_info = df_agg.merge(df_agg_cat, on = parent_var, how = 'outer')
 _, idx = np.unique(df_info, axis = 1, return_index = True)
 df_info = df_info.iloc[:, idx]
gc.enable()
del df_agg, df_agg_cat
gc.collect()
return df_info<create_dataframe>
learner.print_layers()
def agg_grandchild(df, parent_df, parent_var, grandparent_var, df_name):

 parent_df = parent_df[[parent_var, grandparent_var]].copy().set_index(parent_var)
 df_agg = agg_numeric(df, parent_var, '%s_LOAN' % df_name)
 df_agg = df_agg.merge(parent_df,
on = parent_var, how = 'left')
 df_agg_client = agg_numeric(df_agg, grandparent_var, '%s_CLIENT' % df_name)
 if any(df.dtypes == 'category'):
df_agg_cat = agg_categorical(df, parent_var, '%s_LOAN' % df_name)
 df_agg_cat = df_agg_cat.merge(parent_df,
on = parent_var, how = 'left')
 df_agg_cat_client = agg_numeric(df_agg_cat, grandparent_var, '%s_CLIENT' % df_name)
 df_info = df_agg_client.merge(df_agg_cat_client, on = grandparent_var, how = 'outer')
 gc.enable()
del df_agg, df_agg_client, df_agg_cat, df_agg_cat_client
gc.collect()
else:
df_info = df_agg_client.copy()
gc.enable()
del df_agg, df_agg_client
gc.collect()
_, idx = np.unique(df_info, axis = 1, return_index=True)
 df_info = df_info.iloc[:, idx]
return df_info<feature_engineering>
test_df = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 test_df["target"] = predictor.predict(test_df["text"].tolist())
 test_df = test_df[["id", "target"]]
app['LOAN_RATE'] = app['AMT_ANNUITY'] / app['AMT_CREDIT']
app['CREDIT_INCOME_RATIO'] = app['AMT_CREDIT'] / app['AMT_INCOME_TOTAL']
app['EMPLOYED_BIRTH_RATIO'] = app['DAYS_EMPLOYED'] / app['DAYS_BIRTH']
app['EXT_SOURCE_SUM'] = app[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].sum(axis = 1)
 app['EXT_SOURCE_MEAN'] = app[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis = 1)
 app['AMT_REQ_SUM'] = app[[x for x in app.columns if 'AMT_REQ_' in x]].sum(axis = 1 )<feature_engineering>
test_df.to_csv("submisssions.csv", index=False )