Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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:

Text analytics is used to analyze:

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:


Trees are supervised learning

Decision trees are supervised learning models because they require a target variable YY.

There are two common types:

  1. Classification tree
    Predicts a category.

    • Example: customer segment, churn yes/no, default yes/no.

  2. Regression tree
    Predicts a number.

    • Example: sales next month, spending, income, demand.

Today we focus on a classification tree.

Our target variable is:

true_segment

Our predictors are:

total_spend
frequency
recency_days
avg_order_value
discount_rate
return_rate
age
income

How 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:

Practical controls include:


Evaluating classification models

For classification, we should not look only at accuracy.

Important tools include:

  1. Confusion matrix
    Shows correct and incorrect classifications.

  2. Accuracy
    Share of predictions that are correct.

  3. Precision
    Of the cases predicted as a class, how many were actually that class?

  4. Recall
    Of the actual cases in a class, how many did the model find?

  5. 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_days is greater than 100 and frequency is 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:

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:

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:

But sentiment analysis can fail.

It may misread:


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:

For example, income, location, language, or purchasing behavior may act as proxies for sensitive characteristics.

In text analysis, risks can enter through:


Mini-lab: Google Colab

The notebook has two main parts:

  1. Decision trees using customer_segmentation.csv.

  2. 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_segment

The predictors are:

total_spend
frequency
recency_days
avg_order_value
discount_rate
return_rate
age
income

Step 2 — Train and test the tree

The practical workflow is:

  1. load the data,

  2. define features and target,

  3. split into train and test sets,

  4. train a decision tree,

  5. evaluate predictions,

  6. inspect the tree,

  7. 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_matrix

Step 3 — Evaluate the model

In the notebook, we evaluate the decision tree using:


Step 4 — Interpret tree rules

The notebook prints the tree as both:

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:


Step 2 — Clean the text

The notebook applies light text cleaning:

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:


Step 6 — Inspect examples

The notebook asks you to inspect:

This step is important because automated sentiment labels can be wrong.

Possible reasons include:


Optional extension: news text from GDELT

The notebook includes an optional extension using GDELT news data.

You can change the query term, for example:

The workflow is:

  1. collect news titles,

  2. clean titles,

  3. create a word cloud,

  4. compute sentiment scores,

  5. inspect examples.

This connects Lesson 9 to the data collection work from earlier lessons.


Responsible presentation checklist

When presenting decision tree results, include:

  1. the target variable,

  2. the predictors,

  3. train/test evaluation,

  4. confusion matrix,

  5. one or two interpretable rules,

  6. one limitation,

  7. one ethical or fairness concern.

When presenting text analysis results, include:

  1. where the text came from,

  2. how the text was cleaned,

  3. one word cloud or word count table,

  4. sentiment summary,

  5. examples checked manually,

  6. one limitation,

  7. 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

  1. Load customer_segmentation_synthetic.xlsx.

  2. Define features and target.

  3. Split the data into training and test sets.

  4. Train a decision tree with max_depth=3.

  5. Report accuracy and the classification report.

  6. Produce a confusion matrix.

  7. Visualize the tree.

  8. Print the decision rules.

  9. Identify one useful rule and one limitation.

  10. Compare training and test accuracy for different tree depths.

Text-as-data / NLP

  1. Scrape text using BeautifulSoup.

  2. Create a cleaned text column.

  3. Create a word cloud.

  4. Produce a top-20 word count table.

  5. Compute sentiment scores.

  6. Classify text as positive, negative, or neutral.

  7. Inspect the most positive and most negative examples.

  8. Identify one case where sentiment may be misleading.

Ethics

  1. Identify one possible proxy feature risk in the decision tree.

  2. Identify one limitation or bias risk in the text analysis.

  3. Write a short responsible-use note.


Review questions

  1. Why are decision trees considered supervised learning?

  2. What is overfitting, and why are decision trees prone to it?

  3. Why can accuracy be misleading?

  4. What does a confusion matrix show?

  5. Why should feature importance not be interpreted causally?

  6. Why is a word cloud only an exploratory tool?

  7. What are two reasons sentiment analysis might fail?

  8. What is one ethical concern in using customer data for prediction?

  9. What is one ethical concern in using scraped text for analysis?