A corpus of manually labelled comments and classify each comment
import numpy as np
import pandas as pd
import keras
import matplotlib.pyplot as plt
%matplotlib inline
import vis
#!wget http://bit.do/deep_toxic_train -O data/train.zip
df = pd.read_csv('data/train.zip')
df.head()
import keras
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
# Tokenizer
max_words = 2000
tokenizer = Tokenizer(num_words=max_words)
train_sentences = df["comment_text"]
train_sentences.head()
tokenizer.fit_on_texts(list(train_sentences))
# Index Representation
tokenizer_train = tokenizer.texts_to_sequences(train_sentences)
list(train_sentences)[1]
tokenizer_train[1], len(tokenizer_train[1])
#Select Padding
number_of_words = [len(comment) for comment in tokenizer_train]
plt.hist(number_of_words, bins=40);
maxlen = 200
X = pad_sequences(tokenizer_train, maxlen=maxlen, padding="post")
X.shape
X[1]
labels = df.iloc[:,2].values
labels.shape
# Baseline
1- df.iloc[:,2].sum()/ df.iloc[:,2].count()
from keras.utils import to_categorical
y = to_categorical(labels)
y.shape
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,
random_state=42)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
X_train[0]
from keras.models import Sequential
from keras.layers import Dense, Embedding, Dropout, Flatten, LSTM
model = Sequential()
model.add(Embedding(max_words, output_dim=128, input_length=maxlen))
model.add(LSTM(60))
model.add(Dropout(0.2))
model.add(Dense(2, activation="sigmoid"))
model.summary()
### Step 3: Complile & Fit model
model.compile(loss="binary_crossentropy", optimizer="rmsprop", metrics=["accuracy"])
%%time
history = model.fit(X_train, y_train, batch_size=32, epochs=1, verbose=2)