<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>TensorFlow | Ibrahim Maïga</title>
    <link>https://ibrahimmaiga.com/tag/tensorflow/</link>
      <atom:link href="https://ibrahimmaiga.com/tag/tensorflow/index.xml" rel="self" type="application/rss+xml" />
    <description>TensorFlow</description>
    <generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Mon, 20 May 2024 00:00:00 +0000</lastBuildDate>
    <image>
      <url>https://ibrahimmaiga.com/media/icon_hufd6f977d0fa8c1812e905f5bcd032e38_62203_512x512_fill_lanczos_center_3.png</url>
      <title>TensorFlow</title>
      <link>https://ibrahimmaiga.com/tag/tensorflow/</link>
    </image>
    
    <item>
      <title>From Text to Insight: A Comprehensive Guide to Model Design and Sentiment Analysis</title>
      <link>https://ibrahimmaiga.com/project/sentiment-analysis-on-airline-customer-feedback/</link>
      <pubDate>Mon, 20 May 2024 00:00:00 +0000</pubDate>
      <guid>https://ibrahimmaiga.com/project/sentiment-analysis-on-airline-customer-feedback/</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;In the age of social media and online reviews, sentiment analysis has become essential for understanding public opinion. This NLP technique classifies sentiments in text as positive, negative, or neutral and is especially valuable for businesses to monitor brand reputation, gauge customer satisfaction, and assess market trends.&lt;/p&gt;
