Kaggle competition: Quora Insincere Questions Classification
<SOS> metric: FScore_1 Kaggle data source: quora-insincere-questions-classification<categorify>
%matplotlib inline
tqdm.pandas()
year_oh = pd.get_dummies(all_data['year_index'], prefix='year')
 month_oh = pd.get_dummies(all_data['month'], prefix='month')
 all_data_oh = all_data.drop(columns=['shop_id', 'item_id', 'item_category_id', 'year_index', 'month', 'target', 'last_sale_ago', 'last_shop_sale_ago', 'last_item_sale_ago'])
 all_data_oh = pd.concat([all_data_oh, year_oh, month_oh], axis=1)
 all_data_oh.head()<prepare_x_and_y>
train = pd.read_csv('.. /input/quora-insincere-questions-classification/train.csv')
 test = pd.read_csv('.. /input/quora-insincere-questions-classification/test.csv' )
train_b_index =(all_data['year_index'] == 2)&(all_data['month'] == 9)
 train_c_index =(all_data['year_index'] == 2)&(all_data['month'] == 10)
 train_a_index = ~train_b_index & ~train_c_index
X_train_a = all_data[train_a_index]
y_train_a = X_train_a['target'].clip(0, 20)
 X_train_a = X_train_a.drop(columns='target')
 X_train_b = all_data[train_b_index]
y_train_b = X_train_b['target'].clip(0, 20)
 X_train_b = X_train_b.drop(columns='target')
 X_train_c = all_data[train_c_index]
y_train_c = X_train_c['target'].clip(0, 20)
 X_train_c = X_train_c.drop(columns='target' )<prepare_x_and_y>
print("Tỉ lệ phần trăm số câu hỏi Insincere là:",(len(train.loc[train.target==1])) /(len(train.loc[train.target == 0])) * 100 )
X_train_a_oh = all_data_oh[train_a_index]
X_train_b_oh = all_data_oh[train_b_index]
X_train_c_oh = all_data_oh[train_c_index]<train_model>
print('Số lượng từ trung bình của các câu hỏi trong dữ liệu huấn luyện là {0:.0f}.'.format(np.mean(train['question_text'].apply(lambda x: len(x.split())))))
 print('Số lượng từ trung bình của các câu hỏi trong dữ liệu kiểm thử là {0:.0f}.'.format(np.mean(test['question_text'].apply(lambda x: len(x.split())))) )
model_xgb = xgb.XGBRegressor(max_depth=4, learning_rate=0.5, n_jobs=-1)
 model_xgb.fit(X_train_a, y_train_a )<train_model>
print('Số lượng từ lớn nhất của các câu hỏi trong dữ liệu huấn luyện là {0:.0f}.'.format(np.max(train['question_text'].apply(lambda x: len(x.split())))))
 print('Số lượng từ lớn nhất của các câu hỏi trong dữ liệu kiểm thử là {0:.0f}.'.format(np.max(test['question_text'].apply(lambda x: len(x.split())))) )
model_mlp = MLPRegressor(hidden_layer_sizes=(100, 100), activation='relu', learning_rate_init=0.01, max_iter=10, shuffle=False, verbose=True)
 model_mlp.fit(X_train_a_oh.values, y_train_a )<train_model>
print('Số lượng ký tự trung bình của các câu hỏi trong dữ liệu huấn luyện là {0:.0f}.'.format(np.mean(train['question_text'].apply(lambda x: len(x)))))
 print('Số lượng ký tự trung bình của các câu hỏi trong dữ liệu kiểm thử là {0:.0f}.'.format(np.mean(test['question_text'].apply(lambda x: len(x)))) )
model_rf = RandomForestRegressor(n_estimators=10, criterion='mse', max_depth=None, n_jobs=-1, verbose=1)
 model_rf.fit(X_train_a.values, y_train_a )<compute_test_metric>
embed_size = 300
max_features = 100000
maxlen = 80
y_pred_1 = model_xgb.predict(X_train_b)
 rmse = math.sqrt(mean_squared_error(y_train_b, y_pred_1))
R_score = r2_score(y_train_b, y_pred_1)
 print('XGBoost rmse: ' + str(rmse)+ ', R2: ' + str(R_score))<compute_test_metric>
train, val = train_test_split(train, test_size=0.1, random_state=1023 )
y_pred_2 = model_mlp.predict(X_train_b_oh)
 rmse = math.sqrt(mean_squared_error(y_train_b, y_pred_2))
R_score = r2_score(y_train_b, y_pred_2)
 print('MLP rmse: ' + str(rmse)+ ', R2: ' + str(R_score))<compute_train_metric>
train_X = train["question_text"].fillna("_na_" ).values
val_X = val["question_text"].fillna("_na_" ).values
test_X = test["question_text"].fillna("_na_" ).values
y_pred_3 = model_rf.predict(X_train_b)
 rmse = math.sqrt(mean_squared_error(y_train_b, y_pred_3))
R_score = r2_score(y_train_b, y_pred_3)
 print('Random forest rmse: ' + str(rmse)+ ', R2: ' + str(R_score))<compute_test_metric>
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 val_X = tokenizer.texts_to_sequences(val_X)
 test_X = tokenizer.texts_to_sequences(test_X )
