Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<save_model>
sns.set_style("darkgrid")
np.save("preds_kernel.npy" , preds )<groupby>
df_train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 print(df_train.shape)
 df_train.head()
test_group = test.groupby('molecule_name' )<normalization>
df_test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 print(df_test.shape)
 df_test.head()
preds = preds.reshape(( -1,max_size, max_size))
out_unscaled = make_outs(test_group, preds )<feature_engineering>
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 )
test['scalar_coupling_constant'] = out_unscaled
coups_to_isolate = ['1JHC', '1JHN', '2JHC', '2JHH', '2JHN', '3JHC', '3JHH', '3JHN']
for i, coup in enumerate(coups_to_isolate):
scale_min = train['scalar_coupling_constant'].loc[train.type == coup].min()
scale_max = train['scalar_coupling_constant'].loc[train.type == coup].max()
scale_mid =(scale_max + scale_min)/2
scale_norm = scale_max - scale_mid
test.loc[test.type == coup, 'scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup]*scale_norm + scale_mid
test.loc[test.type == coup, 'pred_scalar_coupling_constant'] = test['scalar_coupling_constant'].loc[test.type == coup] 
<save_to_csv>
set_stopwords = set(stopwords.words('english'))
df_train['text_processed'] = df_train['text'].apply(lambda x: re.compile(r'https?://\S+|www\.\S+' ).sub(r'',x))
df_test['text_processed'] = df_test['text'].apply(lambda x: re.compile(r'https?://\S+|www\.\S+' ).sub(r'',x))
df_train['text_processed'] = df_train['text_processed'].apply(lambda x: remove_emoji(x))
df_test['text_processed'] = df_test['text_processed'].apply(lambda x: remove_emoji(x))
df_train['text_processed'] = df_train['text_processed'].apply(lambda x: ''.join([c for c in x if c in string.printable]))
df_test['text_processed'] = df_test['text_processed'].apply(lambda x: ''.join([c for c in x if c in string.printable]))
df_train['text_processed'] = df_train['text_processed'].apply(lambda x : ' '.join([word.translate(str.maketrans('', '', string.punctuation)) for word in x.split(' ')]))
df_test['text_processed'] = df_test['text_processed'].apply(lambda x : ' '.join([word.translate(str.maketrans('', '', string.punctuation)) for word in x.split(' ')]))
df_train['text_processed'] = df_train['text_processed'].apply(lambda x: ' '.join([w for w in word_tokenize(x.lower())if w not in set_stopwords]))
df_test['text_processed'] = df_test['text_processed'].apply(lambda x: ' '.join([w for w in word_tokenize(x.lower())if w not in set_stopwords]))
test[['id','scalar_coupling_constant']].to_csv('submission.csv', index=False )<define_variables>
abbreviations = {
"$" : " dollar ",
"€" : " euro ",
"4ao" : "for adults only",
"a.m" : "before midday",
"a3" : "anytime anywhere anyplace",
"aamof" : "as a matter of fact",
"acct" : "account",
"adih" : "another day in hell",
"afaic" : "as far as i am concerned",
"afaict" : "as far as i can tell",
"afaik" : "as far as i know",
"afair" : "as far as i remember",
"afk" : "away from keyboard",
"app" : "application",
"approx" : "approximately",
"apps" : "applications",
"asap" : "as soon as possible",
"asl" : "age, sex, location",
"atk" : "at the keyboard",
"ave." : "avenue",
"aymm" : "are you my mother",
"ayor" : "at your own risk",
"b&b" : "bed and breakfast",
"b+b" : "bed and breakfast",
"b.c" : "before christ",
"b2b" : "business to business",
"b2c" : "business to customer",
"b4" : "before",
"b4n" : "bye for now",
"b@u" : "back at you",
"bae" : "before anyone else",
"bak" : "back at keyboard",
"bbbg" : "bye bye be good",
"bbc" : "british broadcasting corporation",
"bbias" : "be back in a second",
"bbl" : "be back later",
"bbs" : "be back soon",
"be4" : "before",
"bfn" : "bye for now",
"blvd" : "boulevard",
"bout" : "about",
"brb" : "be right back",
"bros" : "brothers",
"brt" : "be right there",
"bsaaw" : "big smile and a wink",
"btw" : "by the way",
"bwl" : "bursting with laughter",
"c/o" : "care of",
"cet" : "central european time",
"cf" : "compare",
"cia" : "central intelligence agency",
"csl" : "can not stop laughing",
"cu" : "see you",
"cul8r" : "see you later",
"cv" : "curriculum vitae",
"cwot" : "complete waste of time",
"cya" : "see you",
"cyt" : "see you tomorrow",
"dae" : "does anyone else",
"dbmib" : "do not bother me i am busy",
"diy" : "do it yourself",
"dm" : "direct message",
"dwh" : "during work hours",
"e123" : "easy as one two three",
"eet" : "eastern european time",
"eg" : "example",
"embm" : "early morning business meeting",
"encl" : "enclosed",
"encl." : "enclosed",
"etc" : "and so on",
"faq" : "frequently asked questions",
"fawc" : "for anyone who cares",
"fb" : "facebook",
"fc" : "fingers crossed",
"fig" : "figure",
"fimh" : "forever in my heart",
"ft." : "feet",
"ft" : "featuring",
"ftl" : "for the loss",
"ftw" : "for the win",
"fwiw" : "for what it is worth",
"fyi" : "for your information",
"g9" : "genius",
"gahoy" : "get a hold of yourself",
"gal" : "get a life",
"gcse" : "general certificate of secondary education",
"gfn" : "gone for now",
"gg" : "good game",
"gl" : "good luck",
"glhf" : "good luck have fun",
"gmt" : "greenwich mean time",
"gmta" : "great minds think alike",
"gn" : "good night",
"g.o.a.t" : "greatest of all time",
"goat" : "greatest of all time",
"goi" : "get over it",
"gps" : "global positioning system",
"gr8" : "great",
"gratz" : "congratulations",
"gyal" : "girl",
"h&c" : "hot and cold",
"hp" : "horsepower",
"hr" : "hour",
"hrh" : "his royal highness",
"ht" : "height",
"ibrb" : "i will be right back",
"ic" : "i see",
"icq" : "i seek you",
"icymi" : "in case you missed it",
"idc" : "i do not care",
"idgadf" : "i do not give a damn fuck",
"idgaf" : "i do not give a fuck",
"idk" : "i do not know",
"ie" : "that is",
"i.e" : "that is",
"ifyp" : "i feel your pain",
"IG" : "instagram",
"iirc" : "if i remember correctly",
"ilu" : "i love you",
"ily" : "i love you",
"imho" : "in my humble opinion",
"imo" : "in my opinion",
"imu" : "i miss you",
"iow" : "in other words",
"irl" : "in real life",
"j4f" : "just for fun",
"jic" : "just in case",
"jk" : "just kidding",
"jsyk" : "just so you know",
"l8r" : "later",
"lb" : "pound",
"lbs" : "pounds",
"ldr" : "long distance relationship",
"lmao" : "laugh my ass off",
"lmfao" : "laugh my fucking ass off",
"lol" : "laughing out loud",
"ltd" : "limited",
"ltns" : "long time no see",
"m8" : "mate",
"mf" : "motherfucker",
"mfs" : "motherfuckers",
"mfw" : "my face when",
"mofo" : "motherfucker",
"mph" : "miles per hour",
"mr" : "mister",
"mrw" : "my reaction when",
"ms" : "miss",
"mte" : "my thoughts exactly",
"nagi" : "not a good idea",
"nbc" : "national broadcasting company",
"nbd" : "not big deal",
"nfs" : "not for sale",
"ngl" : "not going to lie",
"nhs" : "national health service",
"nrn" : "no reply necessary",
"nsfl" : "not safe for life",
"nsfw" : "not safe for work",
"nth" : "nice to have",
"nvr" : "never",
"nyc" : "new york city",
"oc" : "original content",
"og" : "original",
"ohp" : "overhead projector",
"oic" : "oh i see",
"omdb" : "over my dead body",
"omg" : "oh my god",
"omw" : "on my way",
"p.a" : "per annum",
"p.m" : "after midday",
"pm" : "prime minister",
"poc" : "people of color",
"pov" : "point of view",
"pp" : "pages",
"ppl" : "people",
"prw" : "parents are watching",
"ps" : "postscript",
"pt" : "point",
"ptb" : "please text back",
"pto" : "please turn over",
"qpsa" : "what happens",
"ratchet" : "rude",
"rbtl" : "read between the lines",
"rlrt" : "real life retweet",
"rofl" : "rolling on the floor laughing",
"roflol" : "rolling on the floor laughing out loud",
"rotflmao" : "rolling on the floor laughing my ass off",
"rt" : "retweet",
"ruok" : "are you ok",
"sfw" : "safe for work",
"sk8" : "skate",
"smh" : "shake my head",
"sq" : "square",
"srsly" : "seriously",
"ssdd" : "same stuff different day",
"tbh" : "to be honest",
"tbs" : "tablespooful",
"tbsp" : "tablespooful",
"tfw" : "that feeling when",
"thks" : "thank you",
"tho" : "though",
"thx" : "thank you",
"tia" : "thanks in advance",
"til" : "today i learned",
"tl;dr" : "too long i did not read",
"tldr" : "too long i did not read",
"tmb" : "tweet me back",
"tntl" : "trying not to laugh",
"ttyl" : "talk to you later",
"u" : "you",
"u2" : "you too",
"u4e" : "yours for ever",
"utc" : "coordinated universal time",
"w/" : "with",
"w/o" : "without",
"w8" : "wait",
"wassup" : "what is up",
"wb" : "welcome back",
"wtf" : "what the fuck",
"wtg" : "way to go",
"wtpa" : "where the party at",
"wuf" : "where are you from",
"wuzup" : "what is up",
"wywh" : "wish you were here",
"yd" : "yard",
"ygtr" : "you got that right",
"ynk" : "you never know",
"zzz" : "sleeping bored and tired"
}
dtypes = {'atom_index_0':'uint8',
'atom_index_1':'uint8',
'scalar_coupling_constant':'float32',
'num_C':'uint8',
'num_H':'uint8',
'num_N':'uint8',
'num_O':'uint8',
'num_F':'uint8',
'total_atoms':'uint8',
'num_bonds':'uint8',
'num_mol_bonds':'uint8',
'min_d':'float32',
'mean_d':'float32',
'max_d':'float32',
'space_dr':'float32',
'bond_dr':'float32',
'bond_1':'uint8',
'bond_2':'uint8',
'bond_3':'uint8',
'atom_0_pc':'float32',
'atom_end_pc':'float32',
'atom_2_hyb':'uint8',
'atom_3_hyb':'uint8',
'atom_end_hyb':'uint8',
'path_count':'uint8',
'atom_0_min':'float32',
'atom_0_mean':'float32',
'atom_0_max':'float32',
'atom_0_Cmin':'float32',
'atom_0_Cmean':'float32',
'atom_0_Cmax':'float32',
'atom_0_Omin':'float32',
'atom_0_Omean':'float32',
'atom_0_Omax':'float32',
'atom_0_Nmin':'float32',
'atom_0_Nmean':'float32',
'atom_0_Nmax':'float32',
'atom_0_Fmin':'float32',
'atom_0_Fmean':'float32',
'atom_0_Fmax':'float32',
'atom_end_min':'float32',
'atom_end_mean':'float32',
'atom_end_max':'float32',
'atom_end_Cmin':'float32',
'atom_end_Cmean':'float32',
'atom_end_Cmax':'float32',
'atom_end_Omin':'float32',
'atom_end_Omean':'float32',
'atom_end_Omax':'float32',
'atom_end_Nmin':'float32',
'atom_end_Nmean':'float32',
'atom_end_Nmax':'float32',
'atom_end_Fmin':'float32',
'atom_end_Fmean':'float32',
'atom_end_Fmax':'float32',
'Dmin_COM':'float32',
'Dmean_COM':'float32',
'Dmax_COM':'float32',
'COM_dr_0': 'float32',
'COM_dr_1': 'float32',
'bond2_angle': 'float32',
'bond3_angle': 'float32'
}<load_from_csv>
def convert_abbrev(word):
return abbreviations[word.lower() ] if word.lower() in abbreviations.keys() else word
df_train['text_processed'] = df_train['text_processed'].apply(lambda x: ' '.join([convert_abbrev(word)for word in word_tokenize(x)]))
df_test['text_processed'] = df_test['text_processed'].apply(lambda x: ' '.join([convert_abbrev(word)for word in word_tokenize(x)]))
train = pd.read_csv(".. /input/predmolprop-featureengineering-final/train_extend.csv",dtype=dtypes)
 test = pd.read_csv(".. /input/predmolprop-featureengineering-finaltest/test_extend.csv",dtype=dtypes )<categorify>
ids_with_target_error = [328,443,513,2619,3640,3900,4342,5781,6552,6554,6570,6701,6702,6729,6861,7226]
df_train.loc[df_train['id'].isin(ids_with_target_error),'target'] = 0
cols = ['atom_0_type2','atom_2_type','atom_3_type','atom_end_type2']
for col in cols:
enc = LabelEncoder()
train[col]=enc.fit_transform(train[col] ).astype(np.uint8)
 test[col]=enc.transform(test[col] ).astype(np.uint8)
 del cols<define_variables>
learning_rate = 1e-5
valid = 0.2
epochs_num = 3
batch_size_num = 16
prefix_train= ['id', 'type', 'scalar_coupling_constant']
prefix_test= ['id', 'type']
fc_distance = ['space_dr','min_d','mean_d', 'max_d']
fc_COM = ['Dmin_COM', 'Dmean_COM', 'Dmax_COM']
fc_size = ['num_mol_bonds', 'total_atoms','num_C', 'num_H', 'num_N', 'num_O', 'num_F']
fc_atom_0 = ['atom_0_pc','atom_0_type2','COM_dr_0',
'atom_0_min','atom_0_mean', 'atom_0_max',
'atom_0_Cmin', 'atom_0_Cmean','atom_0_Cmax',
'atom_0_Nmin', 'atom_0_Nmean', 'atom_0_Nmax',
'atom_0_Omin', 'atom_0_Omean','atom_0_Omax',
'atom_0_Fmin', 'atom_0_Fmean', 'atom_0_Fmax']
fc_atom_end = ['atom_end_pc', 'atom_end_type2', 'COM_dr_1',
'atom_end_min','atom_end_mean', 'atom_end_max',
'atom_end_Cmin', 'atom_end_Cmean','atom_end_Cmax',
'atom_end_Nmin', 'atom_end_Nmean', 'atom_end_Nmax',
'atom_end_Omin', 'atom_end_Omean','atom_end_Omax',
'atom_end_Fmin', 'atom_end_Fmean', 'atom_end_Fmax']
fc_2 = ['bond_dr','path_count','atom_2_type','bond_2','bond2_angle']
fc_3 = ['atom_3_type','bond_3','bond3_angle']
fc1 = set(fc_distance+fc_COM+fc_size+fc_atom_0+fc_atom_end)
 train = train[prefix_train+fc_distance+fc_COM+fc_size+fc_atom_0+fc_atom_end+fc_2+fc_3]
test = test[prefix_test+fc_distance+fc_COM+fc_size+fc_atom_0+fc_atom_end+fc_2+fc_3]
len(train.columns )<split>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
def ProcessData(df,features,test_size=0.25):
if test_size == 0:
train_Y = df.pop('scalar_coupling_constant')
 train_type = df.pop('type')
 df.pop('id')
 return df.loc[:,df.columns.map(lambda x: x in features)], train_Y, train_type
train_X, val_X, train_Y, val_Y = train_test_split(df.loc[:,df.columns.map(lambda x: x in features or x=='type')],
df.scalar_coupling_constant,test_size=test_size,random_state=42)
 train_type = train_X.pop('type')
 val_type = val_X.pop('type')
 return train_X, train_Y, train_type, val_X, val_Y, val_type<compute_test_metric>
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
def CalcLMAE(y_true, y_pred, groups, floor=1e-9):
maes =(y_true-y_pred ).abs().groupby(groups ).mean()
return np.log(maes.map(lambda x: max(x, floor)) ).mean()<categorify>
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 )
def SingleRun(df,features, test_size=0.25, model_fn=XGBRegressor, includeType=False, early_stopping_rounds=None, do_SHAP=False, **kwargs):
data = ProcessData(df,features,test_size)
 if(test_size==0):
