0
0
Data Analysis Pythondata~10 mins

Customer segmentation pattern in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Customer segmentation pattern
Load customer data
Select features for segmentation
Apply clustering algorithm
Assign cluster labels to customers
Analyze and visualize segments
Use segments for targeted actions
This flow shows how customer data is loaded, features are selected, clustering groups customers, and segments are analyzed for business use.
Execution Sample
Data Analysis Python
import pandas as pd
from sklearn.cluster import KMeans

# Load data
customers = pd.DataFrame({'Age':[25,45,35,23,52],'Income':[50000,64000,58000,45000,72000]})

# Cluster
kmeans = KMeans(n_clusters=2, random_state=0).fit(customers)
customers['Segment'] = kmeans.labels_
This code loads a small customer dataset, applies KMeans clustering with 2 groups, and assigns segment labels.
Execution Table
StepActionData ShapeCluster CentersSegment Labels
1Load customer data(5, 2)N/AN/A
2Initialize KMeans with 2 clusters(5, 2)N/AN/A
3Fit KMeans to data(5, 2)[[24.0, 47500.0], [44.0, 64666.66666667]]N/A
4Assign cluster labels(5, 3)[[24.0, 47500.0], [44.0, 64666.66666667]][0, 1, 1, 0, 1]
5Final segmented data(5, 3)[[24.0, 47500.0], [44.0, 64666.66666667]][0, 1, 1, 0, 1]
💡 Clustering complete, segments assigned to all customers.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
customersEmpty(5, 2) DataFrame(5, 3) DataFrame(5, 3) DataFrame with Segment
kmeans.cluster_centers_N/A[[24.0, 47500.0], [44.0, 64666.66666667]][[24.0, 47500.0], [44.0, 64666.66666667]][[24.0, 47500.0], [44.0, 64666.66666667]]
kmeans.labels_N/AN/A[0, 1, 1, 0, 1][0, 1, 1, 0, 1]
Key Moments - 3 Insights
Why do the cluster centers have values like [24.0, 47500.0]?
These centers represent the average Age and Income of customers in each cluster, calculated during fitting (see Step 3 in execution_table).
Why are segment labels assigned only after fitting the model?
Labels depend on the clustering result, so they are only available after the model learns clusters from data (see Step 4).
What does the 'Segment' column in the final DataFrame represent?
It shows which cluster each customer belongs to, enabling segmentation for analysis or marketing (see Step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the segment label for the customer with Age 35?
A1
B0
C2
DN/A
💡 Hint
Check the Segment Labels column at Step 4; the third label corresponds to the third customer (Age 35).
At which step do we first see the cluster centers calculated?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the Cluster Centers column; centers appear after fitting the model.
If we change n_clusters to 3, what will change in the execution_table?
ASegment labels will remain the same
BNumber of cluster centers will be 3
CData shape will change
DCluster centers will be empty
💡 Hint
Number of clusters controls how many centers are computed (see Step 3 Cluster Centers).
Concept Snapshot
Customer Segmentation Pattern:
- Load customer data with features (e.g., Age, Income)
- Use clustering (e.g., KMeans) to group customers
- Assign cluster labels as segments
- Analyze segments for targeted marketing
- Key: cluster centers represent group averages
Full Transcript
Customer segmentation groups customers by similar traits. We load data, select features like Age and Income, then apply KMeans clustering. The algorithm finds cluster centers representing average customer profiles. Each customer gets a segment label showing their group. This helps businesses target marketing or services. The execution table shows steps from loading data to assigning segments. Variable tracking shows how data and model outputs change. Key moments clarify cluster centers and label assignment. Quizzes test understanding of labels, cluster centers, and effect of changing cluster count.