Exploring Customer Sentiments: Analyzing Amazon Product Reviews with Dash.

Oluwakemi Helen Deniran
14 min readMar 17, 2024
Photo by Christian Wiediger on Unsplash

Customer sentiments have a significant impact in an increasingly interconnected society, particularly in e-commerce. Understanding these feelings is critical for firms to adapt, evolve, and succeed in today’s competitive environment. Among the several forums for customer expression, Amazon stands out as a hotbed of consumer feedback, with a massive collection of product reviews spanning almost every conceivable item.

In this era of data-driven decision-making, collecting relevant insights from this vast volume of customer input is critical. Enter the world of sentiment analysis, a powerful tool that uses customers’ collective voice to gain vital insights into their preferences, satisfaction levels, and pain concerns. We start on a trip through sentiment analysis to decipher the nuanced language of Amazon product evaluations, revealing the attitudes that drive consumer perceptions and purchasing decisions.

We offer an interactive investigation of Amazon product evaluations, powered by cutting-edge technology and new approaches. Using Dash, a dynamic web application framework, we provide an interactive platform for dissecting and evaluating client attitudes with unprecedented depth and clarity. Join us as we explore the intricate tapestry of customer opinions, uncovering the undiscovered stories hidden behind the huge expanse of Amazon’s digital marketplace.

INTRODUCTION

In this research, we will thoroughly investigate natural language processing (NLP) approaches and sentiment analysis procedures. Our major goal is to assess and identify the attitudes represented in a collection of product reviews acquired from an online platform, with a special emphasis on Amazon.

The project progresses via a succession of high-level processes, each of which contributes to the overall goal of understanding client attitudes and views. These steps include:

Data Preparation: We start by gathering and preparing a dataset of product reviews from an internet site. This includes retrieving essential information, dealing with missing data, and guaranteeing data integrity.

Exploratory Data Analysis (EDA): Using the dataset, we perform exploratory data analysis to acquire insights into the distribution of reviews, discover essential aspects, and identify potential patterns or trends.

Data Preprocessing: The next step is to prepare the textual data for sentiment analysis. This involves activities like text cleaning, tokenization, and normalization to assure uniformity and eliminate noise.

Sentiment Labeling: We assign a sentiment class to each review, indicating whether it is good, negative, or neutral based on the attitudes conveyed in the text. This phase lays the groundwork for future sentiment analysis tasks.

Text Vectorization: To allow machine learning algorithms to interpret textual input, we convert the text to numerical representations using techniques like TF-IDF vectorization or word embeddings.

Model Development: Using the labeled dataset, we train machine learning models to classify the sentiment in product reviews. We investigated many algorithms, including logistic regression, random forest, support vector machines, and neural networks, to determine the most successful sentiment classification model.

Model Evaluation: We assess the performance of each trained model using metrics such as accuracy, precision, recall, and F1-score. This allows us to examine the algorithms’ accuracy in predicting sentiment labels.

Sentiment Analysis Dashboard: Finally, we create an interactive dashboard with Dash, a Python web application framework. This dashboard allows users to examine and visualize sentiment trends in product evaluations, providing valuable data for organizations and marketers.

ASSUMPTIONS

The product review dataset is representative of the target population and broad enough to capture a variety of sentiments.
The sentiment labels assigned to each review appropriately represent the sentiments conveyed in the text.
Text preparation approaches successfully remove noise and irrelevant information from reviews, increasing the quality of sentiment analysis.
Machine learning models trained on the labeled dataset are good at generalizing to new data and making trustworthy sentiment label predictions.
The sentiment analysis dashboard offers a straightforward and user-friendly interface for discovering and evaluating sentiment trends in product reviews.

METHODOLOGY

Data Preparation:

During the project’s first phase, the dataset was methodically gathered from a reliable source, namely GitHub. The information, which included 30,847 Amazon product reviews, offered substantial issues due to its raw and unstructured nature. To solve this, a sophisticated data cleaning procedure was implemented. To verify the dataset’s integrity and quality, noise, including special characters, HTML elements, and superfluous information, was removed. The cleaned data was then turned into a structured format, namely a Pandas Data-Frame, which allows for smooth manipulation and analysis. This conversion was critical in speeding subsequent data exploration and preparation procedures, laying the groundwork for reliable sentiment analysis.

Data Cleaning:

Prior to analysis, the dataset was thoroughly cleaned to ensure correctness and consistency. This included deleting duplicate entries, resolving missing values, and standardizing text formatting. Special characters, HTML elements, and punctuation were removed to ensure consistency among reviews. Additionally, unnecessary metadata, such as timestamps and product IDs, were taken out, leaving only the review content. These thorough cleaning procedures yielded a streamlined dataset that was devoid of noise and errors, allowing for additional analysis and interpretation.

