Kaggle competition: Categorical Feature Encoding Challenge
temp = {}
for i in multi_method:
if i in preds.keys() :
temp['Val'] = temp.get('Val', [])+ [np.mean(multi_method[i]['Val'])]
temp['ValC'] = temp.get('ValC', [])+ [np.mean(multi_method[i]['ValC'])]
print("At same cutoff:
Best val score: {:6.3f} @ 70 Conf
\
Best Val score: {:6.3f} @ Pred Conf".format(np.mean(temp['Val']), np.mean(temp['ValC'])) )<save_to_csv>
train = pd.read_csv('/kaggle/input/cat-in-the-dat/train.csv')
 test = pd.read_csv('/kaggle/input/cat-in-the-dat/test.csv' )
mean = np.mean(np.stack(pd.DataFrame(preds ).iloc[0].values), 0)
 upper = np.max(np.stack(pd.DataFrame(preds ).iloc[1].values), 0)
 lower = np.min(np.stack(pd.DataFrame(preds ).iloc[2].values), 0)
 sub['FVC'] = mean
sub['Confidence'] = upper - lower
sub[['Patient_Week', 'FVC', 'Confidence']].to_csv("submission.csv", index=False)
 sub.head()<load_from_csv>
train.nunique()
submission_1 = pd.read_csv(".. /input/sub19042/submission_19042.csv")
 submission_2 = pd.read_csv(".. /input/submission-v3/submission.csv")
 submission_3 = pd.read_csv(".. /input/blend2/submission.csv")
 submission_4 = pd.read_csv(".. /input/blend3/submission.csv")
 submission_5 = pd.read_csv(".. /input/blend4/submission.csv" )<create_dataframe>
train.isna().sum()
submission = pd.DataFrame.from_dict({
'id': submission_1['id'],
'prediction':(submission_1.prediction.values * 0.5)+(submission_2.prediction.values * 0.3)+(submission_3.prediction.values * 0.125)+(submission_4.prediction.values * 0.0375)+(submission_5.prediction.values * 0.0375)
 } )<save_to_csv>
bin_cols = [col for col in train.columns.values if col.startswith('bin')]
nom_cols = [col for col in train.columns.values if col.startswith('nom')]
ord_cols = [col for col in train.columns.values if col.startswith('ord')]
submission.to_csv('submission.csv', index=False )<save_to_csv>
def ord_to_num(df, col):
keys=np.sort(df[col].unique())
 values=np.arange(len(keys))
map = dict(zip(keys, values))
df[col] = df[col].replace(map )
submission.to_csv('submission.csv', index=False )<feature_engineering>
ord_to_num(test, 'ord_3')
 ord_to_num(train, 'ord_3' )
np.random.seed(32)
 os.environ["OMP_NUM_THREADS"] = "4" 
<load_from_csv>
ord_to_num(train, 'ord_4')
 ord_to_num(test,'ord_4' )
train = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv",
usecols= ['id','target','comment_text'])
 test = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv")
<feature_engineering>
ord_to_num(train, 'ord_5')
 ord_to_num(test,'ord_5' )
train['target'] = np.where(train['target'] > 0.5, 1, 0)
 train['comment_text'] = train.comment_text.apply(lambda x: x.lower() )<split>
values_ord_1=[3,4,0,1,2]
y = train.target.values
X_train, X_valid, Y_train, Y_valid = train_test_split(train[['comment_text']], y, test_size = 0.1 )<define_variables>
map_ord_1 = dict(zip(keys_ord_1, values_ord_1))
map_ord_1
embed_size = 300
max_features = 200000
max_len = 220<feature_engineering>
train['ord_1'] = train['ord_1'].replace(map_ord_1)
 test['ord_1'] = test['ord_1'].replace(map_ord_1 )
tk = Tokenizer(num_words=max_features, lower = True)
 tk.fit_on_texts(X_train['comment_text'].values)
 X_train["comment_seq"] = tk.texts_to_sequences(X_train['comment_text'].values)
 X_valid["comment_seq"] = tk.texts_to_sequences(X_valid['comment_text'].values)
 test["comment_seq"] = tk.texts_to_sequences(test['comment_text'].values )<string_transform>
values_ord_2=[1,3,5,4,0,2]
%%time
X_train_p = pad_sequences(X_train.comment_seq, maxlen = max_len)
 X_valid_p = pad_sequences(X_valid.comment_seq, maxlen = max_len)
 test_p = pad_sequences(test.comment_seq, maxlen = max_len)
 del X_train, X_valid, test, train<feature_engineering>
