Kaggle competition: Quora Insincere Questions Classification
cols_with_missing = [col for col in X.columns if X[col].isnull().any() ]
print(cols_with_missing)
 print()
print(len(cols_with_missing))
cols_with_missing_test = [col for col in X_test.columns if X_test[col].isnull().any() ]
print(cols_with_missing_test)
 print()
print(len(cols_with_missing_test))<count_missing_values>
zip_ref = zipfile.ZipFile('/kaggle/input/quora-insincere-questions-classification/embeddings.zip', 'r')
 print(zip_ref.namelist())
 embeddings = zip_ref.open('glove.840B.300d/glove.840B.300d.txt', 'r' )
for col in cols_with_missing:
print(col + " : " + str(X[col].isnull().sum()))<drop_column>
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.decode().split(" ")) for o in embeddings )
X.drop(['Alley', 'PoolQC', 'Fence', 'MiscFeature'], axis=1, inplace=True)
 X_test.drop(['Alley', 'PoolQC', 'Fence', 'MiscFeature'], axis=1, inplace=True )<define_variables>
import tensorflow as tf
from sklearn.model_selection import train_test_split
cols_with_missing = [col for col in X.columns if X[col].isnull().any() ]
cat_cols = [col for col in X.columns if X[col].dtypes=='object']
cols_with_missing_test = [col for col in X_test.columns if X_test[col].isnull().any() ]
cat_cols_test = [col for col in X_test.columns if X_test[col].dtypes=='object']<drop_column>
stopwords = [ "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "could", "did", "do", "does", "doing", "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "it", "it's", "its", "itself", "let's", "me", "more", "most", "my", "myself", "nor", "of", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "she", "she'd", "she'll", "she's", "should", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "we", "we'd", "we'll", "we're", "we've", "were", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "would", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself", "yourselves" ]
X.drop(['LotConfig', 'LandSlope'], axis=1, inplace=True)
 X_test.drop(['LotConfig', 'LandSlope'], axis=1, inplace=True)
 cat_cols = [col for col in X.columns if X[col].dtypes=='object']
cat_cols_test = [col for col in X_test.columns if X_test[col].dtypes=='object']<drop_column>
train_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')
 test_data = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv' )
X.drop(['YrSold', 'MoSold', 'MiscVal', '3SsnPorch', 'BsmtHalfBath', 'LowQualFinSF', 'BsmtFinSF2', 'MSSubClass'],
axis=1, inplace=True)
 X_test.drop(['YrSold', 'MoSold', 'MiscVal', '3SsnPorch', 'BsmtHalfBath', 'LowQualFinSF', 'BsmtFinSF2', 'MSSubClass'],
axis=1, inplace=True)
 num_cols = [col for col in X.columns if X[col].dtypes in ['int64', 'float64']]
num_cols_test = [col for col in X_test.columns if X_test[col].dtypes in ['int64', 'float64']]<split>
print(train_data.columns)
 train_input = list(train_data['question_text'])
 train_label = list(train_data['target'])
 test_input = list(test_data['question_text'] )
X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0 )<set_options>
def remove_stop_words(x):
for word in stopwords:
token = " " + word + " "
if(x.find(token)!= -1):
x = x.replace(token, " ")
 return x
train_input_rsw = list(map(remove_stop_words, train_input))
test_input_rsw = list(map(remove_stop_words, test_input))
pd.options.mode.chained_assignment = None<create_dataframe>
tokenizer = Tokenizer()
tokenizer.fit_on_texts(train_input_rsw)
 word_index = tokenizer.word_index
embedding_matrix = np.zeros(( len(word_index)+1, 300))
for word, index in word_index.items() :
embedding_vector = embeddings_index.get(word)
 if(embedding_vector is not None):