# Load the DataFrame from the text file
file_path = r"C:\Users\Great Woman\Downloads\Amazon Product Review.txt"
df = pd.read_csv(file_path)

# 1. Handle Missing Values
missing_values = df.isnull().sum() # Check for missing values
df.dropna(inplace=True) # Drop rows with any missing values

# 2. Convert Data Types
df['review_date'] = pd.to_datetime(df['review_date']) # Convert 'review_date' to datetime

# 3. Remove Duplicates
duplicates = df.duplicated().sum() # Check for duplicates
df.drop_duplicates(inplace=True)

# 4. Text Cleaning (Example: Removing stopwords)
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
df['review_body'] = df['review_body'].apply(lambda x: ' '.join([word for word in x.split() if word.lower() not in stop_words]))

# Display the cleaned DataFrame
print("Cleaned DataFrame:")
print(df.head())

# Display missing values and duplicates information
print("\nMissing Values:")
print(missing_values)
print("\nDuplicates:", duplicates)

EXPLORATORY DATA ANALYSIS

During the Exploratory Data Analysis (EDA) phase, we examined various aspects of the dataset to understand its structure and contents. Here are some key aspects from the EDA:

Data Overview: We started by examining the general structure of the dataset, including the number of rows and columns, data types, and any missing values.

Data Overview

Summary Statistics: We calculated descriptive statistics such as mean, median, minimum, maximum, and standard deviation for numerical variables to understand their distribution and central tendencies.

statistical information of dataset

Distribution of Sentiments: We visualized the distribution of sentiments (positive, negative) in the dataset to understand the overall sentiment distribution and potential class imbalances.

Word Frequency Analysis: We conducted word frequency analysis to identify the most common words and phrases used in the reviews. This helped us gain insights into the language used by customers in their reviews.

Word Frequency Analysis

Word Cloud Visualization: We created word clouds to visualize the most frequently occurring words in the dataset. This provided a more intuitive representation of the prominent terms used in the reviews.

Correlation Analysis: We explored potential correlations between different variables, such as the length of reviews and the sentiment expressed. This analysis helped us understand relationships within the data.

Time-Series Analysis: we conducted time-series analysis to examine how number of reviews changed over time.

Sentiment Distribution Across Categories: We analyzed how sentiments vary across different product categories or other relevant variables. This helped us identify any patterns or trends specific to certain categories.

DATA PRE-PROCESSING PHASE

After completing the Exploratory Data Analysis (EDA), the next step is data preprocessing. This involves preparing the dataset for sentiment analysis by cleaning and transforming the text data. Below are the key tasks involved in the data preprocessing phase:

  1. Text Cleaning: We performed various text cleaning operations to remove noise, irrelevant characters, and special symbols from the text data. This typically includes removing punctuation, digits, and special characters, as well as converting text to lowercase.
  2. Tokenization: We tokenized the text data, splitting it into individual words or tokens. This step is essential for further analysis, as it breaks down the text into meaningful units for processing.
# Tokenize the preprocessed text data
tokenized_text = [text.split() for text in df['preprocessed_review']]

# Create a dictionary representation of the documents
dictionary = corpora.Dictionary(tokenized_text)

# Print token to ID mapping
print(dictionary.token2id)
Tokenization

Stopword Removal: We removed common stopwords from the text data. Stopwords are commonly occurring words (e.g., “the”, “and”, “is”) that do not carry significant meaning and can be safely excluded from analysis.

Lemmatization or stemming: To standardize words, we used lemmatization or stemming approaches that reduced them to their base or root form. This helps to reduce the dataset’s dimensionality while also boosting the sentiment analysis model’s performance.

Feature Engineering: We created additional features or engineered new variables from the text data, such as word count, average word length, or sentiment scores obtained from external libraries like TextBlob.

Feature Engineering

SENTIMENT LABELING

The next phase is sentiment labeling. During this step, we assign sentiment labels to each review in the dataset based on its content. Typically, sentiment labeling entails categorizing reviews as good, negative, or neutral depending on the sentiment represented in the text.

Here are the main tasks in sentiment labeling:

We assign each review to one of three predefined sentiment groups (positive, negative, or neutral) based on the overall sentiment indicated in the text. This classification can be done either manually by human annotators or automatically by machine learning models trained on labeled data.
Once sentiment labeling is completed, each review in the dataset will be connected with a sentiment label, allowing us to proceed with text vectorization and sentiment model construction.

# Define a function to perform sentiment analysis and return the sentiment label
def get_sentiment_label(text):
analysis = TextBlob(text)
# Determine the sentiment polarity
if analysis.sentiment.polarity > 0:
return 'Positive'
elif analysis.sentiment.polarity < 0:
return 'Negative'
else:
return 'Neutral'

