Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<compute_train_metric>
%matplotlib inline
%matplotlib inline
scores_voting = f1_score(y_test, model_voting.predict(X_test), average = 'macro')
 print('voting, f1_score=',scores_voting.mean() )<train_model>
path = '.. /input/'
comp = 'jigsaw-toxic-comment-classification-challenge/'
TRAIN_DATA_FILE=f'{path}{comp}train.csv.zip'
TEST_DATA_FILE=f'{path}{comp}test.csv.zip'
TEST_LABELS_FILE = f'{path}{comp}test_labels.csv.zip'
clf_gradient = GradientBoostingClassifier(n_estimators=800, learning_rate=0.5,
max_depth=3, random_state=8)
 model_gradient = clf_gradient.fit(X_train,y_train )<compute_test_metric>
train = pd.read_csv(TRAIN_DATA_FILE)
 test = pd.read_csv(TEST_DATA_FILE)
 test_l = pd.read_csv(TEST_LABELS_FILE)
 sample_submission = pd.read_csv(f'{path}{comp}sample_submission.csv.zip' )
scores_gradient = f1_score(y_test, model_gradient.predict(X_test), average = 'macro')
 print('GradientBoosting f1_score=',scores_gradient.mean() )<choose_model_class>
embed_size = 300
max_features = 20000
maxlen = 200
clf_xgb = XGBClassifier(max_depth=4,booster='dart',
min_child_weight=1,
learning_rate=0.01,
n_estimators=500,
silent=True,
objective='multi:softprob',
gamma=0,
max_delta_step=0,
subsample=0.9,
colsample_bytree=0.9,
colsample_bylevel=1,
reg_alpha=0,
reg_lambda=0,
scale_pos_weight=1,
seed=0,
missing=None)
 model_xgb = clf_xgb.fit(X_train, y_train, eval_metric='mlogloss', verbose=100,
eval_set=[(X_test, y_test)], early_stopping_rounds=20 )<compute_train_metric>
train.isnull().any() ,test.isnull().any()
scores_xgb = f1_score(y_test, model_xgb.predict(X_test), average = 'macro')
 print('xgb f1_score=',scores_xgb.mean() )<choose_model_class>
def clean_text(text):
text = text.lower()
text = re.sub(r"what's", "what is ", text)
 text = re.sub(r"'s", " ", text)
 text = re.sub(r"'ve", " have ", text)
 text = re.sub(r"can't", "cannot ", text)
 text = re.sub(r"n't", " not ", text)
 text = re.sub(r"i'm", "i am ", text)
 text = re.sub(r"'re", " are ", text)
 text = re.sub(r"'d", " would ", text)
 text = re.sub(r"'ll", " will ", text)
 text = re.sub(r"'scuse", " excuse ", text)
 text = re.sub('\W', ' ', text)
 text = re.sub('\s+', ' ', text)
 text = re.sub(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', ' _ip_ ', text)
 text = re.sub(r'(['"\.\(\)\!\?\-\\\/\,])', r' \1 ',text)
 text = re.sub(r'([\;\:\|•«
])', ' ', text)
 text = text.replace('&', ' and ')
 text = text.replace('@', ' at ')
 text = text.replace('0', ' zero ')
 text = text.replace('1', ' one ')
 text = text.replace('2', ' two ')
 text = text.replace('3', ' three ')
 text = text.replace('4', ' four ')
 text = text.replace('5', ' five ')
 text = text.replace('6', ' six ')
 text = text.replace('7', ' seven ')
 text = text.replace('8', ' eight ')
 text = text.replace('9', ' nine ')
 text = text.strip(' ')
 return text
clf_lgbm = lgb.LGBMClassifier(class_weight='balanced', boosting_type='dart',
drop_rate=0.9, min_data_in_leaf=100,
max_bin=255,
n_estimators=500,
bagging_fraction=0.01,
min_sum_hessian_in_leaf=1,
importance_type='gain',
learning_rate=0.1,
max_depth=-1,
num_leaves=31)
 model_lgbm = clf_lgbm.fit(X_train, y_train);<compute_train_metric>
train['comment_text'] = train['comment_text'].map(lambda com : clean_text(com))
scores_lgbm = f1_score(y_test, model_lgbm.predict(X_test), average = 'macro')
 print('lgbm f1_score=',scores_lgbm.mean() )<feature_engineering>
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
y = train[list_classes].values
list_sentences_train = train["comment_text"]
list_sentences_test = test["comment_text"]
scores_algorithm = pd.DataFrame(columns=['scores'])
 scores_algorithm.loc['kNN_distance'] = scores_knn_distance[scores_knn_distance.neighbours==10].score.values
scores_algorithm.loc['kNN_uniform'] = scores_knn_distance[scores_knn_uniform.neighbours==4].score.values
scores_algorithm.loc['SVM'] = scores_svm
scores_algorithm.loc['SGD'] = scores_sgd
scores_algorithm.loc['Neural Network'] = scores_mlp
scores_algorithm.loc['AdaBoosting'] = scores_ada
scores_algorithm.loc['voting'] = scores_voting
scores_algorithm.loc['GradientBoosting'] = scores_gradient
scores_algorithm.loc['XGBoosting'] = scores_xgb
scores_algorithm.loc['LGBM'] = scores_lgbm
scores_algorithm.plot(kind='bar', use_index=True, y='scores')
 plt.ylabel("f1_score")
 plt.title("The f1_score of different algorithms" )<save_to_csv>
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(list_sentences_train))
list_tokenized_train = tokenizer.texts_to_sequences(list_sentences_train)
 list_tokenized_test = tokenizer.texts_to_sequences(list_sentences_test)
 X_t = pad_sequences(list_tokenized_train, maxlen=maxlen)
 X_te = pad_sequences(list_tokenized_test, maxlen=maxlen )
