Decision Trees for NLP

Nlp tutorial · PySpark.in

Decision Trees for NLP 

Decision Tree is a supervised machine learning algorithm used for both classification and regression tasks. In Natural Language Processing (NLP), Decision Trees are primarily used for text classification tasks such as spam detection, sentiment analysis, document categorization, and topic classification. 

A Decision Tree works by splitting the dataset into branches based on feature conditions and making predictions through a tree-like structure. 

The model resembles a flowchart where: 

  • Internal nodes represent conditions/features  

  • Branches represent decision rules  

  • Leaf nodes represent output classes  

Decision Trees are simple, interpretable, and easy to visualize, making them useful for understanding classification logic. A Decision Tree is a hierarchical structure used for decision-making and classification. The algorithm repeatedly splits the data based on the most informative features until the final prediction is obtained. 

For example: 

Text 

Class 

“Win free money” 

Spam 

“Project meeting tomorrow” 

Ham 

 

The Decision Tree learns rules such as: 

 

Working Principle of Decision Tree 

The Decision Tree algorithm works by recursively splitting the dataset into smaller subsets based on the best feature. 

The objective is to maximize class purity after every split. 

The splitting process continues until: 

  • All samples belong to the same class  

  • Maximum depth is reached  

  • No further splitting is possible 

Important Concepts in Decision Trees 

1. Entropy 

Entropy measures impurity or randomness in the dataset. 

The entropy formula is: 

 

 

Interpretation 

Entropy Value 

Meaning 

0 

Pure dataset 

1 

Highly impure dataset 

 

2. Information Gain 

Information Gain measures how much entropy decreases after splitting. 

The formula is: The feature with the highest Information Gain is selected for splitting. 

 

 

3. Gini Index 

Another splitting criterion used in Decision Trees. 

The formula is: Lower Gini value indicates better purity. 

 

 

 

Workflow of Decision Tree in NLP 

Step 1: Collect Text Data 

Example dataset: 

Text 

Label 

“Win lottery now” 

Spam 

“Meeting tomorrow” 

Ham 

“Free prize available” 

Spam 

“Project update today” 

Ham 

 

Step 2: Text Preprocessing 

Text preprocessing includes: 

  • Lowercasing  

  • Removing punctuation  

  • Tokenization  

  • Stop-word removal  

  • Stemming/Lemmatization  

Example: 

Before preprocessing: 

 

After preprocessing: 

 

Step 3: Feature Extraction 

The textual data is converted into numerical vectors using: 

  • Bag of Words (BoW)  

  • TF-IDF  

Example vocabulary: 

 

Sentence: 

 

Vector: 

 

Step 4: Train Decision Tree Model 

The algorithm selects the best features using: 

  • Entropy  

  • Information Gain  

  • Gini Index  

The tree structure is generated automatically. 

Example decision rule: 

 

 

Step 5: Prediction 

The trained model predicts the class of new text documents. 

Example: 

 

 

 

 

Applications of Decision Trees in NLP 

Application 

Example 

Spam Detection 

Email classification 

Sentiment Analysis 

Positive/negative prediction 

News Classification 

Topic categorization 

Chatbot Intent Detection 

User intent prediction 

Fake News Detection 

Authenticity analysis 

 

Random Forest in NLP 

Random Forest is an ensemble learning technique that combines multiple Decision Trees. 

Instead of using one tree, Random Forest creates many trees and combines their predictions. 

This improves: 

  • Accuracy  

  • Stability  

  • Generalization 

 

Random Forest Prediction Formula 

 

Where: 

  • N= Number of trees 

XGBoost in NLP 

XGBoost (Extreme Gradient Boosting) is an advanced ensemble algorithm based on boosting techniques. 

It sequentially builds trees where each new tree corrects errors made by previous trees. 

XGBoost is widely used because of: 

  • High accuracy  

  • Fast performance  

  • Excellent optimization 

Objective Function in XGBoost 

 

Where: 

  • Loss = Prediction error  

  • Ω = Regularization term 

 

Practical Implementation  

Problem Statement 

Build a spam classifier using Decision Tree. 

 

``` 

#Import libraries 

import pandas as pd 

from sklearn.feature_extraction.text import TfidfVectorizer 

from sklearn.model_selection import train_test_split 

from sklearn.tree import DecisionTreeClassifier 

from sklearn.metrics import accuracy_score 

#Dataset 

data = { 

"text": [ 
 
   "Win free lottery", 
 
   "Project meeting tomorrow", 
 
   "Claim your free prize", 
 
   "Submit assignment today", 
 
   "Congratulations you won money", 
 
   "Team discussion at office" 
 
], 
 
"label": [ 
 
   "spam", 
 
   "ham", 
 
   "spam", 
 
   "ham", 
 
   "spam", 
 
   "ham" 
 
] 
 

} 

#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 model 

model = DecisionTreeClassifier() 

#Train model 

model.fit(X_train, y_train) 

#Prediction 

y_pred = model.predict(X_test) 

#Accuracy 

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

#Test custom sentence 

message = ["Free money prize"] 

message_vector = vectorizer.transform(message) 

prediction = model.predict(message_vector) 

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

``` 

Conclusion 

Decision Trees, Random Forest, and XGBoost are powerful classical machine learning algorithms widely used in Natural Language Processing tasks. Decision Trees provide interpretable rule-based classification, while Random Forest and XGBoost improve performance using ensemble learning techniques. These algorithms remain highly important for NLP applications involving text classification, sentiment analysis, spam detection, and information extraction. 

 

 

 

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges