Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<categorify>
matplotlib.use('nbagg')
 plt.rcParams.update({'figure.max_open_warning': 0})
 sns.set_style('whitegrid')
tok=text.Tokenizer(num_words=max_features,lower=True)
 tok.fit_on_texts(list(X_train)+list(X_test))
X_train=tok.texts_to_sequences(X_train)
 X_test=tok.texts_to_sequences(X_test)
 x_train=sequence.pad_sequences(X_train,maxlen=maxlen)
 x_test=sequence.pad_sequences(X_test,maxlen=maxlen )<categorify>
print("Reading Data")
 train = pd.read_csv('.. /input/mercari-price-suggestion-challenge/train.tsv', sep='\t')
 test = pd.read_csv('.. /input/mercari-price-suggestion-challenge/test_stg2.tsv', sep='\t')
 print("Shape of train data: ",train.shape)
 print("Shape of test data: ",test.shape )
embeddings_index = {}
with open(EMBEDDING_FILE,encoding='utf8')as f:
for line in f:
values = line.rstrip().rsplit(' ')
 word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs 
<feature_engineering>
y_train = np.log1p(train["price"] )
word_index = tok.word_index
num_words = min(max_features, len(word_index)+ 1)
 embedding_matrix = np.zeros(( num_words, embed_size))
for word, i in word_index.items() :
if i >= max_features:
continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector<choose_model_class>
NUM_BRANDS = 2500
NAME_MIN_DF = 10
MAX_FEAT_DESCP = 50000
sequence_input = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix],trainable = False )(sequence_input)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(GRU(128, return_sequences=True,dropout=0.1,recurrent_dropout=0.1))(x)
 x = Conv1D(64, kernel_size = 3, padding = "valid", kernel_initializer = "glorot_uniform" )(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 x = concatenate([avg_pool, max_pool])
 preds = Dense(6, activation="sigmoid" )(x)
 model = Model(sequence_input, preds)
 model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-3),metrics=['accuracy'] )<split>
print('No of duplicates in train: {}'.format(sum(train.duplicated())))
 print('No of duplicates in test : {}'.format(sum(test.duplicated())) )
batch_size = 128
epochs = 4
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y_train, train_size=0.9, random_state=233 )<train_model>
train.isnull().any()
model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs, validation_data=(X_val, y_val),callbacks = callbacks_list,verbose=1)
 model.load_weights(filepath)
 print('Predicting.... ')
 y_pred = model.predict(x_test,batch_size=1024,verbose=1 )<save_to_csv>
print('We have {} NaN/Null values in train'.format(train.isnull().values.sum()))
print('We have {} NaN/Null values in test'.format(test.isnull().values.sum()))
submission = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv.zip',compression='zip')
 submission[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]] = y_pred
submission.to_csv('submission.csv', index=False )<define_variables>
train["category_name"] = train["category_name"].fillna("Other" ).astype("category")
 train["brand_name"] = train["brand_name"].fillna("unknown")
 test["category_name"] = test["category_name"].fillna("Other" ).astype("category")
 test["brand_name"] = test["brand_name"].fillna("unknown")
 top_brands = train["brand_name"].value_counts().index[:NUM_BRANDS]
train.loc[~train["brand_name"].isin(top_brands), "brand_name"] = "Other"
test.loc[~test["brand_name"].isin(top_brands), "brand_name"] = "Other"
train["item_description"] = train["item_description"].fillna("None")
 train["brand_name"] = train["brand_name"].astype("category")
 test["item_description"] = test["item_description"].fillna("None")
 test["brand_name"] = test["brand_name"].astype("category" )
f_caps_gru = '.. /input/capsule-net-with-gru/submission.csv'
f_dual_embed_pl = '.. /input/submission-dual-embed-pl/submission_dual_embed(2 ).csv'
f_dual_embed_mish = '.. /input/bi-gru-lstm-dual-embedding-with-mish/submission.csv'
f_lstm_glove_tta = '.. /input/improved-lstm-baseline-glove-dropout-trainta/submission.csv'
f_dual_embed_dehyp = '.. /input/improved-lstm-baseline-bi-lstm-dual-embed-dehyp/submission.csv'
f_lstm_fast = '.. /input/improved-lstm-baseline-fasttext-dropout/submission.csv'
f_nbsvm = '.. /input/nb-svm-strong-linear-baseline/submission.csv'
f_bi_post = '.. /input/bi-post/10fold_lstmpp_am.csv'
f_dpcnn = '.. /input/dpcnn-wordcloud/10fold_dpcnn_test.csv'
f_dmcnn = '.. /input/dmcnn-demoji/10fold_dmcnn_am.csv'
f_rcn = '.. /input/rcn-capsule/10fold_capsule_am.csv'<load_from_csv>
print("Top 15 brands in train data: ")
 brand_nam.most_common(15 )