<train_model>
train_X = pad_sequences(train_X, maxlen=maxlen)
 val_X = pad_sequences(val_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen )
X_train_b_2 = np.stack([y_pred_1, y_pred_2, y_pred_3], axis=-1)
 model = LinearRegression(n_jobs=-1)
 model.fit(X_train_b_2, y_train_b )<predict_on_test>
train_y = train['target'].values
val_y = val['target'].values
y_pred_1 = model_xgb.predict(X_train_c)
 y_pred_2 = model_mlp.predict(X_train_c_oh)
 y_pred_3 = model_rf.predict(X_train_c)
 X_train_c_2 = np.stack([y_pred_1, y_pred_2, y_pred_3], axis=-1 )<compute_test_metric>
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size )(inp)
 x = Bidirectional(LSTM(64, return_sequences=True))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 print(model.summary() )
stack_pred = model.predict(X_train_c_2)
 rmse = math.sqrt(mean_squared_error(y_train_c, stack_pred))
R_score = r2_score(y_train_c, y_pred_1)
 print('Ensemble rmse: ' + str(rmse)+ ', R2: ' + str(R_score))<concatenate>
def train_pred(model, train_X, train_y, val_X, val_y, epochs=2):
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y))
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 best_thresh = 0.5
best_score = 0.0
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 score = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
if score > best_score:
best_thresh = thresh
best_score = score
print("Val F1 Score: {:.4f}".format(best_score))
pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 print('='*100)
 return pred_val_y, pred_test_y, best_score
X_train_bc_2 = np.concatenate([X_train_b_2, X_train_c_2], axis=0)
 y_train_bc = np.concatenate([y_train_b, y_train_c], axis=0 )<train_model>
model.fit(train_X, train_y, batch_size=512, epochs=2, validation_data=(val_X, val_y))
model.fit(X_train_bc_2, y_train_bc )<merge>
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 pred_test_y = model.predict([test_X], batch_size=1024, verbose=0 )
test_data.loc[test_data['shop_id'] == 0, 'shop_id'] = 57
test_data.loc[test_data['shop_id'] == 1, 'shop_id'] = 58
test_data.loc[test_data['shop_id'] == 11, 'shop_id'] = 10
test_data['date_block_num'] = 34
lags = [1, 2, 3, 6, 12]
all_data['date_block_num'] = all_data['year_index'] * 12 + all_data['month'] - 1
for lag in lags:
lag_col_name = 'target_lag_' + str(lag)
 shifted = all_data[index_cols + ['target']].copy()
shifted.columns = index_cols + [lag_col_name]
shifted['date_block_num'] += lag
test_data = pd.merge(test_data, shifted, on=index_cols, how='left')
 test_data[lag_col_name].fillna(0, inplace=True)
 test_data = pd.merge(test_data, item_category_mapping, how='left', on='item_id')
 item_id_mean = all_data.groupby('item_id')['target'].mean()
test_data['item_target_enc_exp'] = test_data['item_id'].map(item_id_mean)
 test_data['item_target_enc_exp'].fillna(target_mean, inplace=True)
 shop_id_mean = all_data.groupby('shop_id')['target'].mean()
test_data['shop_target_enc_exp'] = test_data['shop_id'].map(shop_id_mean)
 test_data['shop_target_enc_exp'].fillna(target_mean, inplace=True)
 item_id_mean = all_data.groupby('item_category_id')['target'].mean()
test_data['item_category_target_enc_exp'] = test_data['item_category_id'].map(item_id_mean)
 test_data['item_category_target_enc_exp'].fillna(target_mean, inplace=True)
 test_data = pd.merge(test_data, last_sale_df, on=['shop_id', 'item_id', 'date_block_num'], how='left')
 test_data['last_sale_ago'].fillna(0, inplace=True)
 test_data = pd.merge(test_data, last_shop_sale_df, on=['shop_id', 'date_block_num'], how='left')
 test_data['last_shop_sale_ago'].fillna(0, inplace=True)
 test_data = pd.merge(test_data, last_item_sale_df, on=['item_id', 'date_block_num'], how='left')
 test_data['last_item_sale_ago'].fillna(0, inplace=True)
 test_data['year_index'] = 2
test_data['month'] = 11
test_data['days_in_month'] = 30
test_data.drop(columns='ID', inplace=True)
 test_data.drop(columns='date_block_num', inplace=True)
 test_data.head()<feature_engineering>
def f1_smart(y_true, y_pred):
thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = f1_score(y_true,(y_pred > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]
best_f1 = thresholds[0][1]
print("Best threshold: ", best_thresh)
 return best_f1, best_thresh
test_data_oh = test_data.drop(columns=['shop_id', 'item_id', 'item_category_id', 'year_index', 'month', 'last_sale_ago', 'last_shop_sale_ago', 'last_item_sale_ago'])
 for year in range(1,3):
test_data_oh['year_' + str(year)] = int(year == 2)
 for month in range(1, 13):
test_data_oh['month_' + str(month)] = int(month == 11)
 test_data_oh.head()<save_to_csv>
f1, threshold = f1_smart(val_y, pred_val_y)
 print('Optimal F1: {} at threshold: {}'.format(f1, threshold))
<load_from_csv><EOS> 
pred_test_y =(pred_test_y >threshold ).astype(int)
 out_df = pd.DataFrame({"qid":test["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )