text_plain_output_1.png
from nltk.corpus import stopwords

from nltk.stem import PorterStemmer

from sklearn.metrics import accuracy_score

from tensorflow.keras.layers import Dense

from tensorflow.keras.layers import Dropout

from tensorflow.keras.layers import Embedding

from tensorflow.keras.layers import LSTM

from tensorflow.keras.models import Sequential

from tensorflow.keras.preprocessing.sequence import pad_sequences

from tensorflow.keras.preprocessing.text import one_hot

import numpy as np

import pandas as pd

import re

df = pd.read_csv('/kaggle/input/covid19-india-news-headlines-for-nlp/raw_data.csv', usecols=['Headline', 'Sentiment', 'Description'])

df.shape

X = df.drop('Sentiment', axis=1)
Y = df['Sentiment']

messages = X.copy()

import re
import nltk
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
ps = PorterStemmer()
corpus = []
for i in range(len(messages)):
    review = re.sub('[^a-zA-Z]', ' ', messages['Headline'][i])
    review = review.lower()
    review = review.split()
    review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus.append(review)

voc_size = 5000

onehot_repr = [one_hot(words, voc_size) for words in corpus]

sent_length = 20
embedded_docs = pad_sequences(onehot_repr, padding='pre', maxlen=sent_length)

from tensorflow.keras.layers import Dropout
embedding_vector_features = 40
model = Sequential()
model.add(Embedding(voc_size, embedding_vector_features, input_length=sent_length))
model.add(Dropout(0.5))
model.add(LSTM(200))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

X_final = np.array(embedded_docs)
y_final = np.array(Y)

model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=64)

y_pred = np.argmax(model.predict(X_test), axis=-1)
from sklearn.metrics import accuracy_score
print(f'{accuracy_score(y_test, y_pred) * 100}%')