train_X,train_Y,train_type = data
else:
train_X,train_Y,train_type,val_X,val_Y,val_type = data
if includeType:
train_X=train_X.join(train_type)
 val_X=val_X.join(val_type)
 enc = LabelEncoder()
train_X.type=enc.fit_transform(train_X.type ).astype(np.uint8)
 val_X.type=enc.transform(val_X.type ).astype(np.uint8)
 model = model_fn(**kwargs)
 t1=time.time()
if early_stopping_rounds is None:
model.fit(train_X,train_Y, verbose=False)
 else:
if test_size==0:
print('ERROR: need test data for early_stopping_rounds')
 return -1,-1,-1
model.fit(train_X,train_Y, early_stopping_rounds=5, eval_set=[(val_X, val_Y)], verbose=False)
 print('best LMAE:',log(model.best_score))
print('best ntree:', model.best_ntree_limit)
 t2=time.time()
print('training time:',t2-t1)
 train_predict = pd.Series(model.predict(train_X),index=train_X.index)
 train_LMAE = CalcLMAE(train_Y,train_predict,train_type)
 print('\ttrain LMAE:',train_LMAE)
 if(test_size==0):
val_LMAE = None
plt.figure(figsize=[6,6])
 sns.scatterplot(x=train_Y, y=train_predict)
 plt.plot(train_Y,train_Y,color='black')
 plt.title(train_type.values[0])
 plt.xlabel('scalar coupling constant')
 plt.ylabel('predicted value')
 plt.show()
