Artificial intelligence April 04 ,2025

Developing a Sentiment Analysis Tool

Sentiment analysis is the process of determining the emotional tone behind a body of text. It is used to understand the attitudes, opinions, and emotions expressed within text data. A common application is analyzing customer reviews, social media posts, and other textual data to determine whether the sentiment is positive, negative, or neutral.

For this project, we will build a simple sentiment analysis tool using a deep learning model based on Recurrent Neural Networks (RNNs), specifically using LSTM (Long Short-Term Memory) networks. LSTM is a type of RNN that works well for sequences of data, such as text.

Step-by-Step Guide to Building a Sentiment Analysis Tool

Step 1: Install Required Libraries

We will use TensorFlow for building the neural network and Keras for its API. Additionally, we need nltk (Natural Language Toolkit) for text preprocessing and tokenization.

Install the required libraries by running:

pip install tensorflow nltk

Step 2: Load the Dataset

We’ll use the IMDB dataset, which contains movie reviews labeled as either positive or negative.

import tensorflow as tf
from tensorflow.keras.datasets import imdb

# Load the IMDB dataset
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)

Output:

  • x_train and x_test are the sequences of integers representing the words in the reviews.
  • y_train and y_test are the labels: 0 for negative sentiment and 1 for positive sentiment.

You can check the shape of the data:

print(x_train.shape)  # Output: (25000,)
print(y_train.shape)  # Output: (25000,)

Step 3: Preprocess the Data

Before training the model, we need to preprocess the text data. This includes padding the sequences to ensure that all inputs are of the same length.

# Pad sequences to ensure uniform input length
maxlen = 500  # Maximum number of words per review
x_train = tf.keras.preprocessing.sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = tf.keras.preprocessing.sequence.pad_sequences(x_test, maxlen=maxlen)

Output:

  • x_train and x_test are now padded sequences, each having the same length (500 words).

Step 4: Build the Model

Now, let's create the model using an LSTM layer. We'll use Embedding to convert word indices into dense vectors of fixed size, and an LSTM layer followed by a Dense layer for the final binary classification.

model = tf.keras.models.Sequential([
    tf.keras.layers.Embedding(10000, 128, input_length=maxlen),
    tf.keras.layers.LSTM(128),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

Here:

  • The Embedding layer maps each word to a 128-dimensional vector.
  • The LSTM layer processes the sequences and outputs a fixed-size vector.
  • The Dense layer outputs the sentiment classification.

Step 5: Compile the Model

We need to compile the model, specifying the optimizer, loss function, and evaluation metric.

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

No direct output here, but this step prepares the model for training.

Step 6: Train the Model

Now, let's train the model using the training data.

history = model.fit(x_train, y_train, epochs=5, batch_size=64, validation_data=(x_test, y_test))

Output:

  • The model will output the loss and accuracy for both the training and validation sets after each epoch:
Epoch 1/5
391/391 [==============================] - 15s 39ms/step - loss: 0.4512 - accuracy: 0.7878 - val_loss: 0.3253 - val_accuracy: 0.8658
Epoch 2/5
391/391 [==============================] - 14s 37ms/step - loss: 0.2991 - accuracy: 0.8751 - val_loss: 0.2974 - val_accuracy: 0.8805
...
Epoch 5/5
391/391 [==============================] - 14s 36ms/step - loss: 0.2150 - accuracy: 0.9164 - val_loss: 0.3244 - val_accuracy: 0.8716

Step 7: Evaluate the Model

After training, evaluate the model on the test dataset to see how well it generalizes to new data.

test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test Accuracy: {test_acc}")

Output:

  • The accuracy on the test set is printed, showing the final performance of the model.
Test Accuracy: 0.8716

Step 8: Visualize the Training Results

We can visualize the loss and accuracy over the epochs to understand the model's learning progress.

import matplotlib.pyplot as plt

# Plot training & validation accuracy values
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

Output:

  • A plot showing the model's accuracy and loss on both the training and test sets across the epochs. This helps visualize if the model is overfitting or underfitting.

Step 9: Make Predictions

Now, we can use the trained model to make predictions on new, unseen text.

sample_review = x_test[0]  # A sample review from the test set
prediction = model.predict(sample_review.reshape(1, -1))

print("Sentiment (0 = Negative, 1 = Positive):", prediction[0][0])

Output:

  • The model will output the predicted sentiment of the sample review.

Conclusion

In this step-by-step guide, you've learned how to build a sentiment analysis tool using LSTM (a type of RNN) on the IMDB dataset. We’ve covered everything from data preprocessing to training, evaluation, and visualizing results. You also saw how to use the model for predictions.

 

Next Blog-  Implementing a Recommendation System

Purnima
0

You must logged in to post comments.

Related Blogs

Artificial intelligence May 05 ,2025
Staying Updated in A...
Artificial intelligence May 05 ,2025
AI Career Opportunit...
Artificial intelligence May 05 ,2025
How to Prepare for A...
Artificial intelligence May 05 ,2025
Building an AI Portf...
Artificial intelligence May 05 ,2025
4 Popular AI Certifi...
Artificial intelligence May 05 ,2025
Preparing for an AI-...
Artificial intelligence May 05 ,2025
AI Research Frontier...
Artificial intelligence May 05 ,2025
The Role of AI in Cl...
Artificial intelligence May 05 ,2025
AI and the Job Marke...
Artificial intelligence May 05 ,2025
Emerging Trends in A...
Artificial intelligence April 04 ,2025
AI for Time Series F...
Artificial intelligence April 04 ,2025
Quantum Computing an...
Artificial intelligence April 04 ,2025
AI for Edge Devices...
Artificial intelligence April 04 ,2025
Explainable AI (XAI)
Artificial intelligence April 04 ,2025
Generative AI: An In...
Artificial intelligence April 04 ,2025
Implementing a Recom...
Artificial intelligence April 04 ,2025
Creating an Image Cl...
Artificial intelligence April 04 ,2025
Building a Spam Emai...
Artificial intelligence April 04 ,2025
AI in Social Media a...
Artificial intelligence April 04 ,2025
AI in Gaming and Ent...
Artificial intelligence April 04 ,2025
AI in Autonomous Veh...
Artificial intelligence April 04 ,2025
AI in Finance and Ba...
Artificial intelligence April 04 ,2025
Artificial Intellige...
Artificial intelligence April 04 ,2025
Responsible AI Pract...
Artificial intelligence April 04 ,2025
The Role of Regulati...
Artificial intelligence April 04 ,2025
Fairness in Machine...
Artificial intelligence April 04 ,2025
Ethics in AI Develop...
Artificial intelligence April 04 ,2025
Understanding Bias i...
Artificial intelligence April 04 ,2025
Working with Large D...
Artificial intelligence April 04 ,2025
Data Visualization w...
Artificial intelligence April 04 ,2025
Feature Engineering...
Artificial intelligence April 04 ,2025
Exploratory Data Ana...
Artificial intelligence April 04 ,2025
Exploratory Data Ana...
Artificial intelligence April 04 ,2025
Data Cleaning and Pr...
Artificial intelligence April 04 ,2025
Visualization Tools...
Artificial intelligence April 04 ,2025
Cloud Platforms for...
Artificial intelligence April 04 ,2025
Cloud Platforms for...
Artificial intelligence April 04 ,2025
Deep Dive into AWS S...
Artificial intelligence April 04 ,2025
Cloud Platforms for...
Artificial intelligence March 03 ,2025
Tool for Data Handli...
Artificial intelligence March 03 ,2025
Tools for Data Handl...
Artificial intelligence March 03 ,2025
Introduction to Popu...
Artificial intelligence March 03 ,2025
Introduction to Popu...
Artificial intelligence March 03 ,2025
Introduction to Popu...
Artificial intelligence March 03 ,2025
Introduction to Popu...
Artificial intelligence March 03 ,2025
Deep Reinforcement L...
Artificial intelligence March 03 ,2025
Deep Reinforcement L...
Artificial intelligence March 03 ,2025
Deep Reinforcement L...
Artificial intelligence March 03 ,2025
Implementation of Fa...
Artificial intelligence March 03 ,2025
Implementation of Ob...
Artificial intelligence March 03 ,2025
Implementation of Ob...
Artificial intelligence March 03 ,2025
Implementing a Basic...
Artificial intelligence March 03 ,2025
AI-Powered Chatbot U...
Artificial intelligence March 03 ,2025
Applications of Comp...
Artificial intelligence March 03 ,2025
Face Recognition and...
Artificial intelligence March 03 ,2025
Object Detection and...
Artificial intelligence March 03 ,2025
Image Preprocessing...
Artificial intelligence March 03 ,2025
Basics of Computer V...
Artificial intelligence March 03 ,2025
Building Chatbots wi...
Artificial intelligence March 03 ,2025
Transformer-based Mo...
Artificial intelligence March 03 ,2025
Word Embeddings (Wor...
Artificial intelligence March 03 ,2025
Sentiment Analysis a...
Artificial intelligence March 03 ,2025
Preprocessing Text D...
Artificial intelligence March 03 ,2025
What is NLP
Artificial intelligence March 03 ,2025
Graph Theory and AI
Artificial intelligence March 03 ,2025
Probability Distribu...
Artificial intelligence March 03 ,2025
Probability and Stat...
Artificial intelligence March 03 ,2025
Calculus for AI
Artificial intelligence March 03 ,2025
Linear Algebra Basic...
Artificial intelligence March 03 ,2025
AI vs Machine Learni...
Artificial intelligence March 03 ,2025
Narrow AI, General A...
Artificial intelligence March 03 ,2025
Importance and Appli...
Artificial intelligence March 03 ,2025
History and Evolutio...
Artificial intelligence March 03 ,2025
What is Artificial I...
Get In Touch

123 Street, New York, USA

+012 345 67890

techiefreak87@gmail.com

© Design & Developed by HW Infotech