text_plain_output_1.png
from nltk.corpus import stopwords

from sklearn.metrics import classification_report, confusion_matrix

from tensorflow.keras.callbacks import EarlyStopping

from tensorflow.keras.preprocessing.sequence import pad_sequences

import nltk

import numpy as np

import pandas as pd

import tensorflow as tf

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
import nltk
nltk.download('stopwords')
nltk.download('punkt')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import functools, re
import random

df = pd.read_csv('/kaggle/input/cyberbullying-classification/cyberbullying_tweets.csv')
df

df.cyberbullying_type.value_counts().plot.barh(xlim=(7800, 8000))

stopwords = [i.lower() for i in nltk.corpus.stopwords.words('english') + [chr(i) for i in range(97, 123)]]
x = df.tweet_text.apply(lambda text: re.sub('\\s+', ' ', ' '.join([i for i in re.sub('[^9A-Za-z ]', '', re.sub('\\n', '', re.sub('\\s+', ' ', re.sub('http\\S+', '', text.lower())))).split(' ') if i not in stopwords]))).values.astype(str)
x

tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=2000, oov_token='<OOV>')
tokenizer.fit_on_texts(x)
word_index = tokenizer.word_index
x_train = pad_sequences(tokenizer.texts_to_sequences(x_train), maxlen=100, padding='post', truncating='post')
x_test = pad_sequences(tokenizer.texts_to_sequences(x_test), maxlen=100, padding='post', truncating='post')
x_val = pad_sequences(tokenizer.texts_to_sequences(x_val), maxlen=100, padding='post', truncating='post')

model = tf.keras.Sequential([tf.keras.layers.Embedding(2000, 64), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, dropout=0.2, recurrent_dropout=0.2)), tf.keras.layers.Dropout(rate=0.2), tf.keras.layers.Dense(64, activation='swish'), tf.keras.layers.Dense(1, activation='sigmoid')])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy', 'AUC'])
model.summary()

early_stopping_monitor = EarlyStopping(patience=2)
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val), callbacks=[early_stopping_monitor])

y_pred = model.predict(x_test).round().T[0]
y_pred

y_rand = np.random.random(*y_test.shape).round().astype(int)
(y_rand == y_test).mean()

print(classification_report(y_test.astype(int), y_rand, digits=3))