if do_SHAP:
samp = train_X.sample(200)
 shap.summary_plot(model.get_booster().predict(xgboost.DMatrix(samp),pred_contribs=True)[:,:-1], samp)
 else:
val_predict = pd.Series(model.predict(val_X),index=val_X.index)
 val_LMAE = CalcLMAE(val_Y,val_predict,val_type)
 print('\tval LMAE:',val_LMAE)
 plt.figure(figsize=[6,6])
 sns.scatterplot(x=val_Y, y=val_predict)
 plt.plot(val_Y,val_Y,color='black')
 plt.title(train_type.values[0])
 plt.xlabel('scalar coupling constant')
 plt.ylabel('predicted value')
 plt.show()
if do_SHAP:
samp = val_X.sample(200)
 shap.summary_plot(model.get_booster().predict(xgboost.DMatrix(samp),pred_contribs=True)[:,:-1], samp)
 gc.collect()
return model, train_LMAE, val_LMAE<define_search_model>
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=learning_rate), loss='binary_crossentropy', metrics=['accuracy'])
 return model
coupling_type = '3JHN'
train_sample = train[train.type==coupling_type]
features=fc1.copy()
if(coupling_type[0]=='2'):
features.update(fc_2)
 elif(coupling_type[0]=='3'):
features.update(fc_2[:-1]+fc_3)
 model_3JHN,_,_=SingleRun(train_sample,features,test_size=0.2,model_fn=XGBRegressor,includeType=False,early_stopping_rounds=5,
do_SHAP = True,
max_depth=11, learning_rate=0.1, n_estimators=3000,
verbosity=1,
objective='reg:squarederror', booster='gbtree',tree_method= 'gpu_hist',
n_jobs=4,
gamma=0, min_child_weight=1, max_delta_step=0,
subsample=1,colsample_bytree=1, colsample_bylevel=1, colsample_bynode=1,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5,
random_state=0, seed=None, missing=None, importance_type='gain', eval_metric='mae' )<load_pretrained>
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
def RunByType(df,test_size=0.25,model_fn=XGBRegressor,includeType=False,early_stopping_rounds=None,**kwargs):
model_dict={}
train_LMAE_dict={}
val_LMAE_dict={}
for coupling_type in coupling_types:
print('Now training type:',str(coupling_type))
df_type = df[df['type']==coupling_type]
features=fc1.copy()
if(coupling_type[0]=='2'):
features.update(fc_2)
 elif(coupling_type[0]=='3'):