p_caps_gru = pd.read_csv(f_caps_gru)
 p_dual_embed_pl = pd.read_csv(f_dual_embed_pl)
 p_dual_embed_mish = pd.read_csv(f_dual_embed_mish)
 p_lstm_glove_tta = pd.read_csv(f_lstm_glove_tta)
 p_dual_embed_dehyp = pd.read_csv(f_dual_embed_dehyp)
 p_lstm_fast = pd.read_csv(f_lstm_fast)
 p_nbsvm = pd.read_csv(f_nbsvm)
 p_bi_post = pd.read_csv(f_bi_post)
 p_dpcnn = pd.read_csv(f_dpcnn)
 p_dmcnn = pd.read_csv(f_dmcnn)
 p_rcn = pd.read_csv(f_rcn )<feature_engineering>
print("Top 15 brands in test data: ")
 brand_nam.most_common(15 )
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_caps_gru.copy()
p_res[label_cols] =(p_caps_gru[label_cols] + p_dual_embed_pl[label_cols] + p_dual_embed_mish[label_cols] + p_lstm_glove_tta[label_cols] + p_dual_embed_dehyp[label_cols] + p_lstm_fast[label_cols] + p_nbsvm[label_cols] + p_bi_post[label_cols] * 3 + p_dpcnn[label_cols] * 3 + p_dmcnn[label_cols] * 3 + p_rcn[label_cols] * 3)/ 19<save_to_csv>
print("Encodings")
 count_nm = CountVectorizer(min_df=NAME_MIN_DF)
 train_name_vec = count_nm.fit_transform(train["name"])
 test_name_vec = count_nm.transform(test["name"])
 print("Shape of train Name feature: ",train_name_vec.shape)
 print("Shape of test Name feature: ",test_name_vec.shape )
p_res.to_csv('submission.csv', index=False )<define_variables>
print("Descp encoders")
 count_desc = TfidfVectorizer(max_features = MAX_FEAT_DESCP,
ngram_range =(1,3),
stop_words = "english")
 train_desc_vec = count_desc.fit_transform(train["item_description"])
 test_desc_vec = count_desc.transform(test["item_description"])
 print("Shape of train Name feature: ",train_desc_vec.shape)
 print("Shape of test Name feature: ",test_desc_vec.shape )
f_caps_gru = '.. /input/capsule-net-with-gru/submission.csv'
f_dual_embed_mish = '.. /input/bi-gru-lstm-dual-embedding-with-mish/submission.csv'
f_bi_post = '.. /input/bi-post/10fold_lstmpp_am.csv'
f_dpcnn = '.. /input/dpcnn-wordcloud/10fold_dpcnn_test.csv'
f_dmcnn = '.. /input/dmcnn-demoji/10fold_dmcnn_am.csv'
f_rcn = '.. /input/rcn-capsule/10fold_capsule_am.csv'
f_attn_300d = '.. /input/attn-300d-am/10fold_attn_post_am.csv'
f_slgbm = '.. /input/simple-lightgbm-classifier/submission_001.csv'
f_cat_boost = '.. /input/cat-boost/results_preds_cat.csv'<load_from_csv>
print("Category Encoders")
 unique_categories = pd.Series("/".join(train["category_name"].unique().astype("str")).split("/")).unique()
count_category = CountVectorizer()
encoder_cat_train = count_category.fit_transform(train["category_name"])
 encoder_cat_test= count_category.transform(test["category_name"] )