clf_knn_distance = neighbors.KNeighborsClassifier(n_neighbors=10, weights="distance")
 model_knn_distance = clf_knn_distance.fit(X_train,y_train)
 test['Target'] = model_knn_distance.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_kNN_10_distance.csv",index=False,sep=',' )<save_to_csv>
comp = 'crawl300d2m/'
embedding_path = f'{path}{comp}crawl-300d-2M.vec'
clf_knn_uniform = neighbors.KNeighborsClassifier(n_neighbors=4, weights="uniform")
 model_knn_uniform = clf_knn_uniform.fit(X_train,y_train)
 test['Target'] = model_knn_uniform.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_kNN_4_uniform.csv",index=False,sep=',' )<save_to_csv>
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))
test['Target'] = model_svm.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_svm.csv",index=False,sep=',' )<save_to_csv>
word_index = tokenizer.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
test['Target'] = model_sgd.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_sgd.csv",index=False,sep=',' )<save_to_csv>
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense, Dropout, Activation, LSTM, Bidirectional, GlobalMaxPool1D, Embedding,AveragePooling1D
from tensorflow.keras import Input, Model
from tensorflow.keras import layers, initializers, regularizers, constraints, optimizers
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.compat.v1.keras.layers import CuDNNGRU, CuDNNLSTM
test['Target'] = model_mlp.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_neural_network.csv",index=False,sep=',' )<save_to_csv>
from tensorflow.keras.layers import LeakyReLU
from keras.layers import GRU
test['Target'] = model_ada.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_adaboosting.csv",index=False,sep=',' )<save_to_csv>
x=0
model = 0
test['Target'] = model_voting.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_voting.csv",index=False,sep=',' )<save_to_csv>
maxlen=200
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x= SpatialDropout1D(0.2 )(x)
 x = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x = Conv1D(64, kernel_size = 3, padding = "valid", kernel_initializer = "glorot_uniform" )(x)
 avg_pool = GlobalAvgPool1D()(x)
 max_pool = GlobalMaxPool1D()(x)
 conc = concatenate([avg_pool,max_pool])
 x = Dense(50 )(conc)
 x = LeakyReLU(alpha = 0.01 )(x)
 x = Dropout(0.25 )(x)
 x = Dense(6, activation = "sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 model.summary()
test['Target'] = model_gradient.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_gradientboosting.csv",index=False,sep=',' )<save_to_csv>
model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )
test['Target'] = model_xgb.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_XGboosting.csv",index=False,sep=',' )<save_to_csv>
file_path = "bi_gru.best.hdf5"
checkpoint = ModelCheckpoint(file_path, monitor='val_loss',verbose=1, save_best_only=True, mode = 'min')
 early = EarlyStopping(monitor="val_loss", mode="min",patience=1 )
test['Target'] = model_lgbm.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_LGBM.csv",index=False,sep=',' )<save_to_csv>
batch_size = 32
epochs = 10
callbacks_list = [checkpoint,early]
history = model.fit(X_t,y, batch_size=batch_size, epochs=epochs, validation_split=0.1, callbacks=callbacks_list )
df_head = df[df.parentesco1==1]
X_head = df_head.drop(['Id', 'idhogar', 'Target'], axis=1 ).fillna(0)
 y_head = df_head.Target
model_lgbm_final = clf_lgbm.fit(X_head,y_head)
 test['Target'] = model_lgbm.predict(test_X)
 result = test[['Id','Target']]
result.to_csv("result_LGBM_final.csv",index=False,sep=',' )<save_to_csv>
batch_size = 1024
results = model.predict(X_te, batch_size=batch_size,verbose = 1 )
<set_options><EOS> 
sample_submission[list_classes] = results
sample_submission.to_csv('fin.csv', index=False)
 files.download('fin.csv' )