Kaggle competition: Jigsaw Unintended Bias in Toxicity Classification
<SOS> metric: JigsawBiasAUC Kaggle data source: jigsaw-unintended-bias-in-toxicity-classification<categorify>
np.random.seed(32)
 os.environ["OMP_NUM_THREADS"] = "4"
extracted_titles = test["Name"].str.extract('([A-Za-z]+)\.',expand=False)
 test["Title"] = extracted_titles.map(titles)
 test["Title"].head()<categorify>
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")
for column in ["Title","Cabin_type"]:
train = create_dummies(train,column)
 test = create_dummies(test,column)
 train.head()<define_variables>
train['target'] = np.where(train['target'] > 0.5, 1, 0)
 train['comment_text'] = train.comment_text.apply(lambda x: x.lower() )
predictor_columns = ['Age_categories_Missing', 'Age_categories_Infant',
'Age_categories_Child', 'Age_categories_Young Adult',
'Age_categories_Adult', 'Age_categories_Senior', 'Pclass_1', 'Pclass_3',
'Embarked_C', 'Embarked_Q', 'Embarked_S', 'SibSp_scaled',
'Parch_scaled', 'Fare_categories_0-12', 'Fare_categories_50-100',
'Fare_categories_100+', 'Title_Miss', 'Title_Mr', 'Title_Mrs',
'Title_Officer', 'Title_Royalty', 'Cabin_type_B', 'Cabin_type_C',
'Cabin_type_D', 'Cabin_type_E', 'Cabin_type_F', 'Cabin_type_G',
'Cabin_type_T', 'Cabin_type_Unknown']
all_X = train[predictor_columns]
all_y = train["Survived"]
lr = LogisticRegression()
selector = RFECV(lr, cv = 10)
 selector.fit(all_X, all_y)
 optimized_predictors = all_X.columns[selector.support_]
print(optimized_predictors )<train_on_grid>
y = train.target.values
X_train, X_valid, Y_train, Y_valid = train_test_split(train[['comment_text']], y, test_size = 0.1 )
all_X = train[optimized_predictors]
all_y = train["Survived"]
lr = LogisticRegression()
lr.fit(all_X, all_y)
 scores = cross_val_score(lr,all_X,all_y, cv=10)
 print(scores )<compute_test_metric>
embed_size = 300
max_features = 200000
max_len = 220
accuracy = scores.mean()
print(accuracy )<predict_on_test>
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 )
predictions = lr.predict(test[optimized_predictors] )<save_to_csv>
%%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
submission = pd.DataFrame({
"PassengerId": test["PassengerId"],
"Survived": predictions
})
 submission.to_csv('submission1.csv', index=False )<import_modules>
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
train_dataset = pd.read_csv('./.. /input/train.csv')
 test_dataset = pd.read_csv('./.. /input/test.csv' )<sort_values>
warnings.filterwarnings('ignore' )
print(train_dataset[["Parch", "Survived"]].groupby('Parch' ).mean().sort_values(by='Survived',ascending=False))<drop_column>
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
train_dataset.drop('Ticket',axis=1,inplace=True)
 test_dataset.drop('Ticket', axis=1,inplace=True )<feature_engineering>
model = build_model(lr = 1e-3, lr_d = 0, units = 128, dr = 0.3 )
train_dataset['Title'] = train_dataset.Name.str.extract('([A-Za-z]+)\.')
 train_dataset['Title'].replace(['Lady','Countess','Capt','Col','Don','Dr','Major','Rev','Sir','Jonkheer','Dona'],'Rare',inplace=True)
 test_dataset['Title'] = test_dataset.Name.str.extract('([A-Za-z]+)\.')
 test_dataset['Title'].replace(['Lady','Countess','Capt','Col','Don','Dr','Major','Rev','Sir','Jonkheer','Dona'],'Rare',inplace=True)
 train_dataset.groupby('Title' ).sum() ['Survived']<feature_engineering>
pred = model.predict(test_p, batch_size = 1024, verbose = 1 )
<count_values><EOS> 
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 )