p_caps_gru = pd.read_csv(f_caps_gru)
 p_dual_embed_mish = pd.read_csv(f_dual_embed_mish)
 p_bi_post = pd.read_csv(f_bi_post)
 p_dpcnn = pd.read_csv(f_dpcnn)
 p_dmcnn = pd.read_csv(f_dmcnn)
 p_rcn = pd.read_csv(f_rcn)
 p_attn_300d = pd.read_csv(f_attn_300d)
 p_slgbm = pd.read_csv(f_slgbm)
 p_cat_boost = pd.read_csv(f_cat_boost )<feature_engineering>
print("Brand encoders")
 vect_brand = LabelBinarizer(sparse_output=True)
 encoder_brnd_train = vect_brand.fit_transform(train["brand_name"])
 encoder_brnd_test= vect_brand.transform(test["brand_name"] )
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_caps_gru.copy()
p_res[label_cols] =(p_cat_boost[label_cols] + p_slgbm[label_cols] + p_caps_gru[label_cols] + p_dual_embed_mish[label_cols] + p_bi_post[label_cols] + p_dpcnn[label_cols] + p_dmcnn[label_cols] + p_rcn[label_cols] + p_attn_300d[label_cols])/ 9<save_to_csv>
print(encoder_brnd_train.shape)
 print(encoder_brnd_test.shape )
p_res.to_csv('submission.csv', index=False )<define_variables>
X_train = scipy.sparse.hstack((
train_desc_vec,
encoder_brnd_train,
encoder_cat_train,
train_name_vec,
np.array(train['item_condition_id'] ).reshape(-1,1),
np.array(train['shipping'] ).reshape(-1,1)
)).tocsr()
print(X_train.shape )
f_caps_gru = '.. /input/capsule-net-with-gru/submission.csv'
f_dual_embed_pl = '.. /input/submission-dual-embed-pl/submission_dual_embed(2 ).csv'
f_dual_embed_mish = '.. /input/bi-gru-lstm-dual-embedding-with-mish/submission.csv'
f_lstm_glove_tta = '.. /input/improved-lstm-baseline-glove-dropout-trainta/submission.csv'
f_dual_embed_dehyp = '.. /input/improved-lstm-baseline-bi-lstm-dual-embed-dehyp/submission.csv'
f_lstm_fast = '.. /input/improved-lstm-baseline-fasttext-dropout/submission.csv'
f_nbsvm = '.. /input/nb-svm-strong-linear-baseline/submission.csv'
f_bi_post = '.. /input/bi-post/10fold_lstmpp_am.csv'
f_dpcnn = '.. /input/dpcnn-wordcloud/10fold_dpcnn_test.csv'
f_dmcnn = '.. /input/dmcnn-demoji/10fold_dmcnn_am.csv'
f_rcn = '.. /input/rcn-capsule/10fold_capsule_am.csv'
f_attn_300d = '.. /input/attn-300d/10fold_attn_post_am.csv'
f_slgbm = '.. /input/simple-lightgbm-classifier/submission_001.csv'<load_from_csv>
X_test = scipy.sparse.hstack((
test_desc_vec,
encoder_brnd_test,
encoder_cat_test,
test_name_vec,
np.array(test['item_condition_id'] ).reshape(-1,1),
np.array(test['shipping'] ).reshape(-1,1)
)).tocsr()
print(X_test.shape )
p_caps_gru = pd.read_csv(f_caps_gru)
 p_dual_embed_pl = pd.read_csv(f_dual_embed_pl)
 p_dual_embed_mish = pd.read_csv(f_dual_embed_mish)
 p_lstm_glove_tta = pd.read_csv(f_lstm_glove_tta)
 p_dual_embed_dehyp = pd.read_csv(f_dual_embed_dehyp)
 p_lstm_fast = pd.read_csv(f_lstm_fast)
 p_nbsvm = pd.read_csv(f_nbsvm)
 p_bi_post = pd.read_csv(f_bi_post)
 p_dpcnn = pd.read_csv(f_dpcnn)
 p_dmcnn = pd.read_csv(f_dmcnn)
 p_rcn = pd.read_csv(f_rcn)
 p_attn_300d = pd.read_csv(f_attn_300d)
 p_slgbm = pd.read_csv(f_slgbm )<feature_engineering>