# Apply the function to each text in your DataFrame and create a new column for sentiment labels
df['sentiment'] = df['preprocessed_review'].apply(get_sentiment_label)

# Display the DataFrame with sentiment labels
print(df[['review_id', 'preprocessed_review', 'sentiment']].head())

TEXT VECTORIZATION AND MODEL BUILDING

These tasks include translating text data to numerical representations and building machine learning models to predict review sentiment based on features.

Here’s a summary of text vectorization and model development:

Text Vectorization: Raw text data cannot be incorporated directly into machine learning algorithms. As a result, we need to translate the language into numerical representations that models can interpret. Text vectorization algorithms often used for this purpose include Bag-of-Words, TF-IDF (Term Frequency-Inverse Document Frequency), and word embeddings (e.g., Word2Vec, GloVe). These methods convert each review into a vector or matrix of numerical features.


# Initialize the TF-IDF vectorizer
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, min_df=5, stop_words='english')

# Fit and transform the preprocessed text data
tfidf_matrix = tfidf_vectorizer.fit_transform(df['preprocessed_review'])

# Print the shape of the TF-IDF matrix
print("Shape of TF-IDF matrix:", tfidf_matrix.shape)

Feature Extraction: During text vectorization, we extract significant features from the text data to capture the reviews’ semantics and context. These characteristics may Word frequencies, n-grams (near word sequences), and dense vector representations derived from word embeddings are some examples. The features selected are determined by the intricacy of the text data and the sentiment analysis model’s performance requirements.

Model Selection: Once the text input has been vectorized, we choose suitable machine learning techniques or deep learning architectures for sentiment analysis. Logistic regression, support vector machines (SVM), random forests, recurrent neural networks (RNNs), and convolutional neural networks (CNNs) are among the most widely used models. Model selection is determined by criteria such as dataset size, text feature complexity, and desired prediction accuracy.

Model Training: After choosing the model, we train it with the vectorized text data and related Sentiment labels. During the training process, the model learns to identify patterns in text features that indicate different sentiment classes (positive, negative, and neutral). The training algorithm optimizes the model parameters to reduce prediction errors while increasing sentiment prediction accuracy.

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(tfidf_matrix_selected, df['sentiment'], test_size=0.2, random_state=42)

# Initialize the logistic regression model
logreg_model = LogisticRegression()

# Fit the model on the training data
logreg_model.fit(X_train, y_train)

# Predict the sentiment labels for the test data
y_pred = logreg_model.predict(X_test)

# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of Logistic Regression Model:", accuracy)

Hyperparameter Tuning: To improve the performance of the sentiment analysis model, we can undertake hyperparameter tuning, which entails determining the best values for model parameters that affect its behavior (e.g., learning rate, regularization strength, network design). To determine the optimal combination of hyperparameters, approaches such as grid search or random search are commonly used.

# Define the hyperparameters grid
param_grid = {
'C': [0.001, 0.01, 0.1, 1, 10, 100], # Regularization parameter
'max_iter': [100, 200, 300, 400, 500] # Maximum number of iterations
}

# Initialize the logistic regression model
logreg_model = LogisticRegression()

# Initialize GridSearchCV
grid_search = GridSearchCV(estimator=logreg_model, param_grid=param_grid, cv=5, scoring='accuracy', n_jobs=-1)

# Perform grid search
grid_search.fit(X_train, y_train)

# Get the best hyperparameters
best_params = grid_search.best_params_
print("Best Hyperparameters:", best_params)

# Get the best model
best_model = grid_search.best_estimator_

# Evaluate the best model
best_model_accuracy = best_model.score(X_test, y_test)
print("Accuracy of Best Model:", best_model_accuracy)

Model Evaluation: Once the model has been trained and tweaked, we assess its performance on a different validation or test dataset. Evaluation metrics such as Accuracy, precision, recall, F1-score, and area under the receiver operating characteristic curve (AUC-ROC) are often used to evaluate a model’s ability to accurately classify sentiment.

# Initialize the classifiers
logreg_model = LogisticRegression()
rf_model = RandomForestClassifier(random_state=42)
mlp_model = MLPClassifier(random_state=42)
svm_model = SVC(kernel='linear', random_state=42)

# Check if the classifiers are initialized correctly
print("Logistic Regression Model:", logreg_model)
print("Random Forest Model:", rf_model)
print("MLP Classifier Model:", mlp_model)
print("SVM Model:", svm_model)

By following these methods, we create a sentiment analysis model that can accurately predict the sentiment of Amazon product reviews. The trained model serves as the foundation for developing an interactive sentiment analysis dashboard that visualizes and interprets the results.