features.update(fc_2[:-1]+fc_3)
 model,train_LMAE,val_LMAE = SingleRun(df_type,features,test_size,model_fn=model_fn,includeType=False,early_stopping_rounds=early_stopping_rounds,**kwargs)
 model_dict[coupling_type]=model
train_LMAE_dict[coupling_type]=train_LMAE
val_LMAE_dict[coupling_type]=val_LMAE
train_LMAE = sum(train_LMAE_dict.values())/len(coupling_types)
 if test_size>0:
val_LMAE = sum(val_LMAE_dict.values())/len(coupling_types)
 else:
val_LMAE=None
print('total train LMAE:',train_LMAE)
 print('total val LMAE', val_LMAE)
 gc.collect()
return model_dict<predict_on_test>
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 )
def PredictByType(df_X,model_dict,df_Y=None):
predictions = pd.DataFrame()
for coupling_type in coupling_types:
print('predicting type:',str(coupling_type))
model = model_dict[coupling_type]
df_type = df_X[df_X['type']==coupling_type]
features=fc1.copy()
if(coupling_type[0]=='2'):
features.update(fc_2)
 elif(coupling_type[0]=='3'):
features.update(fc_2[:-1]+fc_3)
 predict = model.predict(df_type.loc[:,df_X.columns.map(lambda col: col in features)])
 predict = pd.Series(predict,index=df_type.index)
 predict = pd.DataFrame({'id':df_type.id,'scalar_coupling_constant': predict})
 predict.set_index('id',inplace=True)
 predictions = pd.concat([predictions,predict])
 if df_Y is not None:
