application_vnd.jupyter.stderr_output_1.png image_output_1.png
from keras.layers import LSTM,Embedding, Conv1D, Dense, Flatten, MaxPooling1D, Dropout , Bidirectional , Dropout , Flatten , GlobalMaxPooling1D

from keras.models import Sequential

from keras.preprocessing.sequence import pad_sequences

from keras.preprocessing.text import Tokenizer

from keras.preprocessing.text import Tokenizer

from sklearn.metrics import classification_report,confusion_matrix

from sklearn.model_selection import train_test_split

from tensorflow.keras.utils import plot_model

import keras

import pandas as pd

import re

import seaborn as sns

data_train = pd.read_csv('../input/nlp-getting-started/train.csv')
data_test = pd.read_csv('../input/nlp-getting-started/test.csv')

data_train = data_train[['id', 'text', 'target']]
data_test = data_test[['id', 'text']]

s = data_train.target.value_counts()

def clean_text(data):
    data['clean_text'] = data['text'].str.lower()
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('http\\S+', '', elem))
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('[^\\w\\s]', '', elem))
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('/n', '', elem))
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('\\d+', '', elem))
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('\\s+', ' ', elem))
    data['clean_text'] = data['clean_text'].apply(lambda elem: re.sub('\\s+[a-zA-Z]\\s+', ' ', elem))
    return data

data_train = clean_text(data_train)
data_test = clean_text(data_test)

max_fatures = 5000
tokenizer = Tokenizer(num_words=max_fatures, split=' ')
tokenizer.fit_on_texts(data_train['clean_text'].values)
X = tokenizer.texts_to_sequences(data_train['clean_text'].values)
X = pad_sequences(X, maxlen=31, padding='post')

Y = data_train['target'].values
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=0)

len(tokenizer.index_word)

embed_dim = 50
vocab_size = len(tokenizer.index_word) + 1
model1 = Sequential()
model1.add(Embedding(input_dim=vocab_size, input_length=31, output_dim=embed_dim))
model1.add(LSTM(30))
model1.add(Dropout(0.2))
model1.add(Flatten())
model1.add(Dense(1, activation='sigmoid'))
model1.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
from tensorflow.keras.utils import plot_model

keras.backend.clear_session()
batch_size = 32
history1 = model1.fit(X_train, Y_train, epochs=10, batch_size=batch_size, validation_data=(X_test, Y_test), verbose=1)

scores = model1.evaluate(X_test, Y_test, verbose=0)

predict = model1.predict(X_test)

predict1 = [1 if i > 0.5 else 0 for i in predict]

conf = confusion_matrix(Y_test, predict1)
conf