MACHINE LEARNING OPTIMIZATION

Machine learning optimization plays a pivotal role in enhancing the performance of our sentiment analysis models. It involves fine-tuning various parameters to ensure optimal accuracy and efficiency.

By employing optimization techniques, we aim to refine our models to deliver more accurate predictions and insights from the vast amounts of data at our disposal.

Model Selection and Comparison:

Our first step involves selecting the most suitable machine learning algorithms for sentiment analysis. We carefully evaluate and compare different algorithms, considering factors such as accuracy, scalability, and computational efficiency. Through rigorous experimentation and analysis, we identify the most promising algorithm that aligns with our project requirements and objectives.

Model Selections

Hyperparameter Tuning:

Hyperparameter tuning is a crucial aspect of optimization, where we fine-tune the parameters of our chosen algorithm to achieve optimal performance. This involves adjusting parameters such as learning rates, regularization strengths, and model complexities to strike the right balance between bias and variance.

Cross-Validation Strategies:

Cross-validation techniques are employed to assess the generalization performance of our models and mitigate overfitting. By partitioning the dataset into multiple subsets and iteratively training and validating our models, we gain valuable insights into their robustness and reliability.

Evaluation Metrics and Model Deployment:

We evaluate the performance of our optimized models using a variety of metrics, including accuracy, precision, recall, and F1-score. Once satisfied with the performance, we deploy the models into production environments, ensuring seamless integration and real-time prediction capabilities.

We remain vigilant in monitoring model performance and exploring new techniques and algorithms to stay at the forefront of sentiment analysis advancements.

DEPLOYMENT

Our interactive dashboard, developed using Dash, represents a significant milestone in our project’s journey. With Dash, a Python-based framework, we’ve created a dynamic platform that allows users to engage with sentiment analysis results derived from Amazon product reviews. The dashboard boasts intuitive features, including an input text area where users can input their reviews and an “Analyze” button to trigger the sentiment analysis process. Upon analysis completion, users are presented with sentiment scores and categories, visually displayed in an interactive format. Customizability and scalability are core aspects of our dashboard, ensuring adaptability to various user needs and accommodating future feature expansions. Moreover, its user-friendly design fosters engagement and accessibility for individuals of all technical backgrounds. Moving forward, we see vast potential in further research into advanced sentiment analysis techniques and the integration of real-time data streams. By soliciting user feedback and embracing an iterative development approach, we aim to continuously enhance the dashboard’s functionality and user experience, thereby providing valuable insights for businesses and researchers alike.

https://glitch.com/~elfin-zenith-hope

CONCLUSION

The completion of this sentiment analysis project marks a significant achievement in leveraging natural language processing techniques to extract insights from Amazon product reviews. Through meticulous data preparation, exploratory data analysis, and sentiment labeling, we gained a deeper understanding of customer sentiments expressed in the reviews. Utilizing advanced machine learning algorithms and optimization techniques, we developed models capable of accurately classifying sentiments as positive, negative, or neutral.

The implementation of an interactive dashboard using Dash provided a user-friendly interface for real-time sentiment analysis, enabling stakeholders to intuitively explore and interpret the sentiment trends of Amazon product reviews. This interactive visualization platform enhances decision-making processes and facilitates actionable insights for product development, marketing strategies, and customer engagement initiatives.

In conclusion, this project demonstrates the power of combining data science methodologies with interactive visualization tools to derive meaningful insights from vast amounts of textual data. By effectively analyzing customer sentiments, organizations can gain a competitive edge in understanding consumer preferences, improving product offerings, and fostering stronger customer relationships. As technology continues to evolve, the application of sentiment analysis in various domains will undoubtedly play a pivotal role in shaping business strategies and driving innovation.

RECOMMENDATIONS

Looking ahead, we recommend further exploration into advanced sentiment analysis methodologies, such as deep learning and ensemble techniques, to improve the dashboard’s predictive capabilities and accuracy. Additionally, incorporating user feedback mechanisms and sentiment trend analysis features would enrich the dashboard’s functionality, providing users with actionable insights to drive business strategies. Moreover, expanding the scope of the dashboard to include sentiment analysis for other e-commerce platforms and integrating real-time data feeds would enhance its relevance and utility in an ever-evolving digital landscape. By embracing continuous improvement and innovation, we envision our dashboard becoming a indispensable tool for businesses seeking to understand and leverage customer sentiments effectively.

--

--

Oluwakemi Helen Deniran

Tech Enthusiast | EmpowerHer in Tech💡 | Passionate Data Scientist 📊 | Community Impact Advocate 🌍 | Aspiring Strategic Communicator for Sustainability 🌟