Kaggle competition: Quora Insincere Questions Classification
df_block['item_price_by_item_mean'] = df_block.groupby(['item_id'])['item_price'].transform('mean' ).astype(np.float16 )<categorify>
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Input, Embedding, Dense, Dropout, Concatenate, Lambda, Flatten
from keras.layers import GlobalMaxPool1D
from keras.models import Model
import tqdm
for i in [1, 2, 3, 4, 5, 6]: df_block['item_price_by_item_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'item_id'])['item_price_shift' + str(i)].transform('mean' ).astype(np.float16 )<feature_engineering>
MAX_SEQUENCE_LENGTH = 60
MAX_WORDS = 45000
EMBEDDINGS_TRAINED_DIMENSIONS = 100
EMBEDDINGS_LOADED_DIMENSIONS = 300
for i in [1, 2, 3, 4, 5, 6]: df_block['delta_item_price_by_item_date_mean_shift' + str(i)] =(df_block['item_price_by_item_date_mean_shift' + str(i)] - df_block['item_price_by_item_mean'])/ df_block['item_price_by_item_mean']
def select_nonnull(row):
for i in [1, 2, 3, 4, 5, 6]:
if not(np.isnan(row['delta_item_price_by_item_date_mean_shift' + str(i)])) : return row['delta_item_price_by_item_date_mean_shift' + str(i)]
return 0
df_block['delta_item_price_by_item_date_mean_shift'] = df_block.apply(select_nonnull, axis=1 )<feature_engineering>
def load_embeddings(file):
embeddings = {}
with open(file)as f:
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings = dict(get_coefs(*line.split(" ")) for line in f)
 print('Found %s word vectors.' % len(embeddings))
return embeddings
df_block['item_cnt_month'] = df_block['item_cnt_month'].clip(0 ,20 )<merge>
pretrained_embeddings = load_embeddings(".. /input/embeddings/glove.840B.300d/glove.840B.300d.txt" )
for i in [1, 2, 3, 6, 12]:
shifted = df_block[['date_block_num', 'shop_id', 'item_id', 'item_cnt_month']].copy(deep=True)
 shifted.columns = ['date_block_num', 'shop_id','item_id', 'item_cnt_month_shift' + str(i)]
shifted['date_block_num'] = shifted['date_block_num'] + i
df_block = pd.merge(df_block, shifted, how='left', on=['date_block_num', 'shop_id', 'item_id'] )<data_type_conversions>
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv" )
for i in [1]: df_block['item_cnt_month_by_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<categorify>
BATCH_SIZE = 512
Q_FRACTION = 1
questions = df_train.sample(frac=Q_FRACTION)
 question_texts = questions["question_text"].values
question_targets = questions["target"].values
test_texts = df_test["question_text"].fillna("_na_" ).values
print(f"Working on {len(questions)} questions" )
for i in [1, 2, 3, 6, 12]: df_block['item_cnt_month_by_item_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'item_id'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<categorify>
tokenizer = Tokenizer(num_words=MAX_WORDS)
 tokenizer.fit_on_texts(list(df_train["question_text"].values))
for i in [1, 2, 3, 6, 12]: df_block['item_cnt_month_by_category_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'item_category_id'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<categorify>
def create_embedding_weights(tokenizer, embeddings, dimensions):
not_embedded = defaultdict(int)
 word_index = tokenizer.word_index
words_count = min(len(word_index), MAX_WORDS)
 embeddings_matrix = np.zeros(( words_count, dimensions))
for word, i in word_index.items() :
if i >= MAX_WORDS:
continue
if word not in embeddings:
not_embedded[word] = not_embedded[word] + 1
continue
embedding_vector = embeddings[word]
if embedding_vector is not None:
embeddings_matrix[i] = embedding_vector
print(sorted(not_embedded, key=not_embedded.get)[:10])
 return embeddings_matrix
for i in [1, 2, 3, 6, 12]: df_block['item_cnt_month_by_shop_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'shop_id'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<data_type_conversions>
pretrained_emb_weights = create_embedding_weights(tokenizer, pretrained_embeddings, EMBEDDINGS_LOADED_DIMENSIONS )
for i in [1]: df_block['item_cnt_month_by_city_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'city_code'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<data_type_conversions>
THRESHOLD = 0.35
class EpochMetricsCallback(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.f1s = []
self.precisions = []
self.recalls = []
def on_epoch_end(self, epoch, logs={}):
predictions = self.model.predict(self.validation_data[0])
 predictions =(predictions > THRESHOLD ).astype(int)
 predictions = np.asarray(predictions)
 targets = self.validation_data[1]
f1 = metrics.f1_score(targets, predictions)
 precision = metrics.precision_score(targets, predictions)
 recall = metrics.recall_score(targets, predictions)
 print(" - F1 score: {0:.4f}, Precision: {1:.4f}, Recall: {2:.4f}" 
.format(f1, precision, recall))
self.f1s.append(f1)
 self.precisions.append(precision)
 self.recalls.append(recall)
 return
def display_model_history(history):
data = pd.DataFrame(data={'Train': history.history['loss'], 'Test': history.history['val_loss']})
 ax = sns.lineplot(data=data, palette="pastel", linewidth=2.5, dashes=False)
 ax.set(xlabel='Epoch', ylabel='Loss', title='Loss')
 sns.despine()
plt.show()
def display_model_epoch_metrics(epoch_callback):
data = pd.DataFrame(data = {
'F1': epoch_callback.f1s,
'Precision': epoch_callback.precisions,
'Recall': epoch_callback.recalls})
 sns.lineplot(data=data, palette='muted', linewidth=2.5, dashes=False)
 sns.despine()
plt.show()
for i in [1]: df_block['item_cnt_month_by_item_city_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'item_id', 'city_code'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<data_type_conversions>
X = pad_sequences(tokenizer.texts_to_sequences(question_texts),
maxlen=MAX_SEQUENCE_LENGTH)
 Y = question_targets
test_word_tokens = pad_sequences(tokenizer.texts_to_sequences(test_texts),
maxlen=MAX_SEQUENCE_LENGTH )
for i in [1]: df_block['item_cnt_month_by_category_shop_date_mean_shift' + str(i)] = df_block.groupby(['date_block_num', 'item_category_id', 'shop_id'])['item_cnt_month_shift' + str(i)].transform('mean' ).astype(np.float16 )<data_type_conversions>
def make_model(filter_size, num_filters):
tokenized_input = Input(shape=(MAX_SEQUENCE_LENGTH,), name="tokenized_input")
 pretrained = Embedding(MAX_WORDS,
EMBEDDINGS_LOADED_DIMENSIONS,
weights=[pretrained_emb_weights],
trainable=False )(tokenized_input)
 pretrained = Reshape(( MAX_SEQUENCE_LENGTH, EMBEDDINGS_LOADED_DIMENSIONS, 1))(pretrained)
 pretrained = Dropout(0.1 )(pretrained)
 conv_0 = Conv2D(num_filters, kernel_size=(filter_size, EMBEDDINGS_LOADED_DIMENSIONS),
kernel_initializer='he_normal', activation='tanh' )(pretrained)
 maxpool_0 = MaxPool2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_size + 1, 1))(conv_0)
 d0 = Dropout(0.15 )(maxpool_0)
 d0 = Dense(10 )(d0)
 x = Flatten()(d0)
 x = Dropout(0.3 )(x)
 x = BatchNormalization()(x)
 out = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=[tokenized_input], outputs=out)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary()
return model
df_block['first_sale_item'] =(df_block['date_block_num'] - df_block.groupby(['item_id'])['date_block_num'].transform('min')).astype(np.int16)
 df_block['first_sale_item_shop'] =(df_block['date_block_num'] - df_block.groupby(['item_id', 'shop_id'])['date_block_num'].transform('min')).astype(np.int16 )<merge>
filter_sizes = [1, 2, 3, 5]
num_filters = 45
test_predictions = []
kaggle_predictions = []
train_X, test_X, train_Y, test_Y = train_test_split(X, Y, test_size=0.025)
 for f in filter_sizes:
print("CNN MODEL WITH FILTER OF SIZE {0}".format(f))
epoch_callback = EpochMetricsCallback()
model = make_model(f, num_filters)
 x, val_x, y, val_y = train_test_split(train_X, train_Y, test_size=0.01)
 history = model.fit(
 x=x, y=y, validation_data=(val_x, val_y),
batch_size=512, epochs=23, callbacks=[epoch_callback], verbose=2)
 display_model_history(history)
 display_model_epoch_metrics(epoch_callback)
 kaggle_predictions.append(model.predict([test_word_tokens], batch_size=1024, verbose=2))
test_predictions.append(model.predict([test_X]))
list_of_first = []
for dateblocknum in df_block['date_block_num'].unique() :
df_first = df_block[(df_block['date_block_num'] < dateblocknum)&(df_block['first_sale_item'] == 0)].groupby(['item_category_id'], as_index=False ).agg({'item_cnt_month': 'mean'})
 df_first.insert(0, 'date_block_num', dateblocknum)
 list_of_first.append(df_first)
 df_first = pd.concat(list_of_first, ignore_index=True ).rename(columns={'item_cnt_month': 'item_cnt_month_by_category_first'})
 df_block = pd.merge(df_block, df_first, how='left', on=['date_block_num', 'item_category_id'] )<groupby>
avg = np.average(kaggle_predictions, axis=0)
 df_out = pd.DataFrame({"qid":df_test["qid"].values})
 df_out['prediction'] =(avg > THRESHOLD ).astype(int)
 df_out.to_csv("submission.csv", index=False )
for i in [12, 18, 24, 30]:
list_of_first = []
for dateblocknum in df_block['date_block_num'].unique() :
df_first = df_block[(df_block['date_block_num'] >= i)&(df_block['date_block_num'] < dateblocknum)&(df_block['first_sale_item'] == 0)].groupby(['item_category_id'], as_index=False ).agg({'item_cnt_month': 'mean'})
 df_first.insert(0, 'date_block_num', dateblocknum)
 list_of_first.append(df_first)
 df_first = pd.concat(list_of_first, ignore_index=True ).rename(columns={'item_cnt_month': 'item_cnt_month_by_category_first_month' + str(i)+ 'onwards'})
 df_block = pd.merge(df_block, df_first, how='left', on=['date_block_num', 'item_category_id'] )<merge>