📖 Scenario: You work for a small online store. You want to group customers based on how much they spent last month. This helps the store send special offers to big spenders and thank smaller spenders.
🎯 Goal: Build a simple program that groups customers into 'High spender' and 'Low spender' based on their spending amount.
📋 What You'll Learn
Create a dictionary with customer names and their spending amounts.
Set a spending threshold to separate high and low spenders.
Use a dictionary comprehension to create a new dictionary with customer names and their segment ('High spender' or 'Low spender').
Print the final segmentation dictionary.
💡 Why This Matters
🌍 Real World
Stores and businesses often group customers by spending to target marketing and rewards effectively.
💼 Career
Data analysts and marketers use customer segmentation to improve sales strategies and customer satisfaction.
Progress0 / 4 steps
1
Create the customer spending data
Create a dictionary called customer_spending with these exact entries: 'Alice': 120, 'Bob': 75, 'Charlie': 200, 'Diana': 50, 'Evan': 180.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary with the exact customer names and spending amounts.
2
Set the spending threshold
Create a variable called threshold and set it to 100. This will separate high spenders from low spenders.
Data Analysis Python
Hint
Just create a variable named threshold and assign it the value 100.
3
Create the customer segmentation dictionary
Use a dictionary comprehension to create a new dictionary called customer_segment. For each customer and spending in customer_spending.items(), assign 'High spender' if spending is greater than or equal to threshold, otherwise assign 'Low spender'.
Data Analysis Python
Hint
Use a dictionary comprehension with for name, spending in customer_spending.items() and a conditional expression to assign segments.
4
Print the customer segmentation
Write a print statement to display the customer_segment dictionary.
Data Analysis Python
Hint
Use print(customer_segment) to show the final dictionary.
Practice
(1/5)
1. What is the main goal of customer segmentation in data science?
easy
A. To group customers based on similar characteristics
B. To delete customer data from the database
C. To increase the number of customers randomly
D. To create a single profile for all customers
Solution
Step 1: Understand the purpose of segmentation
Customer segmentation means dividing customers into groups with similar traits.
Step 2: Identify the correct goal
The goal is to group customers, not delete or randomly increase them.
Final Answer:
To group customers based on similar characteristics -> Option A
Quick Check:
Segmentation = Grouping customers [OK]
Hint: Segmentation means grouping similar customers [OK]
Common Mistakes:
Thinking segmentation deletes data
Confusing segmentation with random customer increase
Assuming segmentation creates one big group
2. Which Python library is commonly used for KMeans clustering in customer segmentation?
easy
A. scikit-learn
B. matplotlib
C. pandas
D. numpy
Solution
Step 1: Recall common libraries for clustering
KMeans clustering is part of machine learning, often done with scikit-learn.
Step 2: Match library to KMeans
scikit-learn provides KMeans; matplotlib is for plotting, pandas for data frames, numpy for arrays.
Final Answer:
scikit-learn -> Option A
Quick Check:
KMeans = scikit-learn [OK]
Hint: KMeans is in scikit-learn library [OK]
Common Mistakes:
Choosing matplotlib for clustering
Confusing pandas with clustering tools
Selecting numpy instead of scikit-learn
3. What will be the output of this code snippet?
import pandas as pd
from sklearn.cluster import KMeans
data = pd.DataFrame({'Age': [25, 45, 35, 50], 'Income': [50000, 80000, 60000, 90000]})
model = KMeans(n_clusters=2, random_state=0)
model.fit(data)
print(model.labels_)
medium
A. [1 0 1 0]
B. [1 1 0 0]
C. [0 0 1 1]
D. [0 1 0 1]
Solution
Step 1: Understand KMeans clustering with 2 clusters
The model groups similar rows into 2 clusters based on Age and Income.
Step 2: Predict cluster labels for each row
Rows 0 and 2 are younger with lower income, likely cluster 1; rows 1 and 3 older with higher income, cluster 0.
Final Answer:
[1 1 0 0] -> Option B
Quick Check:
Similar customers grouped as [1 1 0 0] [OK]
Hint: Younger & lower income cluster 1, older & higher income cluster 0 [OK]
Common Mistakes:
Assuming clusters are assigned randomly
Mixing cluster labels order
Forgetting random_state fixes output
4. Identify the error in this customer segmentation code:
from sklearn.cluster import KMeans
import pandas as pd
data = pd.DataFrame({'Age': [22, 34, 29], 'Income': [40000, 60000, 50000]})
model = KMeans(n_clusters=3)
model.fit(data)
print(model.labels_)
medium
A. KMeans requires data to be normalized before fitting
B. DataFrame columns should be strings, not numbers
C. No error; code runs correctly and prints labels
D. Missing random_state parameter in KMeans
Solution
Step 1: Check code syntax and usage
Code imports libraries, creates DataFrame, fits KMeans with 3 clusters, and prints labels.
Step 2: Verify if code runs without error
random_state is optional; columns as strings are correct; normalization is recommended but not required.
Final Answer:
No error; code runs correctly and prints labels -> Option C
Quick Check:
Code runs fine without random_state [OK]
Hint: random_state optional; code runs if syntax correct [OK]
Common Mistakes:
Thinking random_state is mandatory
Believing normalization is required to run
Confusing column names with data types
5. You have customer data with Age, Income, and Spending Score. You want to create 3 segments using KMeans. Which steps should you follow to prepare and segment the data correctly?
hard
A. Remove Age column, set n_clusters=1, fit KMeans, ignore labels
B. Convert numeric data to strings, set n_clusters=3, fit KMeans, assign labels
C. Use raw data without scaling, set n_clusters=5, fit KMeans, assign labels
D. Normalize numeric columns, choose n_clusters=3, fit KMeans, assign labels to customers
Solution
Step 1: Prepare data by normalizing numeric columns
Scaling ensures Age, Income, and Spending Score have equal influence on clustering.
Step 2: Set number of clusters and fit KMeans
Choose n_clusters=3 as desired segments, then fit model and assign labels.
Final Answer:
Normalize numeric columns, choose n_clusters=3, fit KMeans, assign labels to customers -> Option D