from xgboost import XGBRegressor
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
p_res = p_caps_gru.copy()
p_res[label_cols] =(p_slgbm[label_cols] + p_caps_gru[label_cols] + p_dual_embed_pl[label_cols] + p_dual_embed_mish[label_cols] + p_lstm_glove_tta[label_cols] + p_dual_embed_dehyp[label_cols] + p_lstm_fast[label_cols] + p_nbsvm[label_cols] + p_bi_post[label_cols] * 3 + p_dpcnn[label_cols] * 3 + p_dmcnn[label_cols] * 3 + p_rcn[label_cols] * 3 + p_attn_300d[label_cols] * 3)/ 23<save_to_csv>
xgb = XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bytree=1, gamma=0.7, learning_rate=0.1, max_delta_step=0,
max_depth=16, min_child_weight=1, missing=None, n_estimators=100,
n_jobs=-1, random_state=0, reg_alpha=0, reg_lambda=1, scale_pos_weight=1,
seed=None, silent=True, subsample=1)
 print("Fitting Model 1")
 xgb.fit(X_train, y_train )
p_res.to_csv('submission.csv', index=False )<install_modules>
y_pred = xgb.predict(X_test )
!pip install gast==0.2.2<import_modules>
model = RidgeCV(fit_intercept=True, alphas=[5.0], normalize=False, cv = 2, scoring='neg_mean_squared_error')
 print("Fitting Model")
 model.fit(X_train, y_train )
print(tf.__version__)
 tf.test.is_gpu_available(
 cuda_only=False,
min_cuda_compute_capability=None 
)
 tf.__version__<load_from_csv>
preds = model.predict(X_test )
BATCH_SIZE = 512
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
EPOCHS = 4
MAX_LEN = 220
TEXT_COLUMN = 'comment_text'
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
CHARS_TO_REMOVE = '!"
“”’'∞θ÷α•à−β∅³π‘₹´°£€\×™√²—'
submission = pd.read_csv(".. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv" )<define_variables>
from sklearn.preprocessing import LabelBinarizer, LabelEncoder
from keras.preprocessing.text import Tokenizer
categories = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
data_folder = ".. /input/jigsaw-toxic-comment-classification-challenge/"
pretrained_folder = ".. /input/"
train_filepath = data_folder + "train.csv"
test_filepath = data_folder + "test.csv"
submission_path = data_folder + "submission.csv"
facebook_filepath = pretrained_folder + "fasttext-crawl-300d-2m/crawl-300d-2M.vec"
google_filepath = pretrained_folder + "glove840b300dtxt/glove.840B.300d.txt"
embedding_dim_facebook = 300
embedding_dim_google = 300<define_variables>
full_df = pd.concat([train, test] )
training_samples_count = 149571
validation_samples_count = 10000
length_threshold = 20000
word_count_threshold = 900
words_limit = 310000
valid_characters = " " + "@$" + "'!?-" + "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz".upper()
valid_characters_ext = valid_characters + "abcdefghijklmnopqrstuvwxyz".upper()
valid_set = set(x for x in valid_characters)
 valid_set_ext = set(x for x in valid_characters_ext)
 toxic_words = ["poop", "crap", "prick", "twat", "wikipedia", "wiki", "hahahahaha", "lol", "bastard", "sluts", "slut", "douchebag", "douche", "blowjob", "nigga", "dumb", "jerk", "wanker", "wank", "penis", "motherfucker", "fucker", "fuk", "fucking", "fucked", "fuck", "bullshit", "shit", "stupid", "bitches", "bitch", "suck", "cunt", "dick", "cocks", "cock", "die", "kill", "gay", "jewish", "jews", "jew", "niggers", "nigger", "faggot", "fag", "asshole"]