&lt;p&gt;In this blog post, we’ll explore a step-by-step guide to conducting sentiment analysis using a machine learning approach. This includes data loading, preprocessing, model training, evaluation, and insights derived from airline customer reviews. Let&amp;rsquo;s get started!&lt;/p&gt;
&lt;h2 id=&#34;1-understanding-the-sentiment-analysis-process&#34;&gt;1. Understanding the Sentiment Analysis Process&lt;/h2&gt;
&lt;p&gt;Sentiment analysis typically involves classifying text into distinct categories. For this project, we’ll perform binary classification to distinguish between positive and negative sentiments. The complete process includes:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Loading and cleaning the dataset.&lt;/li&gt;
&lt;li&gt;Conducting exploratory data analysis (EDA).&lt;/li&gt;
&lt;li&gt;Transforming text data into numerical features.&lt;/li&gt;
&lt;li&gt;Training a machine learning model.&lt;/li&gt;
&lt;li&gt;Evaluating the model’s performance.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Each step is critical for building a reliable sentiment analysis pipeline.&lt;/p&gt;
&lt;h2 id=&#34;2-data-loading-and-initial-exploration&#34;&gt;2. Data Loading and Initial Exploration&lt;/h2&gt;
&lt;p&gt;We begin by loading a dataset containing 23,171 reviews with multiple attributes, including customer sentiment labels. Initial data exploration ensures data integrity and familiarizes us with its structure.&lt;/p&gt;
&lt;h3 id=&#34;key-steps-in-initial-exploration&#34;&gt;Key Steps in Initial Exploration:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Checking for Missing Values&lt;/strong&gt;: Identifying and handling empty text entries or undefined labels to maintain data quality.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Overview&lt;/strong&gt;: Analyzing the distribution of features to understand their composition and relevance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A clean dataset is essential for accurate sentiment analysis, and this step sets the foundation for further preprocessing.&lt;/p&gt;
&lt;h2 id=&#34;3-data-preprocessing-cleaning-the-text-data&#34;&gt;3. Data Preprocessing: Cleaning the Text Data&lt;/h2&gt;
&lt;p&gt;Text data, especially from user-generated sources like reviews, can be noisy. Effective preprocessing ensures that text is standardized and irrelevant elements are removed.&lt;/p&gt;
&lt;h3 id=&#34;preprocessing-techniques&#34;&gt;Preprocessing Techniques:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lowercasing&lt;/strong&gt;: Standardizes text by converting all words to lowercase.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Removing Special Characters&lt;/strong&gt;: Eliminates punctuation, symbols, and emojis to focus on core words.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tokenizing&lt;/strong&gt;: Splits sentences into individual words (tokens) for word-by-word analysis.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Removing Stopwords&lt;/strong&gt;: Filters out common words such as &amp;ldquo;the,&amp;rdquo; &amp;ldquo;is,&amp;rdquo; and &amp;ldquo;and&amp;rdquo; to reduce noise and improve text relevancy.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The cleaned data provides a consistent format for model training and meaningful feature extraction.&lt;/p&gt;
&lt;h2 id=&#34;4-exploratory-data-analysis-eda&#34;&gt;4. Exploratory Data Analysis (EDA)&lt;/h2&gt;
&lt;p&gt;EDA helps reveal data characteristics, enabling better decision-making during model training.&lt;/p&gt;
&lt;h3 id=&#34;key-eda-steps&#34;&gt;Key EDA Steps:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sentiment Distribution Analysis&lt;/strong&gt;: Visualizes the proportion of positive vs. negative samples to identify potential data imbalances.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Word Frequency Analysis&lt;/strong&gt;: Identifies commonly used words within each sentiment class, offering insights into key differentiators for positive and negative sentiments.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These insights are crucial for understanding the dataset’s structure and ensuring it aligns with the analysis goals.&lt;/p&gt;
&lt;h2 id=&#34;5-feature-engineering-with-tf-idf&#34;&gt;5. Feature Engineering with TF-IDF&lt;/h2&gt;
&lt;p&gt;To train a machine learning model, text data must be transformed into numerical features. &lt;strong&gt;TF-IDF (Term Frequency-Inverse Document Frequency)&lt;/strong&gt; is a popular method that highlights word importance in the context of a document:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Term Frequency (TF)&lt;/strong&gt;: Counts how frequently a word appears in a document.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inverse Document Frequency (IDF)&lt;/strong&gt;: Downscales words that appear frequently across all documents to emphasize unique words.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Limiting the transformation to the top 5000 words maintains efficiency and retains essential features for training.&lt;/p&gt;
&lt;h2 id=&#34;6-model-training&#34;&gt;6. Model Training&lt;/h2&gt;
&lt;p&gt;With the numerical features ready, we can train a custom neural network using Keras. The model architecture was designed from scratch and includes:&lt;/p&gt;
&lt;h3 id=&#34;model-architecture-overview&#34;&gt;Model Architecture Overview:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Input Layer&lt;/strong&gt;: Embedding layer with a vocabulary size of 5000, embedding vector size of 128, and sequence length of 100.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LSTM Layers&lt;/strong&gt;: Two LSTM layers, with the first having 128 units and &lt;code&gt;return_sequences=True&lt;/code&gt; and the second with 64 units.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dense Layers&lt;/strong&gt;: Three dense layers (32, 16, and 1 unit) with ReLU activation for the first two and sigmoid activation for the output.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regularization&lt;/strong&gt;: BatchNormalization and dropout layers (0.2 rate) are used after each major layer to prevent overfitting.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model Compilation&lt;/strong&gt;: The model is compiled using the Adam optimizer (&lt;code&gt;learning_rate=0.001&lt;/code&gt;) and &lt;code&gt;binary_crossentropy&lt;/code&gt; as the loss function, with accuracy as the evaluation metric.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;model-training-steps&#34;&gt;Model Training Steps:&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Dataset Splitting&lt;/strong&gt;: The data is split into an 80% training set and a 20% test set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Training the Model&lt;/strong&gt;: The model learns the patterns from the training data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hyperparameter Tuning&lt;/strong&gt;: Random search is employed to optimize hyperparameters for better performance.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;7-evaluating-model-performance&#34;&gt;7. Evaluating Model Performance&lt;/h2&gt;
&lt;p&gt;Model performance is evaluated using the following metrics:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: Indicates the percentage of correctly classified samples.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precision and Recall&lt;/strong&gt;: Precision checks the correctness of positive predictions, while recall measures how well actual positives are identified.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F1-Score&lt;/strong&gt;: Balances precision and recall, providing a comprehensive metric for imbalanced data.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Confusion Matrix&lt;/strong&gt;: Visualizes true positives, true negatives, false positives, and false negatives to identify error types.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;benchmarking-results&#34;&gt;Benchmarking Results:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Accuracy Improvement&lt;/strong&gt;: From an initial 84.79% to 86.39% after tuning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loss Reduction&lt;/strong&gt;: Decreased from 0.5018 to 0.3843, reflecting improved prediction accuracy.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;8-ranking-airlines-by-customer-recommendations&#34;&gt;8. Ranking Airlines by Customer Recommendations&lt;/h2&gt;
&lt;p&gt;Using the &amp;ldquo;Recommended&amp;rdquo; column, we aggregated positive recommendations to rank airlines. Here’s how we ranked the top 20 airlines:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Group by &amp;#39;Airline Name&amp;#39; and aggregate the sum of &amp;#39;Recommended&amp;#39; column&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;airline_recommendations&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;df&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;groupby&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Airline Name&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Recommended&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sum&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;reset_index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Rename the columns for better understanding&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;airline_recommendations&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;columns&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Airline Name&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;Count of Recommended (yes=1)&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Sort the airlines by the count of recommendations in descending order&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ranked_airlines&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;airline_recommendations&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;sort_values&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;by&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;Count of Recommended (yes=1)&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ascending&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;False&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;reset_index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;drop&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;True&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Display the ranked airlines&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ranked_airlines&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;insights-from-the-analysis&#34;&gt;Insights from the Analysis:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;High Customer Satisfaction&lt;/strong&gt;: Airlines like China Southern Airlines and Hainan Airlines topped the list with the highest positive recommendations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regional Diversity&lt;/strong&gt;: The rankings included airlines from various regions, showcasing global recognition for excellent service.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;9-resources-and-environment&#34;&gt;9. Resources and Environment&lt;/h2&gt;
&lt;h3 id=&#34;environment-details&#34;&gt;Environment Details:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Platform&lt;/strong&gt;: Google Colab&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hardware&lt;/strong&gt;: TPU V2&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RAM&lt;/strong&gt;: 2.79 GB&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disk&lt;/strong&gt;: 27.47 GB&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using TPU V2 accelerated training and allowed for efficient processing of a large dataset with complex deep learning models.&lt;/p&gt;
&lt;h2 id=&#34;10-future-work&#34;&gt;10. Future Work&lt;/h2&gt;
&lt;h3 id=&#34;areas-for-further-improvement&#34;&gt;Areas for Further Improvement:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Advanced Hyperparameter Tuning&lt;/strong&gt;: Experiment with techniques like Grid Search or Bayesian Optimization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Augmentation&lt;/strong&gt;: Increase training data diversity to improve model generalization.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Explainability&lt;/strong&gt;: Use SHAP values to interpret the model’s predictions.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Granular Analysis&lt;/strong&gt;: Investigate specific text features that drive sentiment.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Improvement Strategies for Airlines&lt;/strong&gt;: Use insights to suggest enhancements for airlines with lower rankings.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;11-lessons-learned&#34;&gt;11. Lessons Learned&lt;/h2&gt;
&lt;h3 id=&#34;key-takeaways&#34;&gt;Key Takeaways:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Importance of Hyperparameter Tuning&lt;/strong&gt;: This step significantly boosted model performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model Evaluation Matters&lt;/strong&gt;: Regularly assess models using multiple metrics to understand their strengths and weaknesses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Resource Management&lt;/strong&gt;: Efficient use of computational resources, like TPUs, can accelerate deep learning workflows.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Customer Service Excellence&lt;/strong&gt;: Top-ranked airlines share traits like strong customer service and consistent positive reviews.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Power of Reviews&lt;/strong&gt;: Customer feedback plays a crucial role in brand reputation.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This sentiment analysis project showcases how deep learning and NLP techniques can turn raw text data into actionable insights. With proper data preparation, EDA, feature engineering, model training, and evaluation, this approach provides a structured method for future projects. Experimenting with advanced models and larger datasets can further refine sentiment analysis capabilities, benefiting businesses, researchers, and data enthusiasts.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
