NLP Transformer-Based Models Used For Sentiment Analysis: 1. BERT
NLP Transformer-Based Models Used For Sentiment Analysis: 1. BERT
import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style='whitegrid')
train = pd.read_csv('/kaggle/input/sentiment-analysis-dataset/trainin
g.csv',header=None)
validation = pd.read_csv('/kaggle/input/sentiment-analysis-dataset/va
lidation.csv',header=None)
display(train.isnull().sum())
print("*****"* 5)
display(validation.isnull().sum())
sentiment_counts_train = train['Sentiment'].value_counts()
sentiment_counts_validation = validation['Sentiment'].value_counts()
ax2.pie(sentiment_counts_validation, labels=sentiment_counts_valid
ation.index, autopct='%1.1f%%', colors=['gold', 'lightcoral', 'lightsky
blue','#99FF99'])
ax2.set_title('Sentiment Distribution (Validation Data)', fontsize=20)
plt.tight_layout()
plt.show()
# Calculate the value counts of 'Entity'
entity_counts = train['Entity'].value_counts()
top_names = entity_counts.head(19)
other_count = entity_counts[19:].sum()
top_names['Other'] = other_count
top_names.to_frame()
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
fig = go.Figure(data=[go.Pie(
labels=percentages.index,
values=percentages,
textinfo='label+percent',
insidetextorientation='radial'
)])
fig.update_layout(
title_text='Top Names with Percentages',
showlegend=False
)
fig.show()
import pandas as pd
from sklearn.model_selection import train_test_split
import pandas as pd
import plotly.graph_objects as go
fig.show()
import plotly.graph_objects as go
fig.update_layout(
title='First 5 Rows of Test Data',
width=1000,
height=500,
)
fig.show()
1. BERT (Bidirectional Encoder Representations from Transformers)
BERT is a groundbreaking language model that has significantly advanced the field of Natural
Language Processing (NLP).
It stands for Bidirectional Encoder Representations from Transformers.
Key Concepts
Bidirectional: Unlike previous models that processed text sequentially (left to right or right
to left), BERT considers the entire context of a word, both preceding and following it. This
enables a deeper understanding of language nuances.
Encoder: BERT focuses on understanding the input text rather than generating new text. It
extracts meaningful representations from the input sequence.
Transformers: The underlying architecture of BERT is based on the Transformer model,
known for its efficiency in handling long sequences and capturing dependencies between
words.
How BERT Works
Pre-training: BERT is initially trained on a massive amount of text data (like Wikipedia and
BooksCorpus) using two unsupervised tasks:
Masked Language Modeling (MLM): Randomly masks some words in the input and
trains the model to predict the masked words based on the context of surrounding
words.
Next Sentence Prediction (NSP): Trains the model to predict whether two given
sentences are consecutive in the original document.
Fine-tuning: After pre-training, BERT can be adapted to specific NLP tasks with minimal
additional training. This is achieved by adding a task-specific output layer to the pre-trained
model.
Advantages of BERT
Strong performance: BERT has achieved state-of-the-art results on a wide range of NLP
tasks, including question answering, text classification, named entity recognition, and
more.
Efficiency: Fine-tuning BERT for new tasks is relatively quick and requires less data compared
to training models from scratch.
Versatility: BERT can be applied to various NLP problems with minimal modifications.
Applications of BERT
Search engines: Improving search relevance and understanding user queries.
Chatbots: Enhancing natural language understanding and generating more human-like
responses.
Sentiment analysis: Accurately determining the sentiment expressed in text.
Machine translation: Improving the quality of translated text.
Text summarization: Generating concise summaries of lengthy documents.
In essence, BERT is a powerful language model that has revolutionized NLP by capturing the
bidirectional context of words and enabling efficient transfer learning for various tasks.
%%time
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertForSequenceClassification, Ada
mW
from sklearn.metrics import accuracy_score, classification_report
def __len__(self):
return len(self.texts)
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
# Set up optimizer
optimizer = AdamW(model_BERT.parameters(), lr=2e-5)
# Training loop
num_epochs = 3
# Final evaluation
print(classification_report(test_true, test_preds, target_names=['Neg
ative', 'Neutral', 'Positive', 'Irrelevant']))
Benefits of RoBERTa
Improved Performance: RoBERTa consistently outperforms BERT on a wide range
of NLP tasks, achieving state-of-the-art results.
Efficiency: The modifications in RoBERTa lead to faster training and convergence.
Versatility: Like BERT, RoBERTa can be fine-tuned for various NLP tasks,
including text classification, question answering, and more.
Applications
Search Engines: Enhancing search relevance and understanding user queries.
Chatbots: Improving natural language understanding and generating more human-
like responses.
Sentiment Analysis: Accurately determining the sentiment expressed in text.
Machine Translation: Enhancing the quality of translated text.
Text Summarization: Generating concise summaries of lengthy documents.
In conclusion, RoBERTa is a powerful language model that builds upon the success of BERT
by incorporating several refinements. Its improved performance and versatility make it a
popular choice for various NLP applications.
%%time
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import BertTokenizer, BertForSequenceClassification, Ada
mW
from transformers import RobertaTokenizer, RobertaForSequenceClassificatio
n, AdamW
from sklearn.metrics import accuracy_score, classification_report
def __len__(self):
return len(self.texts)
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
# Training loop
num_epochs = 3
DistilBERT is a smaller and faster version of the BERT model. It's created using a technique
called knowledge distillation. This means that a smaller model (the student) learns to mimic
the behavior of a larger, more complex model (the teacher). In this case, the teacher is
BERT.
Key Features
Smaller size: DistilBERT is about 40% smaller than BERT, making it more efficient
in terms of memory and computation.
Faster: It's also significantly faster than BERT, making it suitable for real-time
applications.
Comparable performance: Despite its smaller size, DistilBERT retains about 95%
of BERT's language understanding capabilities.
How it Works
Knowledge Distillation: The process involves training DistilBERT to predict the
same outputs as BERT for a given input. However, instead of using hard labels (the
correct answer), DistilBERT is trained on softened outputs from BERT. This allows
the smaller model to learn more generalizable knowledge.
Architecture Simplification: Some architectural elements of BERT, such as the
token type embeddings, are removed to reduce complexity.
Advantages
Efficiency: Smaller size and faster inference speed make it suitable for resource-
constrained environments.
Cost-effective: Lower computational requirements lead to reduced training and
inference costs.
Good performance: Despite its smaller size, it maintains a high level of performance
on various NLP tasks.
Applications
Text classification: Sentiment analysis, topic modeling
Named entity recognition: Identifying entities in text (e.g., persons, organizations,
locations)
Question answering: Finding answers to questions based on given text
Text generation: Summarization, translation
In summary, DistilBERT offers a compelling balance between model size, speed, and
performance. It's a valuable tool for NLP practitioners looking to deploy models efficiently
without sacrificing accuracy.
%%time
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import DistilBertTokenizer, DistilBertForSequenceClassi
fication, AdamW
from sklearn.metrics import accuracy_score, classification_report
def __len__(self):
return len(self.texts)
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
# Training loop
num_epochs = 3
torch.save(model_DistilBERT.state_dict(), 'sentiment_model_distilbert.pth')
# Final evaluation
print(classification_report(test_true, test_preds, target_names=['Neg
ative', 'Neutral', 'Positive', 'Irrelevant']))
ALBERT stands for A Lite BERT for Self-Supervised Learning. It's a language model
developed by Google AI, designed to be more efficient and effective than the original BERT
model.
Key Improvements Over BERT
Parameter Reduction: ALBERT significantly reduces the number of parameters
compared to BERT, making it more computationally efficient and faster to train. This
is achieved by:
Factorized embedding parameterization: Separating the embedding space into two
smaller spaces, reducing the number of parameters.
Cross-layer parameter sharing: Sharing parameters across different layers to reduce
redundancy.
Sentence-Order Prediction (SOP): Instead of the Next Sentence Prediction (NSP)
task used in BERT, ALBERT employs SOP. This task is more challenging and helps
the model better understand sentence relationships.
Architecture
ALBERT maintains the overall transformer architecture of BERT but incorporates the
aforementioned improvements. It consists of:
Embedding layer: Converts input tokens into numerical representations.
Transformer encoder: Processes the input sequence and captures contextual
information.
Output layer: Predicts the masked words and sentence order.
Benefits of ALBERT
Efficiency: ALBERT is significantly smaller and faster to train than BERT.
Improved Performance: Despite its smaller size, ALBERT often achieves better or
comparable performance to BERT on various NLP tasks.
Versatility: Like BERT, ALBERT can be fine-tuned for various NLP tasks.
Applications
Text classification: Sentiment analysis, topic modeling
Question answering: Answering questions based on given text
Named entity recognition: Identifying entities in text (e.g., persons, organizations,
locations)
Text summarization: Generating concise summaries of lengthy documents
In summary, ALBERT is a powerful language model that addresses some of the limitations
of BERT while maintaining its strengths. It offers a good balance between model size, speed,
and performance, making it a popular choice for various NLP applications.
%%time
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import AlbertTokenizer, AlbertForSequenceClassificatio
n, AdamW
from sklearn.metrics import accuracy_score, classification_report
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
text = str(self.texts[idx])
label = self.labels[idx]
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
# Set up optimizer
optimizer = AdamW(model_ALBERT.parameters(), lr=2e-5)
# Training loop
num_epochs = 3
# Final evaluation
print(classification_report(test_true, test_preds, target_names=['Negative',
'Neutral', 'Positive', 'Irrelevant']))
# Final evaluation
print(classification_report(test_true, test_preds, target_names=['Neg
ative', 'Neutral', 'Positive', 'Irrelevant']))
# Assuming test_true and test_preds are defined
from sklearn.metrics import confusion_matrix
XLNet is a powerful language model that builds upon the successes of its predecessor,
BERT, while addressing some of its limitations.
It stands for "Extreme Language Model".
Key Differences from BERT
Autoregressive vs. Autoencoding: While BERT is an autoencoding model, XLNet is
an autoregressive model. This means that XLNet predicts the next token in a sequence
given the previous ones, similar to how we humans generate text. This approach
allows XLNet to capture bidirectional context without the limitations of BERT's
masked language modeling.
Permutation Language Model: XLNet introduces the concept of a permutation
language model. Instead of training on a fixed order of tokens, it considers all possible
permutations of the input sequence. This enables the model to learn dependencies
between any two tokens in the sequence, regardless of their position.
How XLNet Works
Permutation Language Modeling: XLNet randomly permutes the input sequence
and trains the model to predict the masked tokens in any position based on the context
of the remaining tokens.
Attention Mechanism: Similar to BERT, XLNet uses a self-attention mechanism to
capture dependencies between different parts of the input sequence.
Two-Stream Self-Attention: XLNet employs two streams of self-attention:
Content stream: Focuses on the content of the tokens.
Query stream: Focuses on the position of the tokens in the permutation.
Advantages of XLNet
Bidirectional Context: XLNet can capture bidirectional context more effectively
than BERT, leading to improved performance on various NLP tasks.
Flexibility: The permutation language modeling approach allows for more flexible
modeling of language.
Strong Performance: XLNet has achieved state-of-the-art results on many NLP
benchmarks.
Applications of XLNet
Text classification
Question answering
Natural language inference
Machine translation
Text summarization
In summary, XLNet is a significant advancement in the field of natural language processing,
offering improved performance and flexibility compared to previous models. Its ability to
capture bidirectional context effectively makes it a powerful tool for various NLP
applications.
%%time
import pandas as pd
import torch
from torch.utils.data import Dataset, DataLoader
from transformers import XLNetTokenizer, XLNetForSequenceClassificatio
n, AdamW
from sklearn.metrics import accuracy_score, classification_report
def __len__(self):
return len(self.texts)
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
padding='max_length',
truncation=True,
return_attention_mask=True,
return_token_type_ids=True,
return_tensors='pt',
)
return {
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten(),
'token_type_ids': encoding['token_type_ids'].flatten(),
'labels': torch.tensor(label, dtype=torch.long)
}
# Set up optimizer
optimizer = AdamW(model_XLNet.parameters(), lr=2e-5)
# Training loop
num_epochs = 3
# Final evaluation
print(classification_report(test_true, test_preds, target_names=['Neg
ative', 'Neutral', 'Positive', 'Irrelevant']))
# Assuming test_true and test_preds are defined
from sklearn.metrics import confusion_matrix
# Set the width of each bar and the positions of the bars
width = 0.7
# Add value labels on top of each bar with increased font size
for i, v in enumerate(accuracy_trial_1):
ax.text(i, v + 0.2, f'{v:.1f}', ha='center', va='bottom', fontsize=16) #
Adjust vertical offset and format to one decimal place
# Add gridlines
ax.grid(axis='y', linestyle='--', alpha=0.9)
plt.tight_layout()
plt.show()
import os
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set(style='whitegrid')
import tensorflow as tf
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA, TruncatedSVD
from sklearn.metrics import classification_report,confusion_matrix
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Embedding, LSTM,Dense, SpatialDropout1D, Dropout
from keras.initializers import Constant
pd.set_option('display.max_columns', 100)
pd.set_option('display.max_rows', 900)
pd.set_option('display.max_colwidth', 200)
import warnings
warnings.filterwarnings("ignore")
Sentiment analysis is a powerful technique that involves determining the emotional tone of a piece of
text.
1. It classifies text as positive, negative, or neutral, and can even delve deeper into specific emotions like happiness,
sadness, or anger. This process, often referred to as opinion mining, is a cornerstone of Natural Language Processing
(NLP).
2. By applying sentiment analysis, businesses can gain valuable insights into customer perceptions, product performance,
and market trends. For instance, analysing customer reviews can reveal whether a product is meeting customer expectations
or if there's a need for improvement. Similarly, monitoring social media sentiment can help companies understand their
brand reputation and identify potential issues.
3. Sentiment analysis is particularly useful for processing large volumes of unstructured text data, such as customer
feedback, social media posts, and survey responses. It efficiently categorizes this data, making it easier to extract
meaningful information. Tools like Net Promoter Score (NPS) surveys, which measure customer loyalty, benefit greatly
from sentiment analysis. By automating the analysis of NPS responses, businesses can quickly identify patterns and trends
in customer sentiment.
4. In essence, sentiment analysis empowers organizations to understand the voice of their customers, make data-driven
decisions, and ultimately improve their products and services.
1. Data Collection:
Gather text data from various sources (e.g., customer reviews, social media posts, news articles)
Ensure a diverse and representative sample for accurate analysis
2. Text Cleaning:
Remove HTML tags, if present
Strip special characters and punctuation that don't contribute to sentiment.
Convert all text to lowercase for consistency.
3. Tokenization:
Break down the text into individual words or tokens
This step allows for analysis at the word level
4. Stop Word Removal:
Identify and remove common words (e.g., "the", "is", "and") that don't carry sentiment
This reduces noise in the data and focuses on meaningful words
5. Stemming or Lemmatization:
Stemming: Reduce words to their root form by removing suffixes (e.g., "running" to "run")
Lemmatization: Convert words to their base or dictionary form (e.g., "better" to "good")
This step helps in standardizing words and reducing vocabulary size
6. Handling Negations:
Identify negation words (e.g., "not", "never") and mark the affected phrases
This is crucial as negations can invert the sentiment of surrounding words
7. Dealing with Sarcasm and Context:
While challenging, attempt to identify sarcastic phrases or contextual nuances
This may involve looking at surrounding sentences or overall tone
8. Feature Extraction:
Convert the preprocessed text into a format suitable for machine learning algorithms
Common methods include bag-of-words, TF-IDF, or word embeddings
9. Normalization:
Standardize the features to ensure all inputs are on a similar scale
This helps in improving the performance of many machine learning algorithms
train = pd.read_csv('/kaggle/input/sentiment-analysis-dataset/training.csv',header=None)
validation = pd.read_csv('/kaggle/input/sentiment-analysis-dataset/validation.csv',header=N
one)
display(train.isnull().sum())
print("*****"* 5)
display(validation.isnull().sum())
duplicates = train[train.duplicated(subset=['Entity', 'Sentiment', 'Tweet Content'], keep=Fal
se)]
train = train.drop_duplicates(subset=['Entity', 'Sentiment', 'Tweet Content'], keep='first')
top_names = entity_counts.head(19)
other_count = entity_counts[19:].sum()
top_names['Other'] = other_count
top_names.to_frame()
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
fig.update_layout(
title_text='Top Names with Percentages',
showlegend=False
)
fig.show()
WordCloud
WordCloud is a visualization technique used in machine learning (ML), especially in Natural Language Processing
(NLP) tasks. It helps visualize textual data by displaying words in a cloud formation, where the size of each word
reflects its frequency or importance in the text.
Data Input: You feed the WordCloud function with text data. This could be anything from a document collection,
social media comments, or even code repositories.
Frequency Analysis: The algorithm analyzes the text and counts the occurrences of each word.
Word Placement and Sizing: Based on the frequency count, WordCloud positions and sizes the words. More frequent
words appear larger and more prominent in the cloud, while less frequent ones are smaller.
Visualization: Finally, it generates a visual output where the word cloud showcases the prominent themes and
keywords within the text data.
Easy Identification of Key Terms: Word clouds quickly reveal the most frequently used words, helping you
understand the core focus of the text data.
Text Summarization: They provide a summarized view of a large corpus of text, making it easier to grasp the overall
content.
Highlighting Trends: Word clouds can be used to identify emerging trends or topics of discussion within the text data.
Analyzing social media sentiment: See which words are most commonly used when people express positive or
negative opinions.
Topic modeling for research papers: Identify the main themes discussed in a collection of research papers.
Understanding user reviews: Analyze product reviews to see which features are most mentioned by users.
from wordcloud import WordCloud
# Filter positive sentiment tweets and extract the 'Tweet Content' column
positive_tweets = train[train["Sentiment"] == "Positive"]["Tweet Content"]
positive_text = ' '.join(positive_tweets)
wordcloud = WordCloud(width=1600, height=800, max_words=200, background_color='white').generate(po
sitive_text)
plt.figure(figsize=(20, 12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Positive Sentiment Tweets', fontsize=24)
plt.tight_layout(pad=0)
plt.show()
from wordcloud import WordCloud
# Filter positive sentiment tweets and extract the 'Tweet Content' column
negative_tweets = train[train["Sentiment"] == "Negative"]["Tweet Content"]
negative_text = ' '.join(negative_tweets)
wordcloud = WordCloud(width=1600, height=800, max_words=200, background_color='white').generate(ne
gative_text)
plt.figure(figsize=(20, 12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Negative Sentiment Tweets', fontsize=24)
plt.tight_layout(pad=0)
plt.show()
from wordcloud import WordCloud
plt.figure(figsize=(20, 12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Neutral Sentiment Tweets', fontsize=24)
plt.tight_layout(pad=0)
plt.show()
from wordcloud import WordCloud
plt.figure(figsize=(20, 12))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Irrelevant Sentiment Tweets', fontsize=24)
plt.tight_layout(pad=0)
plt.show()
import plotly.graph_objects as go
fig.update_layout(
title='Entity and Sentiment Distribution',
width=800,
height=800,
)
fig.show()
List of methods commonly used for small text classification:
Model Building
from tensorflow.keras.layers import Input, Dropout, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.initializers import TruncatedNormal
from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.metrics import CategoricalAccuracy
from tensorflow.keras.utils import to_categorical
import pandas as pd
from sklearn.model_selection import train_test_split
import pandas as pd
import plotly.graph_objects as go
# Assuming you've already run the data preprocessing steps
data = train[['Tweet Content', 'Sentiment']]
# Set your model output as categorical and save in new label col
data['Sentiment_label'] = pd.Categorical(data['Sentiment'])
fig.show()
import plotly.graph_objects as go
Text Preprocessing:
Text data is cleaned and transformed into a numerical representation. This involves steps like tokenization, stop word
removal, stemming, and lemmatization.
Feature extraction techniques like TF-IDF (Term Frequency-Inverse Document Frequency) are employed to convert
text into numerical vectors.
Hyperplane Creation:
SVM aims to find the optimal hyperplane, which is a decision boundary that separates different text classes in the
feature space.
Each text document is represented as a point in this high-dimensional space based on its extracted features.
Maximizing Margin:
SVM seeks the hyperplane that maximizes the margin between the different classes. This margin is the distance
between the hyperplane and the closest data points from each class (support vectors).
Classification:
New text documents are mapped to the same feature space.
Their position relative to the hyperplane determines the predicted class.
Key Concepts
Support Vectors: These are the data points closest to the hyperplane and significantly influence the model's decision
boundary.
Kernel Trick: SVM can handle non-linear relationships between features using the kernel trick, which implicitly maps
data to a higher-dimensional space.
Regularization: SVM uses regularization to prevent overfitting and improve generalization performance.
Advantages of SVM for Text Classification
Effective with high-dimensional data: Text data often has a large number of features. SVM handles such data efficiently.
Strong generalization performance: SVM tends to perform well on unseen data. Handles complex patterns: The kernel trick
allows SVM to capture complex relationships between words.
In Conclusion
SVM is a robust algorithm for text classification, offering excellent performance and versatility. By understanding its core
principles and effectively addressing its challenges, you can leverage SVM to build accurate and reliable text classification
models.
%%time
How it works:
Text Preprocessing: Similar to other text classification methods, the text data undergoes preprocessing steps like
tokenization, stop word removal, and stemming or lemmatization.
Feature Extraction: Words or n-grams (sequences of words) are typically used as features. These features are
converted into numerical representations, often using techniques like TF-IDF.
Probability Calculation: Naive Bayes calculates the probability of a document belonging to a particular class based
on the presence of specific words or features.
Bayes' Theorem Application: The algorithm applies Bayes' theorem to calculate the posterior probability of a class
given the document's features.
Classification: The class with the highest probability is assigned to the document.
Key Assumption:
The Naive Bayes algorithm makes a simplifying assumption: the occurrence of one word in a document is
independent of the occurrence of other words. While this assumption is often not strictly true in natural language, it
works surprisingly well in practice.
Effective with high-dimensional data: Handles text data with large vocabularies well.
Works well with small datasets: Can achieve reasonable performance with limited training data.
Challenges:
Naive Bayes assumption: The independence assumption might not hold true in all cases, affecting accuracy.
Zero-frequency problem: If a word doesn't appear in a training set for a particular class, its probability becomes
zero, impacting calculations. This can be addressed using techniques like Laplace smoothing.
In Summary:
Naive Bayes is a popular and efficient algorithm for text classification due to its simplicity and ability to handle high-
dimensional data. While it makes a simplifying assumption about feature independence, it often performs well in practice.
It's a good starting point for many text classification tasks.
%%time
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Extract text and labels
train_texts = data_train["Tweet Content"].tolist()
train_labels = data_train["Sentiment_label"].map({"Positive": 2, "Negative": 0, "Neutral": 1, "Irrelevant": 3})
test_texts = data_test["Tweet Content"].tolist()
test_labels = data_test["Sentiment_label"].map({"Positive": 2, "Negative": 0, "Neutral": 1, "Irrelevant": 3})
# Calculate accuracy
accuracy = accuracy_score(test_labels, predictions)
# Print accuracy
print("Test Accuracy Naive Bayes:", accuracy)
print("\n")
confusion_matrix_NB = confusion_matrix(test_labels, predictions)
print("Confusion Matrix:\n", confusion_matrix_NB)
Text Preprocessing: Similar to other text classification methods, the text data is cleaned and transformed into
numerical features. This involves tokenization, stop word removal, stemming, and lemmatization.
Feature Extraction: Text is converted into numerical features using techniques like TF-IDF (Term Frequency-
Inverse Document Frequency) or word embeddings.
Model Training: The Logistic Regression model is trained on the numerical representation of the text data and
corresponding class labels. The model learns the relationship between the features and the target class.
Probability Estimation: For a new text document, the model calculates the probability of the document belonging to
each class.
Classification: The class with the highest probability is assigned to the document.
Key Points:
Probability Estimation: Unlike some other classification algorithms, Logistic Regression provides probability
estimates for each class, which can be useful in certain applications.
Multi-class Classification: For problems with more than two classes, techniques like one-vs-rest or multinomial
logistic regression can be used.
Interpretability: The coefficients of the Logistic Regression model can provide insights into the importance of
different features in the classification process.
Advantages:
Probabilistic Output: Provides probability estimates for each class, which can be valuable in certain applications.
Challenges:
Applications:
Sentiment analysis
Spam detection
Topic classification
In essence, Logistic Regression offers a balance of simplicity, interpretability, and performance for text classification tasks.
While it may not always outperform more complex models, it's often a good starting point and can provide valuable insights
into the data.
%%time
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Calculate accuracy
accuracy = accuracy_score(test_labels, predictions)
# Print accuracy
print("Test Accuracy Logistic Regression:", accuracy)
print("\n")
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
Rf.fit(train_features, train_labels)
predictions = Rf.predict(test_features)
accuracy = accuracy_score(test_labels, predictions)
print("Test Accuracy Random Forest:", accuracy)
print("\n")
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# Calculate accuracy
accuracy = accuracy_score(test_labels, predictions)
# Print accuracy
print("Test Accuracy XGBoost:", accuracy)
print("\n")
print("Classification Report XGBoost:\n")
Xgb = classification_report(test_labels, predictions, target_names=["Negative", "Neutral", "Positive", "Irrel
evant"])
print(Xgb)
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
from sklearn.preprocessing import LabelEncoder
# Calculate accuracy
accuracy = accuracy_score(test_labels, predictions)
print("\n")
print("\033[91mTest Accuracy LightGBM:\033[0m", accuracy)
print("\n")
print("Classification Report LightGBM:\n")
Lgbm = classification_report(test_labels, predictions, target_names=["Negative", "Neutral",
"Positive", "Irrelevant"])
print(Lgbm)
confusion_matrix_lgbm = confusion_matrix(test_labels, predictions)
print("Confusion Matrix:\n\n", confusion_matrix_lgbm)
# Add model names within the bars with white color and adjust vertical alignment
plt.text(index, value / 2, model_name, ha='center', va='center', color='Yellow', fontsize=16, fontweight='
bold') # Shorten model names for better fit
plt.xlabel('Models')
plt.ylabel('Accuracy')
plt.title('Model Accuracy Comparison')
plt.ylim([0, .8])
plt.xticks(rotation=90)
plt.grid(axis='y', linestyle='--', alpha=0.7) # Add a faint grid for better readability