astericks_words = [('mother****ers', 'motherfuckers'),('motherf*cking', 'motherfucking'),('mother****er', 'motherfucker'),('motherf*cker', 'motherfucker'),('bullsh*t', 'bullshit'),('f**cking', 'fucking'),('f*ucking', 'fucking'),('fu*cking', 'fucking'),('****ing', 'fucking'),('a**hole', 'asshole'),('assh*le', 'asshole'),('f******', 'fucking'),('f*****g', 'fucking'),('f***ing', 'fucking'),('f**king', 'fucking'),('f*cking', 'fucking'),('fu**ing', 'fucking'),('fu*king', 'fucking'),('fuc*ers', 'fuckers'),('f*****', 'fucking'),('f***ed', 'fucked'),('f**ker', 'fucker'),('f*cked', 'fucked'),('f*cker', 'fucker'),('f*ckin', 'fucking'),('fu*ker', 'fucker'),('fuc**n', 'fucking'),('ni**as', 'niggas'),('b**ch', 'bitch'),('b*tch', 'bitch'),('c*unt', 'cunt'),('f**ks', 'fucks'),('f*ing', 'fucking'),('ni**a', 'nigga'),('c*ck', 'cock'),('c*nt', 'cunt'),('cr*p', 'crap'),('d*ck', 'dick'),('f***', 'fuck'),('f**k', 'fuck'),('f*ck', 'fuck'),('fc*k', 'fuck'),('fu**', 'fuck'),('fu*k', 'fuck'),('s***', 'shit'),('s**t', 'shit'),('sh**', 'shit'),('sh*t', 'shit'),('tw*t', 'twat')]
fasttext_misspelings = {"'n'balls": 'balls', "-nazi's": 'nazis', 'adminabuse': 'admin abuse', "admins's": 'admins', 'arsewipe': 'arse wipe', 'assfack': 'asshole', 'assholifity': 'asshole', 'assholivity': 'asshole', 'asshoul': 'asshole', 'asssholeee': 'asshole', 'belizeans': 'mexicans', "blowing's": 'blowing', 'bolivians': 'mexicans', 'celtofascists': 'fascists', 'censorshipmeisters': 'censor', 'chileans': 'mexicans', 'clerofascist': 'fascist', 'cowcrap': 'crap', 'crapity': 'crap', "d'idiots": 'idiots', 'deminazi': 'nazi', 'dftt': "don't feed the troll", 'dildohs': 'dildo', 'dramawhores': 'drama whores', 'edophiles': 'pedophiles', 'eurocommunist': 'communist', 'faggotkike': 'faggot', 'fantard': 'retard', 'fascismnazism': 'fascism', 'fascistisized': 'fascist', 'favremother': 'mother', 'fuxxxin': 'fucking', "g'damn": 'goddamn', 'harassmentat': 'harassment', 'harrasingme': 'harassing me', 'herfuc': 'motherfucker', 'hilterism': 'fascism', 'hitlerians': 'nazis', 'hitlerites': 'nazis', 'hubrises': 'pricks', 'idiotizing': 'idiotic', 'inadvandals': 'vandals', "jackass's": 'jackass', 'jiggabo': 'nigga', 'jizzballs': 'jizz balls', 'jmbass': 'dumbass', 'lejittament': 'legitimate', "m'igger": 'nigger', "m'iggers": 'niggers', 'motherfacking': 'motherfucker', 'motherfuckenkiwi': 'motherfucker', 'muthafuggas': 'niggas', 'nazisms': 'nazis', 'netsnipenigger': 'nigger', 'niggercock': 'nigger', 'niggerspic': 'nigger', 'nignog': 'nigga', 'niqqass': 'niggas', "non-nazi's": 'not a nazi', 'panamanians': 'mexicans', 'pedidiots': 'idiots', 'picohitlers': 'hitler', 'pidiots': 'idiots', 'poopia': 'poop', 'poopsies': 'poop', 'presumingly': 'obviously', 'propagandaanddisinformation': 'propaganda and disinformation', 'propagandaministerium': 'propaganda', 'puertoricans': 'mexicans', 'puertorricans': 'mexicans', 'pussiest': 'pussies', 'pussyitis': 'pussy', 'rayaridiculous': 'ridiculous', 'redfascists': 'fascists', 'retardzzzuuufff': 'retard', "revertin'im": 'reverting', 'scumstreona': 'scums', 'southamericans': 'mexicans', 'strasserism': 'fascism', 'stuptarded': 'retarded', "t'nonsense": 'nonsense', "threatt's": 'threat', 'titoists': 'communists', 'twatbags': 'douchebags', 'youbollocks': 'you bollocks'}
acronym_words = {}<define_variables>
print("Processing categorical data...")
 le = LabelEncoder()
le.fit(full_df.category_name)
 full_df.category_name = le.transform(full_df.category_name)
 le.fit(full_df.brand_name)
 full_df.brand_name = le.transform(full_df.brand_name)
 del le
cont_patterns = [ 
(r'(W|w)on't', r'will not'), 
(r'(C|c)an't', r'can not'), 
(r'(I|i)'m', r'i am'), 
(r'(A|a)in't', r'is not'), 
(r'(\w+)'ll', r'\g<1> will'), 
(r'(\w+)n't', r'\g<1> not'), 
(r'(\w+)'ve', r'\g<1> have'), 
(r'(\w+)'s', r'\g<1> is'), 
(r'(\w+)'re', r'\g<1> are'), 
(r'(\w+)'d', r'\g<1> would'),
]
patterns = [(re.compile(regex), repl)for(regex, repl)in cont_patterns]
def split_word(word, toxic_words):
if word == "":
return ""
lower = word.lower()
for toxic_word in toxic_words:
start = lower.find(toxic_word)
 if start >= 0:
end = start + len(toxic_word)
 result = " ".join([word[0:start], word[start:end], split_word(word[end:], toxic_words)])
 return result.replace(" ", " " ).strip()
return word
tknzr = TweetTokenizer(strip_handles=False, reduce_len=True)
 def word_tokenize(sentence):
sentence = sentence.replace("$", "s")
 sentence = sentence.replace("@", "a")
 sentence = sentence.replace("!", " ! ")
 sentence = sentence.replace("?", " ? ")
 return tknzr.tokenize(sentence)
 def replace_url(word):
if "http://" in word or "www." in word or "https://" in word or "wikipedia.org" in word:
return ""
return word
def normalize_by_dictionary(normalized_word, dictionary):
result = []
for word in normalized_word.split() :
if word == word.upper() :
if word.lower() in dictionary:
result.append(dictionary[word.lower() ].upper())
 else:
result.append(word)
 else:
if word.lower() in dictionary:
result.append(dictionary[word.lower() ])
 else:
result.append(word)
 return " ".join(result )<categorify>
print("Transforming text data to sequences...")
 raw_text = np.hstack([full_df.item_description.str.lower() , full_df.name.str.lower() ])
 print(" Fitting tokenizer...")
 tok_raw = Tokenizer()
tok_raw.fit_on_texts(raw_text)
 print(" Transforming text to sequences...")
 full_df['seq_item_description'] = tok_raw.texts_to_sequences(full_df.item_description.str.lower())
 full_df['seq_name'] = tok_raw.texts_to_sequences(full_df.name.str.lower())
 del tok_raw
nlp = spacy.load('en')
 def normalize_comment(comment):
comment = unidecode(comment)
 comment = comment[:length_threshold]
normalized_words = []
for w in astericks_words:
if w[0] in comment:
comment = comment.replace(w[0], w[1])
 if w[0].upper() in comment:
comment = comment.replace(w[0].upper() , w[1].upper())
 for word in word_tokenize(comment):
if word == "." or word == ",":
normalized_words.append(word)
 continue
word = replace_url(word)
 if word.count(".")== 1:
word = word.replace(".", " ")
 filtered_word = "".join([x for x in word if x in valid_set])
 normalized_word = split_word(filtered_word, toxic_words)
 normalized_word = normalize_by_dictionary(normalized_word, fasttext_misspelings)
 normalized_word = normalize_by_dictionary(normalized_word, acronym_words)
 normalized_words.append(normalized_word)
 normalized_comment = " ".join(normalized_words)
 result = []
for word in normalized_comment.split() :
if word.upper() == word:
result.append(word)
 else:
result.append(word.lower())
 result = " ".join(result)
 if "sock puppet" in result:
result = result.replace("sock puppet", "sockpuppet")
 if "SOCK PUPPET" in result:
result = result.replace("SOCK PUPPET", "SOCKPUPPET")
 return result 
<load_from_csv>
MAX_NAME_SEQ = 10
MAX_ITEM_DESC_SEQ = 75
MAX_TEXT = np.max([
np.max(full_df.seq_name.max()),
np.max(full_df.seq_item_description.max()),
])+ 4
MAX_CATEGORY = np.max(full_df.category_name.max())+ 1
MAX_BRAND = np.max(full_df.brand_name.max())+ 1
MAX_CONDITION = np.max(full_df.item_condition_id.max())+ 1
def read_data_files(train_filepath, test_filepath):
train = pd.read_csv(train_filepath)
 labels = train[categories].values
test = pd.read_csv(test_filepath)
 test_comments = test["comment_text"].fillna("_na_" ).values
np_normalize = np.vectorize(normalize_comment)
 comments = train["comment_text"].fillna("_na_" ).values
normalized_comments = np_normalize(comments)
 del comments
gc.collect()
comments = test["comment_text"].fillna("_na_" ).values
normalized_test_comments = np_normalize(test_comments)
 del comments
gc.collect()
print('Shape of data tensor:', normalized_comments.shape)
 print('Shape of label tensor:', labels.shape)
 print('Shape of test data tensor:', normalized_test_comments.shape)
 return(labels, normalized_comments, normalized_test_comments )<load_from_csv>
def get_keras_data(df):
X = {
'name': pad_sequences(df.seq_name, maxlen=MAX_NAME_SEQ),
'item_desc': pad_sequences(df.seq_item_description, maxlen=MAX_ITEM_DESC_SEQ),
'brand_name': np.array(df.brand_name),
'category_name': np.array(df.category_name),
'item_condition': np.array(df.item_condition_id),
'num_vars': np.array(df[["shipping"]]),
}
return X
n_trains = train.shape[0]
n_tests = test.shape[0]
train = full_df[:n_trains]
test = full_df[n_trains:]
X_train = get_keras_data(train)
 X_test = get_keras_data(test )
%%time
labels, x_train, x_test = read_data_files(train_filepath, test_filepath )<define_variables>
def new_rnn_model(lr=0.001, decay=0.0):
name = Input(shape=[X_train["name"].shape[1]], name="name")
 item_desc = Input(shape=[X_train["item_desc"].shape[1]], name="item_desc")
 brand_name = Input(shape=[1], name="brand_name")
 category_name = Input(shape=[1], name="category_name")
 item_condition = Input(shape=[1], name="item_condition")
 num_vars = Input(shape=[X_train["num_vars"].shape[1]], name="num_vars")
 emb_name = Embedding(MAX_TEXT, 20 )(name)
 emb_item_desc = Embedding(MAX_TEXT, 60 )(item_desc)
 emb_brand_name = Embedding(MAX_BRAND, 10 )(brand_name)
 emb_category_name = Embedding(MAX_CATEGORY, 10 )(category_name)
 rnn_layer1 = GRU(16 )(emb_item_desc)
 rnn_layer2 = GRU(8 )(emb_name)
 main_l = concatenate([
Flatten()(emb_brand_name),
Flatten()(emb_category_name),
item_condition,
rnn_layer1,
rnn_layer2,
num_vars,
])
 main_l = Dense(256 )(main_l)
 main_l = Activation('elu' )(main_l)
 main_l = Dense(128 )(main_l)
 main_l = Activation('elu' )(main_l)
 main_l = Dense(64 )(main_l)
 main_l = Activation('elu' )(main_l)
 output = Dense(1, activation="linear" )(main_l)
 model = Model([name, item_desc, brand_name , category_name, item_condition, num_vars], output)
 optimizer = optimizers.Adam(lr=lr, decay=decay)
 model.compile(loss="mse", optimizer=optimizer)
 return model
model = new_rnn_model()
model.summary()
del model
max_features = 20000
maxlen = 100<string_transform>
BATCH_SIZE = 1024
epochs = 2
exp_decay = lambda init, fin, steps:(init/fin)**(1/(steps-1)) - 1
steps = int(n_trains / BATCH_SIZE)* epochs
lr_init, lr_fin = 0.007, 0.0005
lr_decay = exp_decay(lr_init, lr_fin, steps)
 rnn_model = new_rnn_model(lr=lr_init, decay=lr_decay)
 print("Fitting RNN model to training examples...")
 rnn_model.fit(
 X_train, y_train, epochs=epochs, batch_size=BATCH_SIZE, verbose=2 )
%%time
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(x_train))
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test)
 x_train = sequence.pad_sequences(x_train, maxlen=MAX_LEN)
 x_test = sequence.pad_sequences(x_test, maxlen=MAX_LEN )<load_pretrained>
preds = rnn_model.predict(X_test, batch_size=BATCH_SIZE )
<split><EOS> 
test["price"] = np.expm1(preds)
 test["test_id"] = pd.to_numeric(test["test_id"], downcast='integer')
 test[["test_id", "price"]].to_csv("submission.csv", index = False )