predictions.columns = ['predictions']
merged = pd.merge(train,predictions,how='left',left_on='id',right_index=True)
 LMAE = CalcLMAE(df_Y, merged.predictions,merged.type)
 print('LMAE:', LMAE)
 g = sns.FacetGrid(merged,col="type", col_order = coupling_types,sharex=False,sharey=False)
 g.map(sns.scatterplot, "scalar_coupling_constant","predictions")
 del merged
gc.collect()
return predictions<choose_model_class>
train_input = bert_encode(df_train['text_processed'].values, tokenizer, max_len=160)
 test_input = bert_encode(df_test['text_processed'].values, tokenizer, max_len=160)
 train_labels = df_train['target'].values
model_dict=RunByType(train,test_size=0.2,includeType=False,early_stopping_rounds=5,
max_depth=11, learning_rate=0.1, n_estimators=10000,
verbosity=1,
objective='reg:squarederror', booster='gbtree',tree_method= 'gpu_hist',
n_jobs=4,
gamma=0, min_child_weight=1, max_delta_step=0,
subsample=1,colsample_bytree=1, colsample_bylevel=1, colsample_bynode=1,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5,
random_state=0, seed=None, missing=None, importance_type='gain', eval_metric='mae')
 test_predict = PredictByType(test,model_dict)
 test_predict.to_csv('test_predict_TypeModel.csv' )<train_model>