embedding_matrix[index] = embedding_vector
print(embedding_matrix.shape )
cat_cols_with_missing = [col for col in cat_cols if col in cols_with_missing]
cat_cols_with_missing_test = [col for col in cat_cols_test if col in cols_with_missing_test]
cat_imputer = SimpleImputer(strategy='most_frequent')
 X_train_cat_imputed = pd.DataFrame(cat_imputer.fit_transform(X_train[cat_cols_with_missing]))
X_valid_cat_imputed = pd.DataFrame(cat_imputer.transform(X_valid[cat_cols_with_missing]))
X_train_cat_imputed.columns = X_train[cat_cols_with_missing].columns
X_valid_cat_imputed.columns = X_valid[cat_cols_with_missing].columns
X_train_cat_imputed.index = X_train.index
X_valid_cat_imputed.index = X_valid.index
X_train.drop(cat_cols_with_missing, axis=1, inplace=True)
 X_valid.drop(cat_cols_with_missing, axis=1, inplace=True)
 X_train_no_missing_cat = pd.concat([X_train, X_train_cat_imputed], axis=1)
 X_valid_no_missing_cat = pd.concat([X_valid, X_valid_cat_imputed], axis=1)
 cat_imputer_for_test = SimpleImputer(strategy='most_frequent')
 cat_imputer_for_test.fit(X_train_no_missing_cat[cat_cols_with_missing_test])
 X_test_cat_imputed = pd.DataFrame(cat_imputer_for_test.transform(X_test[cat_cols_with_missing_test]))
X_test_cat_imputed.columns = X_test[cat_cols_with_missing_test].columns
X_test_cat_imputed.index = X_test.index
X_test.drop(cat_cols_with_missing_test, axis=1, inplace=True)
 X_test_no_missing_cat = pd.concat([X_test, X_test_cat_imputed], axis=1 )<categorify>
del embeddings_index
gc.collect()
cb_enc = ce.CatBoostEncoder(cols=cat_cols)
 cb_enc.fit(X_train_no_missing_cat[cat_cols], y_train)
 train_encoded_cb = X_train_no_missing_cat.join(cb_enc.transform(X_train_no_missing_cat[cat_cols] ).add_suffix('_cb'))
valid_encoded_cb = X_valid_no_missing_cat.join(cb_enc.transform(X_valid_no_missing_cat[cat_cols] ).add_suffix('_cb'))
test_encoded_cb = X_test_no_missing_cat.join(cb_enc.transform(X_test_no_missing_cat[cat_cols] ).add_suffix('_cb'))
train_encoded_cb.drop(cat_cols, axis=1, inplace=True)
 valid_encoded_cb.drop(cat_cols, axis=1, inplace=True)
 test_encoded_cb.drop(cat_cols, axis=1, inplace=True )<categorify>
max_length = 30
imputer = KNNImputer(n_neighbors=100, weights='distance', metric='nan_euclidean')
 imputed_train_num_cols = pd.DataFrame(imputer.fit_transform(train_encoded_cb[num_cols]))
imputed_valid_num_cols = pd.DataFrame(imputer.transform(valid_encoded_cb[num_cols]))
imputed_test_num_cols = pd.DataFrame(imputer.transform(test_encoded_cb[num_cols]))
imputed_train_num_cols.columns = train_encoded_cb[num_cols].columns
imputed_valid_num_cols.columns = valid_encoded_cb[num_cols].columns
imputed_test_num_cols.columns = test_encoded_cb[num_cols].columns
imputed_train_num_cols.index = train_encoded_cb[num_cols].index
imputed_valid_num_cols.index = valid_encoded_cb[num_cols].index
imputed_test_num_cols.index = test_encoded_cb[num_cols].index
train_encoded_cb.drop(num_cols, axis=1, inplace=True)
 valid_encoded_cb.drop(num_cols, axis=1, inplace=True)
 test_encoded_cb.drop(num_cols, axis=1, inplace=True)
 train_data = train_encoded_cb.join(imputed_train_num_cols)
 valid_data = valid_encoded_cb.join(imputed_valid_num_cols)
 test_data = test_encoded_cb.join(imputed_test_num_cols )<train_model>
sequences = tokenizer.texts_to_sequences(train_input_rsw)
 train_input_padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
 print(train_input_padded.shape)
 sequences = tokenizer.texts_to_sequences(test_input_rsw)
 test_input_padded = pad_sequences(sequences, maxlen=max_length, padding='post', truncating='post')
 print(test_input_padded.shape )
ada_boost = AdaBoostRegressor(base_estimator=XGBRegressor(verbosity=0))
ada_boost.fit(train_data, y_train )<train_model>
has_cv = False
test_split_size = 0.1 if has_cv else 0
if(has_cv):
train_text, cv_text, train_target, cv_target = train_test_split(train_input_padded, train_label, test_size = test_split_size, random_state=2)
 else:
train_text = train_input_padded
train_target = train_label
print(f'Train Input Shape : {len(train_text)}')
 print(f'Train Label Shape : {len(train_target)}')
 if(has_cv):
print(f'CV Input Shape : {len(cv_text)}')
 print(f'CV label Shape : {len(cv_target)}' )
xgb_model = XGBRegressor()
xgb_model.fit(train_data, y_train )<train_model>
tf.random.set_seed(123)
 model = tf.keras.models.Sequential([
tf.keras.layers.Embedding(len(word_index)+1, 300, input_length=max_length, weights=[embedding_matrix], trainable=False),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128,return_sequences=True)) ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv1D(64, 5, activation='relu'),
tf.keras.layers.MaxPooling1D(pool_size=4),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(1, activation="sigmoid")
 ])
 model.summary()
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"] )
lgb_model = lgb.LGBMRegressor()
lgb_model.fit(train_data, y_train )<train_model>
epochs = 5
if(has_cv):
history = model.fit(np.array(train_text), np.array(train_target), epochs = epochs, validation_data=(np.array(cv_text),np.array(cv_target)) , batch_size=1024)
 else:
history = model.fit(np.array(train_text), np.array(train_target), epochs = epochs, batch_size=1024 )
rf_model = RandomForestRegressor()
rf_model.fit(train_data, y_train )<train_model>
if(has_cv):
cv_predictions = model.predict(cv_text, batch_size=1024)
 thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 result = f1_score(cv_target,(cv_predictions>thresh ).astype(int))
thresholds.append([thresh, result])
 print("F1 score at threshold {0} is {1}".format(thresh, result))
thresholds.sort(key=lambda x: x[1], reverse=True)
 print("Best value {0}".format(thresholds[0]))
best_thresh = thresholds[0]
else:
best_thresh = 0.29
gb_model = GradientBoostingRegressor()
gb_model.fit(train_data, y_train )<train_model>
if(has_cv):
predictions = model.predict(cv_text)
 predictions = np.around(predictions ).astype(int)
 df = pd.DataFrame({'pred': predictions.flatten() , 'actual': cv_target})
 df.head()
pd.crosstab(df['pred'], df['actual'], margins=True )
et_model = ExtraTreesRegressor()
et_model.fit(train_data, y_train )<compute_test_metric>
predictions = model.predict(test_input_padded )
print("AdaBoost Score : ",ada_boost.score(valid_data, y_valid))
print("XGBoost Score : ",xgb_model.score(valid_data, y_valid))
print("LightGBM Score : ",lgb_model.score(valid_data, y_valid))
print("Random Forests Score : ",rf_model.score(valid_data, y_valid))
print("Gradient Boosting Score: ",gb_model.score(valid_data, y_valid))
print("Extra Trees Score : ",et_model.score(valid_data, y_valid))<define_search_space>
predictions =(predictions>best_thresh ).astype(int)
 output = pd.DataFrame({'qid': test_data.qid, 'prediction': predictions.flatten() })
 output.to_csv('submission.csv', index=False )