<jupyter_start><jupyter_text>The above visualization clear shows that there is a class imbalance. Even among the toxic types.<jupyter_code># check for nulls
train.isnull().any(),test.isnull().any()
# split data by class
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
y = train[list_classes].values
y_test = test[list_classes].values
list_sentences_train = train["comment_text"]
list_sentences_test = test["comment_text"]

# clean data
special_character_removal=re.compile(r'[^a-z\d ]',re.IGNORECASE)
replace_numbers=re.compile(r'\d+',re.IGNORECASE)

def text_to_words(text):
    # remove special characters
    text=special_character_removal.sub('',text)
    
    # Replace Numbers with character 'n' 
    text=replace_numbers.sub('n', text)

    # Convert words to lower case and split them
    text = text.lower().split()
    
    # remove stop words
    stops = set(stopwords.words("english"))
    text = [w for w in text if not w in stops]
    text = " ".join(text)

    # shorten words to their stems
    text = text.split()
    stemmer = SnowballStemmer('english')
    stemmed_words = [stemmer.stem(word) for word in text]
        
    return stemmed_words

comments = []
for text in list_sentences_train:
    comments.append(text_to_words(text))
    
test_comments=[]
for text in list_sentences_test:
    test_comments.append(text_to_words(text))
# tokenize strings
max_features = 20000
maxlen = 200
tokenizer = Tokenizer(num_words=max_features,filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n\'', lower=True)
tokenizer.fit_on_texts(list(comments + test_comments))
comments_sequence = tokenizer.texts_to_sequences(comments)
test_comments_sequence = tokenizer.texts_to_sequences(test_comments)    
X_t = pad_sequences(comments_sequence , maxlen=maxlen)
X_te = pad_sequences(test_comments_sequence, maxlen=maxlen)

# embedding
# glove

embed_size =100

def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
embeddings_index = dict(get_coefs(*o.strip().split()) for o in open(embedding_file, encoding="utf8"))

all_embs = np.stack(embeddings_index.values())
emb_mean,emb_std = all_embs.mean(), all_embs.std()

word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))
for word, i in word_index.items():
    if i >= max_features: continue
    embedding_vector = embeddings_index.get(word)
    if embedding_vector is not None: embedding_matrix[i] = embedding_vector

# visualize word distribution
totalNumWords = [len(one_comment) for one_comment in list_tokenized_train]
plt.hist(totalNumWords,bins = np.arange(0,410,10))#[0,50,100,150,200,250,300,350,400])#,450,500,550,600,650,700,750,800,850,900])
plt.show()
# Define keras input layer
inp = Input(shape=(maxlen, )) #maxlen=200 as defined earlier

# define embedding layer
embed_size = 100
x = Embedding(max_features, embed_size, weights=[embedding_matrix])(inp)

# define lstm layer
x = LSTM(60, return_sequences=True,name='lstm_layer')(x)

# pooling & dropout
x = GlobalMaxPool1D()(x)
x = Dropout(0.1)(x)

# output layer
x = Dense(50, activation="relu")(x)
x = Dropout(0.1)(x)

# sigmoid for classification
x = Dense(6, activation="sigmoid")(x)

# create model
model = Model(inputs=inp, outputs=x)

model.summary()
# compile model
model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
from keras.models import load_model
model = load_model('model.h5')
# train model
batch_size = 1280
epochs = 2
model.fit(X_t, y, batch_size=batch_size, epochs=epochs, validation_split=0.1)
model.save('model_glove.h5')
batch_size = 1280
epochs = 2
predictions = model.predict(X_te, batch_size=batch_size, verbose=1)
print("roc auc score ", metrics.roc_auc_score(y_test, predictions))
print("accuracy ", metrics.accuracy_score(y_test, np.round(predictions).astype(np.int)))
print("precision score ", metrics.precision_score(y_test, np.round(predictions).astype(np.int),  average='weighted'))
print("recall score ", metrics.recall_score(y_test, np.round(predictions).astype(np.int),  average='weighted'))
print("f1 score ", metrics.f1_score(y_test, np.round(predictions).astype(np.int), average='weighted'))

print("coverage error ", metrics.coverage_error(y_test, predictions))
print("label ranking average precision score ", metrics.label_ranking_average_precision_score(y_test, predictions))
print("label ranking loss ", metrics.label_ranking_loss(y_test, predictions))<jupyter_output>roc auc score  0.9581067690074412
accuracy  0.873956411619399
precision score  0.5258955345703872
recall score  0.6822197680839315
f1 score  0.587851071991779
coverage error  0.2517588568212376
{'fraction_non_alphanumeric': 0.13137916490932097, 'fraction_numerical': 0.043863348797975536, 'mean_word_length': 6.026666666666666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '574891', 'license': 'MIT', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1626, 'n_words': 334, 'path': 'Toxic comments - lstm - glove.ipynb', 'repo_name': 'Kirupakaran/Toxic-comments-classification', 'size': '4742'}