map_ord_2 = dict(zip(keys_ord_2, values_ord_2))
map_ord_2
embedding_path = ".. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec"
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embedding_index = dict(get_coefs(*o.strip().split(" ")) for o in open(embedding_path))
word_index = tk.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embedding_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector<import_modules>
train['ord_2'] = train['ord_2'].replace(map_ord_2)
 test['ord_2'] = test['ord_2'].replace(map_ord_2 )
warnings.filterwarnings('ignore' )<choose_model_class>
train[ord_cols].nunique()
def build_model(lr = 0.0, lr_d = 0.0, units = 0, dr = 0.0):
inp = Input(shape =(max_len,))
x = Embedding(max_features, embed_size, weights = [embedding_matrix], trainable = False )(inp)
 x = Bidirectional(CuDNNLSTM(units, return_sequences=True))(x)
 x = Bidirectional(CuDNNLSTM(units, return_sequences=True))(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 conc = concatenate([avg_pool, max_pool])
 output = Dropout(0.75 )(conc)
 output = Dense(units=110 )(output)
 output = LeakyReLU(alpha=0.3 )(output)
 output = Dense(units=55 )(output)
 output = Activation('relu' )(output)
 prediction = Dense(1, activation = "sigmoid" )(output)
 model = Model(inputs = inp, outputs = prediction)
 model.compile(loss = "binary_crossentropy", optimizer = Adam(lr = lr, decay = lr_d), metrics = ["accuracy"])
 history = model.fit(X_train_p, Y_train, batch_size = 1024, epochs = 6, validation_data =(X_valid_p, Y_valid),
verbose = 1 , callbacks = [check_point])
 model = load_model(file_path)
 return model<choose_model_class>
train['ord_4_band'] = pd.qcut(train['ord_4'], 6)
 bands=train.ord_4_band.unique()
keys_bands=np.sort(bands)
 values_bands=np.arange(len(keys_bands))
map_bands = dict(zip(keys_bands, values_bands))
model = build_model(lr = 1e-3, lr_d = 0, units = 128, dr = 0.3 )<predict_on_test>
train['ord_4_band'] = train['ord_4_band'].replace(map_bands)
 test['ord_4_band']=pd.cut(test.ord_4,pd.IntervalIndex(keys_bands))
pred = model.predict(test_p, batch_size = 1024, verbose = 1 )<save_to_csv>
test['ord_4_band'] = test['ord_4_band'].replace(map_bands)
 test.ord_4_band.head()
submission = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/sample_submission.csv")
 submission['prediction'] = pred
submission.to_csv("submission.csv", index = False)
 submission.head(10 )<import_modules>
train['ord_5_band'] = pd.qcut(train['ord_5'], 6)
 bands=train.ord_5_band.unique()
keys_bands=np.sort(bands)
 values_bands=np.arange(len(keys_bands))
map_bands = dict(zip(keys_bands, values_bands))
import gc
import re
import operator
import numpy as np
import pandas as pd
from gensim.models import KeyedVectors
from sklearn import model_selection
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, Input, Dense, CuDNNGRU,concatenate, Bidirectional, SpatialDropout1D, Conv1D, GlobalAveragePooling1D, GlobalMaxPooling1D
from keras.optimizers import RMSprop, Adam
from keras.models import Model
from keras.callbacks import EarlyStopping
import seaborn as sns<load_from_csv>
train['ord_5_band'] = train['ord_5_band'].replace(map_bands)
 test['ord_5_band']=pd.cut(test.ord_5,pd.IntervalIndex(keys_bands))
train = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv")
 test = pd.read_csv(".. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv")
 print("Train shape : ",train.shape)
 print("Test shape : ",test.shape)
 train.head() 
<concatenate>
test['ord_5_band'] = test['ord_5_band'].replace(map_bands)
 test.ord_5_band.head()
df = pd.concat([train[['id','comment_text']], test], axis=0)
 del(train, test)
 gc.collect()<load_pretrained>
train[nom_cols].nunique()
ft_common_crawl = '.. /input/fasttext-crawl-300d-2m/crawl-300d-2M.vec'
embeddings_index = KeyedVectors.load_word2vec_format(ft_common_crawl )<set_options>
test[nom_cols].nunique()
gc.collect()<feature_engineering>
train[nom_cols].nunique()
def build_vocab(texts):
sentences = texts.apply(lambda x: x.split() ).values
vocab = {}
for sentence in sentences:
for word in sentence:
try:
vocab[word] += 1
except KeyError:
vocab[word] = 1
return vocab<categorify>
test[nom_cols].nunique()
def check_coverage(vocab, embeddings_index):
known_words = {}
unknown_words = {}
nb_known_words = 0
nb_unknown_words = 0
for word in vocab.keys() :
try:
known_words[word] = embeddings_index[word]
nb_known_words += vocab[word]
except:
unknown_words[word] = vocab[word]
nb_unknown_words += vocab[word]
pass
print('Found embeddings for {:.3%} of vocab'.format(len(known_words)/ len(vocab)))
 print('Found embeddings for {:.3%} of all text'.format(nb_known_words /(nb_known_words + nb_unknown_words)))
 unknown_words = sorted(unknown_words.items() , key=operator.itemgetter(1)) [::-1]
return unknown_words<feature_engineering>
train[train.nom_7=='x']
df['comment_text'] = df['comment_text'].apply(lambda x: x.lower())
 gc.collect()<compute_train_metric>
train=train[train.nom_7!='x']
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
 oov[:10]<set_options>
train[train.nom_7=='x']
gc.collect()<define_variables>
labelEnc=LabelEncoder()
contraction_mapping = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "couldn't": "could not", "didn't": "did not", "doesn't": "does not", "don't": "do not", "hadn't": "had not", "hasn't": "has not", "haven't": "have not", "he'd": "he would","he'll": "he will", "he's": "he is", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have" }<set_options>
for col in nom_cols:
train[col]=labelEnc.fit_transform(train[col])
 test[col]=labelEnc.fit_transform(test[col])
del(vocab,oov)
 gc.collect()<define_variables>
for col in ['bin_3', 'bin_4']:
train[col]=labelEnc.fit_transform(train[col])
 test[col]=labelEnc.fit_transform(test[col])
def known_contractions(embed):
known = []
for contract in contraction_mapping:
if contract in embed:
known.append(contract)
 return known<string_transform>
X_temp=train.drop('target', axis=1)
 Y=train.target
def clean_contractions(text, mapping):
specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text<drop_column>
from sklearn.mixture import GaussianMixture
df['comment_text'] = df['comment_text'].apply(lambda x: clean_contractions(x, contraction_mapping))<compute_train_metric>
gm = GaussianMixture(n_components=4)
 gm.fit(X_temp )
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
 oov[:10]<set_options>
X_temp['Gaussian_Mixture']=gm.predict(X_temp )
del(vocab,oov)
 gc.collect()<drop_column>
test['Gaussian_Mixture']=gm.predict(test )
def unknown_punct(embed, punct):
unknown = ''
for p in punct:
if p not in embed:
unknown += p
unknown += ' '
return unknown<define_variables>
X_oh_temp=pd.get_dummies(X_temp.drop('id', axis=1), columns=X_temp.drop('id', axis=1 ).columns, drop_first=True, sparse=True )
punct_mapping = {"_":" ", "`":" "}<categorify>
X_oh=scipy.sparse.csr_matrix(X_oh_temp.values )
def clean_special_chars(text, punct, mapping):
for p in mapping:
text = text.replace(p, mapping[p])
 for p in punct:
text = text.replace(p, f' {p} ')
 return text<feature_engineering>
test_oh_temp=pd.get_dummies(test.drop('id', axis=1), columns=test.drop('id', axis=1 ).columns, drop_first=True, sparse=True )
df['comment_text'] = df['comment_text'].apply(lambda x: clean_special_chars(x, punct, punct_mapping))<compute_train_metric>
test_oh=scipy.sparse.csr_matrix(test_oh_temp.values )
vocab = build_vocab(df['comment_text'])
 oov = check_coverage(vocab, embeddings_index)
<set_options>
X_train, X_valid, Y_train, Y_valid = train_test_split(X_oh,
Y,
test_size = 0.20,
random_state=42)
del(vocab,oov)
 gc.collect()<define_variables>
lr=LogisticRegression(C=0.125, solver="lbfgs", max_iter=500)
 lr.fit(X_train, Y_train)
swear_words = [
' 4r5e ',
' 5h1t ',
' 5hit ',
' a55 ',
' anal ',
' anus ',
' ar5e ',
' arrse ',
' arse ',
' ass ',
' ass-fucker ',
' asses ',
' assfucker ',
' assfukka ',
' asshole ',
' assholes ',
' asswhole ',
' a_s_s ',
' b!tch ',
' b00bs ',
' b17ch ',
' b1tch ',
' ballbag ',
' balls ',
' ballsack ',
' bastard ',
' beastial ',
' beastiality ',
' bellend ',
' bestial ',
' bestiality ',
' biatch ',
' bitch ',
' bitcher ',
' bitchers ',
' bitches ',
' bitchin ',
' bitching ',
' bloody ',
' blow job ',
' blowjob ',
' blowjobs ',
' boiolas ',
' bollock ',
' bollok ',
' boner ',
' boob ',
' boobs ',
' booobs ',
' boooobs ',
' booooobs ',
' booooooobs ',
' breasts ',
' buceta ',
' bugger ',
' bum ',
' bunny fucker ',
' butt ',
' butthole ',
' buttmuch ',
' buttplug ',
' c0ck ',
' c0cksucker ',
' carpet muncher ',
' cawk ',
' chink ',
' cipa ',
' cl1t ',
' clit ',
' clitoris ',
' clits ',
' cnut ',
' cock ',
' cock-sucker ',
' cockface ',
' cockhead ',
' cockmunch ',
' cockmuncher ',
' cocks ',
' cocksuck ',
' cocksucked ',
' cocksucker ',
' cocksucking ',
' cocksucks ',
' cocksuka ',
' cocksukka ',
' cok ',
' cokmuncher ',
' coksucka ',
' coon ',
' cox ',
' crap ',
' cum ',
' cummer ',
' cumming ',
' cums ',
' cumshot ',
' cunilingus ',
' cunillingus ',
' cunnilingus ',
' cunt ',
' cuntlick ',
' cuntlicker ',
' cuntlicking ',
' cunts ',
' cyalis ',
' cyberfuc ',
' cyberfuck ',
' cyberfucked ',
' cyberfucker ',
' cyberfuckers ',
' cyberfucking ',
' d1ck ',
' damn ',
' dick ',
' dickhead ',
' dildo ',
' dildos ',
' dink ',
' dinks ',
' dirsa ',
' dlck ',
' dog-fucker ',
' doggin ',
' dogging ',
' donkeyribber ',
' doosh ',
' duche ',
' dyke ',
' ejaculate ',
' ejaculated ',
' ejaculates ',
' ejaculating ',
' ejaculatings ',
' ejaculation ',
' ejakulate ',
' f u c k ',
' f u c k e r ',
' f4nny ',
' fag ',
' fagging ',
' faggitt ',
' faggot ',
' faggs ',
' fagot ',
' fagots ',
' fags ',
' fanny ',
' fannyflaps ',
' fannyfucker ',
' fanyy ',
' fatass ',
' fcuk ',
' fcuker ',
' fcuking ',
' feck ',
' fecker ',
' felching ',
' fellate ',
' fellatio ',
' fingerfuck ',
' fingerfucked ',
' fingerfucker ',
' fingerfuckers ',
' fingerfucking ',
' fingerfucks ',
' fistfuck ',
' fistfucked ',
' fistfucker ',
' fistfuckers ',
' fistfucking ',
' fistfuckings ',
' fistfucks ',
' flange ',
' fook ',
' fooker ',
' fuck ',
' fucka ',
' fucked ',
' fucker ',
' fuckers ',
' fuckhead ',
' fuckheads ',
' fuckin ',
' fucking ',
' fuckings ',
' fuckingshitmotherfucker ',
' fuckme ',
' fucks ',
' fuckwhit ',
' fuckwit ',
' fudge packer ',
' fudgepacker ',
' fuk ',
' fuker ',
' fukker ',
' fukkin ',
' fuks ',
' fukwhit ',
' fukwit ',
' fux ',
' fux0r ',
' f_u_c_k ',
' gangbang ',
' gangbanged ',
' gangbangs ',
' gaylord ',
' gaysex ',
' goatse ',
' God ',
' god-dam ',
' god-damned ',
' goddamn ',
' goddamned ',
' hardcoresex ',
' hell ',
' heshe ',
' hoar ',
' hoare ',
' hoer ',
' homo ',
' hore ',
' horniest ',
' horny ',
' hotsex ',
' jack-off ',
' jackoff ',
' jap ',
' jerk-off ',
' jism ',
' jiz ',
' jizm ',
' jizz ',
' kawk ',
' knob ',
' knobead ',
' knobed ',
' knobend ',
' knobhead ',
' knobjocky ',
' knobjokey ',
' kock ',
' kondum ',
' kondums ',
' kum ',
' kummer ',
' kumming ',
' kums ',
' kunilingus ',
' l3itch ',
' labia ',
' lmfao ',
' lust ',
' lusting ',
' m0f0 ',
' m0fo ',
' m45terbate ',
' ma5terb8 ',
' ma5terbate ',
' masochist ',
' master-bate ',
' masterb8 ',
' masterbat3 ',
' masterbate ',
' masterbation ',
' masterbations ',
' masturbate ',
' mo-fo ',
' mof0 ',
' mofo ',
' mothafuck ',
' mothafucka ',
' mothafuckas ',
' mothafuckaz ',
' mothafucked ',
' mothafucker ',
' mothafuckers ',
' mothafuckin ',
' mothafucking ',
' mothafuckings ',
' mothafucks ',
' mother fucker ',
' motherfuck ',
' motherfucked ',
' motherfucker ',
' motherfuckers ',
' motherfuckin ',
' motherfucking ',
' motherfuckings ',
' motherfuckka ',
' motherfucks ',
' muff ',
' mutha ',
' muthafecker ',
' muthafuckker ',
' muther ',
' mutherfucker ',
' n1gga ',
' n1gger ',
' nazi ',
' nigg3r ',
' nigg4h ',
' nigga ',
' niggah ',
' niggas ',
' niggaz ',
' nigger ',
' niggers ',
' nob ',
' nob jokey ',
' nobhead ',
' nobjocky ',
' nobjokey ',
' numbnuts ',
' nutsack ',
' orgasim ',
' orgasims ',
' orgasm ',
' orgasms ',
' p0rn ',
' pawn ',
' pecker ',
' penis ',
' penisfucker ',
' phonesex ',
' phuck ',
' phuk ',
' phuked ',
' phuking ',
' phukked ',
' phukking ',
' phuks ',
' phuq ',
' pigfucker ',
' pimpis ',
' piss ',
' pissed ',
' pisser ',
' pissers ',
' pisses ',
' pissflaps ',
' pissin ',
' pissing ',
' pissoff ',
' poop ',
' porn ',
' porno ',
' pornography ',
' pornos ',
' prick ',
' pricks ',
' pron ',
' pube ',
' pusse ',
' pussi ',
' pussies ',
' pussy ',
' pussys ',
' rectum ',
' retard ',
' rimjaw ',
' rimming ',
' s hit ',
' s.o.b.',
' sadist ',
' schlong ',
' screwing ',
' scroat ',
' scrote ',
' scrotum ',
' semen ',
' sex ',
' sh!t ',
' sh1t ',
' shag ',
' shagger ',
' shaggin ',
' shagging ',
' shemale ',
' shit ',
' shitdick ',
' shite ',
' shited ',
' shitey ',
' shitfuck ',
' shitfull ',
' shithead ',
' shiting ',
' shitings ',
' shits ',
' shitted ',
' shitter ',
' shitters ',
' shitting ',
' shittings ',
' shitty ',
' skank ',
' slut ',
' sluts ',
' smegma ',
' smut ',
' snatch ',
' son-of-a-bitch ',
' spac ',
' spunk ',
' s_h_i_t ',
' t1tt1e5 ',
' t1tties ',
' teets ',
' teez ',
' testical ',
' testicle ',
' tit ',
' titfuck ',
' tits ',
' titt ',
' tittie5 ',
' tittiefucker ',
' titties ',
' tittyfuck ',
' tittywank ',
' titwank ',
' tosser ',
' turd ',
' tw4t ',
' twat ',
' twathead ',
' twatty ',
' twunt ',
' twunter ',
' v14gra ',
' v1gra ',
' vagina ',
' viagra ',
' vulva ',
' w00se ',
' wang ',
' wank ',
' wanker ',
' wanky ',
' whoar ',
' whore ',
' willies ',
' willy ',
' xrated ',
' xxx '
]<string_transform>
y_pred_lr=lr.predict_proba(X_valid )
replace_with_fuck = []
for swear in swear_words:
if swear[1:(len(swear)-1)] not in embeddings_index:
replace_with_fuck.append(swear)
 replace_with_fuck = '|'.join(replace_with_fuck)
 replace_with_fuck 
<categorify>
roc_auc_score(Y_valid.values, y_pred_lr[:,1] )
def handle_swears(text):
text = re.sub(replace_with_fuck, ' fuck ', text)
 return text<feature_engineering>
lr.fit(X_oh, Y )
df['comment_text'] = df['comment_text'].apply(lambda x: handle_swears(x))
gc.collect()<split>
y_pred=lr.predict_proba(X_oh)
 roc_auc_score(Y.values, y_pred[:,1] )
train = df.iloc[:1804874,:]
test = df.iloc[1804874:,:]
train.head()<set_options>
test_pred_lr=lr.predict_proba(test_oh )
del(df)
 gc.collect()<load_from_csv>
output_dict = {'id': test.id,
'target': test_pred_lr[:,1]}
output = pd.DataFrame(output_dict, columns = ['id', 'target'])
 output.head(10 )
<concatenate><EOS> 
output.to_csv('submission.csv', index=False )