model_BERT = build_model(bert_layer, max_len=160)
 model_BERT.summary()
print('FINISHED!' )<load_from_csv>
checkpoint = ModelCheckpoint('model_BERT.h5', monitor='val_loss', save_best_only=True)
 train_history = model_BERT.fit(
 train_input, train_labels,
validation_split = valid,
epochs = epochs_num,
callbacks=[checkpoint],
batch_size = batch_size_num
)
one = pd.read_csv('.. /input/champs-blending-tutorial/1.csv')
 two = pd.read_csv('.. /input/champs-blending-tutorial/2.csv')
 three = pd.read_csv('.. /input/champs-blending-tutorial/3.csv')
 four = pd.read_csv('.. /input/otherkernelsadded/submission-2.csv')
 five = pd.read_csv('.. /input/otherkernelsadded/submission-giba-1.csv')
 six = pd.read_csv('.. /input/otherkernelsadded/workingsubmission-test.csv')
 seven = pd.read_csv('.. /input/otherkernelsadded/LGB_2019-07-18_-1.2243.csv')
 submission = pd.DataFrame()
submission['id'] = one.id
submission['scalar_coupling_constant'] =(0.40*one.scalar_coupling_constant)+ \ 
(0.29*two.scalar_coupling_constant)+ \ 
(0.19*three.scalar_coupling_constant)+ \ 
(0.04*five.scalar_coupling_constant)+ \ 
(0.04*six.scalar_coupling_constant)+ \ 
(0.04*seven.scalar_coupling_constant)
 submission.to_csv('my_blend_1.csv', index=False )<set_options>
test_pred = model_BERT.predict(test_input)
 test_pred_int = test_pred.round().astype('int')
 train_pred = model_BERT.predict(train_input)
 train_pred_int = train_pred.round().astype('int' )
warnings.filterwarnings("ignore")
 warnings.filterwarnings(action="ignore",category=DeprecationWarning)
 warnings.filterwarnings(action="ignore",category=FutureWarning )<compute_train_metric>
print("F1 Score = " + str(f1_score(df_train['target'], train_pred_int)) )
<load_from_csv><EOS> 
df_submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 df_submission['target'] = test_pred_int
df_submission.to_csv("submission.csv", index=False, header=True )