Lesson 9 — Decision Trees + Text-as-Data (NLP) + Ethics
(supervised learning, model interpretation, sentiment analysis, and responsible use)
Why this matters¶
Decision trees and text analytics are common in business, economics, and applied research.
Decision trees are used in settings such as:
customer segmentation and targeting,
churn prediction,
credit screening,
operations and logistics,
risk classification.
Text analytics is used to analyze:
customer reviews,
open-ended survey responses,
news articles,
social media posts,
policy documents,
corporate reports.
Both tools are attractive because they seem intuitive. A decision tree gives rules. A word cloud gives a quick visual summary. Sentiment analysis gives a simple positive/negative/neutral signal.
But both can mislead.
Where Lesson 9 fits in the course¶
In Lesson 8, we used the customer data for unsupervised learning.
The question was:
Can we discover customer segments from behavioral data?
In Lesson 9, we use the same customer data for supervised learning.
The question becomes:
Can we predict a known customer segment using spending, frequency, recency, discounts, returns, age, and income?
Part A — Decision trees¶
What is a decision tree?¶
A decision tree is a flowchart-like model that makes predictions through a sequence of splits.
For example, a tree may learn rules such as:
if
recency_daysis high andfrequencyis low, predict “At-Risk Customer”;if
total_spendis high andfrequencyis high, predict “Loyal High-Value Customer”;if
discount_rateis high, predict “Discount-Driven Customer.”
Trees are supervised learning¶
Decision trees are supervised learning models because they require a target variable .
There are two common types:
Classification tree
Predicts a category.Example: customer segment, churn yes/no, default yes/no.
Regression tree
Predicts a number.Example: sales next month, spending, income, demand.
Today we focus on a classification tree.
Our target variable is:
true_segmentOur predictors are:
total_spend
frequency
recency_days
avg_order_value
discount_rate
return_rate
age
incomeHow trees choose splits¶
A decision tree tries many possible splits and chooses the split that best separates the target categories.
For classification, the tree tries to make the resulting groups more “pure.”
A pure group is one where most observations belong to the same class.
Overfitting¶
Decision trees can easily overfit.
A tree can keep splitting until it memorizes the training data.
Symptoms of overfitting include:
very high training accuracy,
lower test accuracy,
many branches,
many small leaves,
rules that depend on very few observations.
Practical controls include:
max_depth,min_samples_leaf,min_samples_split.
Evaluating classification models¶
For classification, we should not look only at accuracy.
Important tools include:
Confusion matrix
Shows correct and incorrect classifications.Accuracy
Share of predictions that are correct.Precision
Of the cases predicted as a class, how many were actually that class?Recall
Of the actual cases in a class, how many did the model find?F1-score
A balance between precision and recall.
Interpreting tree rules¶
One advantage of decision trees is that we can inspect their rules.
A rule might look like:
If
recency_daysis greater than 100 andfrequencyis less than 8, predict “At-Risk Customer.”
This is useful because it can be translated into business language.
But interpretation must be cautious.
For example, if a tree uses discount_rate to predict a segment, this does not prove that discounts caused the customer to belong to that segment.
Part B — Text as data¶
Why text matters¶
Many important signals are stored as text.
Examples include:
customer reviews,
open-ended survey answers,
news articles,
social media posts,
product descriptions,
interview transcripts,
policy documents.
Text is rich but messy. It must be converted into something we can summarize or analyze.
Word clouds and word counts¶
A word cloud is a simple visual summary of common words.
It is useful for a quick first look, but it has limitations:
it ignores context,
it ignores grammar,
it may overemphasize common but unimportant words,
it does not show whether words are positive, negative, or important.
A frequency table is often more informative because it shows actual counts.
Sentiment analysis¶
Sentiment analysis tries to classify text as positive, negative, or neutral.
It can be useful for:
quick monitoring,
scanning customer feedback,
identifying very negative comments for review,
comparing broad sentiment across groups or time.
But sentiment analysis can fail.
It may misread:
sarcasm,
jokes,
mixed sentiment,
domain-specific language,
non-native writing,
short or ambiguous text.
Part C — Ethics and responsible interpretation¶
Ethics is not an extra topic at the end. It is part of the full workflow.
In decision trees, risks can enter through:¶
the target label,
the predictors,
proxy variables,
sampling,
measurement error,
unequal error costs across groups.
For example, income, location, language, or purchasing behavior may act as proxies for sensitive characteristics.
In text analysis, risks can enter through:¶
who writes reviews,
who is missing from the text data,
language bias,
dialect or translation issues,
domain mismatch,
overinterpreting sentiment scores.
Mini-lab: Google Colab¶
The notebook has two main parts:
Decision trees using
customer_segmentation.csv.Text-as-data using scraped quotes, word clouds, word counts, and sentiment analysis.
Practical Part 1 — Decision trees¶
Step 1 — Load the data¶
We use the same data from Lesson 8:
The target variable is:
true_segmentThe predictors are:
total_spend
frequency
recency_days
avg_order_value
discount_rate
return_rate
age
incomeStep 2 — Train and test the tree¶
The practical workflow is:
load the data,
define features and target,
split into train and test sets,
train a decision tree,
evaluate predictions,
inspect the tree,
interpret rules.
Key Python tools:
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, plot_tree, export_text
from sklearn.metrics import accuracy_score, classification_report, confusion_matrixStep 3 — Evaluate the model¶
In the notebook, we evaluate the decision tree using:
accuracy,
classification report,
confusion matrix.
Step 4 — Interpret tree rules¶
The notebook prints the tree as both:
a visual tree,
text-based rules.
A useful interpretation template is:
The tree predicts customer segment using several behavioral variables. One important rule appears to be based on
[feature]. If[condition], the model tends to predict[segment]. This rule is useful because[business reason]. However, it should not be interpreted causally because[caveat].
Step 5 — Check overfitting¶
The notebook compares training and test accuracy for different tree depths.
If training accuracy keeps increasing while test accuracy stops improving or falls, the tree may be overfitting.
Practical Part 2 — Text-as-data and sentiment¶
Step 1 — Scrape text¶
The notebook uses:
https://quotes.toscrape.com/This site is designed for scraping practice.
We use requests and BeautifulSoup to collect:
quote text,
author,
tags,
page number.
Step 2 — Clean the text¶
The notebook applies light text cleaning:
convert to lowercase,
remove punctuation,
remove extra spaces.
This creates a clean_text column.
Step 3 — Create a word cloud¶
The notebook combines the cleaned text into one large text string and creates a word cloud.
A word cloud helps us see frequent words quickly.
But it is only exploratory.
Step 4 — Count common words¶
The notebook also creates a table of the top 20 most common words.
This is useful because counts are more transparent than visual size in a word cloud.
Step 5 — Sentiment analysis¶
The notebook uses VADER sentiment analysis.
Each text receives a compound score.
A simple interpretation is:
positive if score is greater than or equal to 0.05,
negative if score is less than or equal to -0.05,
neutral otherwise.
Step 6 — Inspect examples¶
The notebook asks you to inspect:
most negative examples,
most positive examples.
This step is important because automated sentiment labels can be wrong.
Possible reasons include:
sarcasm,
mixed sentiment,
ambiguous words,
unusual writing style,
domain-specific meaning.
Optional extension: news text from GDELT¶
The notebook includes an optional extension using GDELT news data.
You can change the query term, for example:
inflationclimate changeartificial intelligencetourismtrade
The workflow is:
collect news titles,
clean titles,
create a word cloud,
compute sentiment scores,
inspect examples.
This connects Lesson 9 to the data collection work from earlier lessons.
Responsible presentation checklist¶
When presenting decision tree results, include:
the target variable,
the predictors,
train/test evaluation,
confusion matrix,
one or two interpretable rules,
one limitation,
one ethical or fairness concern.
When presenting text analysis results, include:
where the text came from,
how the text was cleaned,
one word cloud or word count table,
sentiment summary,
examples checked manually,
one limitation,
one ethical or representation concern.
AI check: responsible use¶
Good prompt examples:
Write sklearn code to train a decision tree with max_depth=3 and print a confusion matrix.How do I interpret precision and recall in a customer classification context?Suggest a checklist for validating sentiment analysis outputs on product reviews.List potential proxy variables for sensitive attributes in a customer segmentation model.Bad prompt examples:
Prove my model is fair.Write a persuasive story that the model should be deployed immediately.Summarize the reviews and tell me what customers really think without showing examples.In-class checkpoints¶
Decision trees¶
Load
customer_segmentation_synthetic.xlsx.Define features and target.
Split the data into training and test sets.
Train a decision tree with
max_depth=3.Report accuracy and the classification report.
Produce a confusion matrix.
Visualize the tree.
Print the decision rules.
Identify one useful rule and one limitation.
Compare training and test accuracy for different tree depths.
Text-as-data / NLP¶
Scrape text using BeautifulSoup.
Create a cleaned text column.
Create a word cloud.
Produce a top-20 word count table.
Compute sentiment scores.
Classify text as positive, negative, or neutral.
Inspect the most positive and most negative examples.
Identify one case where sentiment may be misleading.
Ethics¶
Identify one possible proxy feature risk in the decision tree.
Identify one limitation or bias risk in the text analysis.
Write a short responsible-use note.
Review questions¶
Why are decision trees considered supervised learning?
What is overfitting, and why are decision trees prone to it?
Why can accuracy be misleading?
What does a confusion matrix show?
Why should feature importance not be interpreted causally?
Why is a word cloud only an exploratory tool?
What are two reasons sentiment analysis might fail?
What is one ethical concern in using customer data for prediction?
What is one ethical concern in using scraped text for analysis?