Memuat
Memuat
Want to go deeper?
Join one of our courses or ask us directly on WhatsApp.
Picked from the software, topic, and level of “Class Machine Learning Python #13: Clustering: Customer Segmentation”.
Other ways we can help with Python.
Learn with a mentor
Scheduled AI & Machine Learning courses with live guidance and a certificate.

Multi-Objective Optimization Course: NSGA-II, MOPSO & Hybrid Algorithms (Python)

ECG Arrhythmia Classification Course with Wavelet & Deep Learning (PyTorch)

ANFIS + PSO/GA Course for Non-Linear Time Series Forecasting (Python)
Have it built for you
No time to learn Python? Our team can do the project for you.

Python Machine Learning — Prediction Models, Customer Segmentation, Fraud Detection & AI Apps

AI Image Creation with Stable Diffusion — Unlimited Custom Visual Production
★ 4.9

AI Image & Video Creation with Leonardo AI — All-in-One Visuals for Business
★ 4.9
Get notified about new Python classes
At most one email a week. Unfollow anytime with one click.
The full narration of this video. The current sentence is highlighted while the video plays; click any sentence to jump there.
Here is the final result, customers of a mall automatically divided into five segments, from frugal customers to sultan-like customers, complete with the center of each group. There are no labels at all, the algorithm finds the groups on its own. We also compare K-Means with Hierarchical Clustering and The Base Can. Let's get started. Hello, welcome to episode 13 of the Machine Learning with Python series. We're entering Unsupervised Learning, where the data has no labels. The most popular first task is clustering, which is grouping similar data. A real-life example is customer segmentation. The marketing team wants to know what customer groups exist, so their promotions can be targeted properly. We create a file called pelanggan.py.
The data consists of 200 mall customers with two features, annual income in millions of rupiah and a shopping score from 1 to 100. We create from five hidden groups, but we don't save the labels, since in the real world we actually don't know. The first scatter plot shows the points without color.
K-Means needs the number of groups K. How do we choose it? The elbow method plots inertia, which is the sum of squared distances of points to the center of their group, for K from 1 to 10. We look for the elbow, the point where the decrease starts to level off. The second method, silhouette score, measures how well each point fits in its group compared to other groups, the higher the better. Both methods point to K equal to 5.
We train K-Means with 5 groups. Features are first standardized so that income and score have the same weight. We visualize the results with color per group, and the center of each group is marked with a black cross.
New clustering is useful if we can explain each group. With group buying, we calculate the average income, shopping score, and number of members in each group. From here, the marketing team can name clusters, for example, high income and high spending customers are Sultan customers. High income but low spending customers are frugal customers who need to be persuaded.
The second algorithm is Hierarchical Clustering. Create a file named hierarki.py. Aglomerative Clustering starts with each point as its own cluster, then repeatedly merges the two closest clusters. The process can be visualized as a dendrogram. Cutting the dendrogram at a certain height determines the number of clusters.
The third algorithm, DBSCAN, clusters based on density, and there is no need to determine the number of clusters. We test it on the moon dataset. K-Means fails because it always creates round clusters, while DBSCAN successfully follows the shape of the moon dataset. As a bonus, isolated points are labeled as minus one as nice.
Done. We have performed customer segmentation using K-Means, selected the optimal K using elbow and silhouette methods, created group profiles, and learned about Hierarchical Clustering and DBSCAN. In the next episode, we learn about dimensionality reduction with PCA, t-SNE, and UMAP. Want to master machine learning and data science? Cerdas Inovasi Digital offers Python courses, machine learning, and AI from the basics, as well as predictive model development services. Data analysis, and AI applications for business or research, as well as selling complete source code of this tutorial. Want to master all series of this, from machine learning introduction to deploying models to the web? All 18 episodes, complete with step-by-step PDF guides, are available to premium tutorial subscribers. Subscribe starting at Rp69.000 per month, save 28% with the 12-month package, or one-time payment of Rp999.000 for lifetime access. Want just one class? Buy single classes with lifetime access. Subscribe now on the premium page at cerdasinovasidigital.com. Contact us via WhatsApp, or visit cerdasinovasidigital.com. Thank you for watching, see you in the next episode.
Type a question or keyword; we find the part of this episode that explains it and jump the video there.
Notes are saved with the current video time and are only visible to you.
Stuck on a step? Ask here; the instructor and other learners can answer.
No questions yet.

Cerdas Inovasi Digital Team
Software developers, BIM & 3D engineers, and AI practitioners
We build software, BIM models, games and AI automations for clients every day, and turn that hands-on work into step-by-step classes: every step is shown on screen and comes with a PDF guide you can keep.
See our clients →