Logistic Regression for Text Classification in NLP

Nlp tutorial · PySpark.in

Logistic Regression for Text Classification in NLP 

Logistic Regression is one of the most fundamental supervised machine learning algorithms used for classification tasks. In Natural Language Processing (NLP), Logistic Regression is widely applied for text classification problems such as spam detection, sentiment analysis, fake news detection, email classification, and document categorization. 

Despite its name, Logistic Regression is primarily a classification algorithm rather than a regression algorithm. It predicts the probability that a given text belongs to a particular class. 

Because of its simplicity, speed, and effectiveness, Logistic Regression is considered one of the strongest baseline models for NLP tasks. 

Introduction to Logistic Regression 

Logistic Regression is a probabilistic classification algorithm that predicts the probability of a data point belonging to a specific category. 

For example: 

Text 

Label 

“I love this movie” 

Positive 

“Worst product ever” 

Negative 

The model learns patterns from textual features and predicts whether the sentiment is positive or negative. 

Why Logistic Regression is Important in NLP 

Text classification datasets usually contain: 

  • High-dimensional data  

  • Sparse vectors  

  • Large vocabularies  

Logistic Regression performs efficiently in such environments because: 

  1. It handles sparse text vectors effectively.  

  1. It trains quickly.  

  1. It provides probabilistic outputs.  

  1. It works well with TF-IDF features.  

  1. It serves as an excellent baseline classifier. 

Sigmoid Function 

Logistic Regression uses the Sigmoid (Logistic) Function to convert outputs into probabilities between 0 and 1. 

The sigmoid equation is: 

 

The output probability lies between 0 and 1. 

Logistic Regression Prediction Function 

The prediction equation is: 

 

 

Decision Boundary 

Logistic Regression classifies data based on probability thresholds. 

Probability 

Prediction 

≥ 0.5 

Positive Class 

< 0.5 

Negative Class 

Example: 

 

Workflow of Logistic Regression in NLP 

Step 1: Collect Text Data 

Example dataset: 

Text 

Label 

“Amazing movie” 

Positive 

“Worst experience” 

Negative 

“Excellent product” 

Positive 

“Very bad service” 

Negative 

Step 2: Text Preprocessing 

Text preprocessing improves text quality before training. 

Common preprocessing steps include: 

  • Lowercasing  

  • Removing punctuation  

  • Tokenization  

  • Stop-word removal  

  • Stemming/Lemmatization  

Example: 

Before preprocessing: 

 

After preprocessing: 

 

Step 3: Feature Extraction 

Machine learning algorithms require numerical inputs. 

Text is converted into vectors using: 

  • Bag of Words (BoW)  

  • TF-IDF  

TF-IDF is commonly preferred with Logistic Regression. 

Example vocabulary: 

 

Sentence: 

 

Vector representation: 

 

Step 4: Train Logistic Regression Model 

The model learns weights for each feature during training. 

Prediction score: 

 

Probability: 

 

The sigmoid function converts the score into a probability. 

Step 5: Prediction 

Example input: 

 

Output: 

 

 

Cost Function in Logistic Regression 

Logistic Regression uses Log Loss (Cross-Entropy Loss). 

The formula is: 

 

The objective is to minimize prediction error. 

Gradient Descent 

Gradient Descent is used to optimize model parameters. 

Weight update rule: 

 

 

Practical Implementation  

Problem Statement 

Build a sentiment analysis classifier using Logistic Regression. 

``` 

#Import libraries 

import pandas as pd 

from sklearn.feature_extraction.text import TfidfVectorizer 

from sklearn.model_selection import train_test_split 

from sklearn.linear_model import LogisticRegression 

from sklearn.metrics import accuracy_score, classification_report 

#Dataset 

data = { 

"text": [ 
 
   "I love this product", 
 
   "Amazing customer service", 
 
   "Worst experience ever", 
 
   "Very disappointing", 
 
   "Excellent quality", 
 
   "I hate this item" 
 
], 
 
"label": [ 
 
   "positive", 
 
   "positive", 
 
   "negative", 
 
   "negative", 
 
   "positive", 
 
   "negative" 
 
] 
 

} 

#Create DataFrame 

df = pd.DataFrame(data) 

#Features and target 

X = df["text"] 

y = df["label"] 

#TF-IDF Vectorization 

vectorizer = TfidfVectorizer() 

X_vectorized = vectorizer.fit_transform(X) 

#Split dataset 

X_train, X_test, y_train, y_test = train_test_split( 

X_vectorized, 
 
y, 
 
test_size=0.3, 
 
random_state=42 
 

) 

#Create Logistic Regression model 

model = LogisticRegression() 

#Train model 

model.fit(X_train, y_train) 

#Prediction 

y_pred = model.predict(X_test) 

#Accuracy 

print("Accuracy:", accuracy_score(y_test, y_pred)) 

#Classification Report 

print(classification_report(y_test, y_pred)) 

#Test custom sentence 

message = ["excellent product"] 

message_vector = vectorizer.transform(message) 

prediction = model.predict(message_vector) 

print("Prediction:", prediction[0]) 

``` 

Conclusion 

Logistic Regression is one of the most important and effective baseline models for Natural Language Processing tasks. Its ability to efficiently handle sparse high-dimensional text data makes it highly suitable for sentiment analysis, spam detection, and document classification. Although modern deep learning methods provide higher performance for complex NLP tasks, Logistic Regression remains a widely used and reliable classical machine learning algorithm due to its simplicity, interpretability, and computational efficiency. 

 

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges