Exploring Nutritional Data Using K-means Clustering
Nutritional data is a useful playground for unsupervised learning - dozens of nutrient dimensions, no obvious labels, and a real question worth answering: what natural groupings emerge when you let the math sort foods rather than the food pyramid?

Part of the Python and Data Science for .NET Developers series
Phase Modeling, Part 6
Data Science Series — 6 articles
- When the First Visualization Answers the Wrong Question
- Data Science for .NET Developers
- Python: The Language of Data Science
- Exploring Nutritional Data Using K-means Clustering
- Exploratory Data Analysis with Python
- Learning Python in the Post-Copilot World
Filed under Pragmatic architecture and modernization
Topic cluster
Python and Data SciencePython, data analysis, visualization, and machine learning foundations from a .NET developer's perspective.
Understanding K-means Clustering
Clustering, from a data science perspective, refers to the process of grouping a set of objects or data points in such a way that items in the same group (called a cluster) are more similar to each other than to those in other groups. It’s a key technique in unsupervised learning, where the algorithm identifies patterns and relationships in data without predefined labels or categories. The goal of clustering is to uncover hidden structures in data by organizing it into meaningful groups based on their similarities. This approach is widely used in various fields, including customer segmentation, market analysis, image recognition, and medical diagnostics, helping data scientists make sense of complex datasets and draw valuable insights.
Let's look at K-means clustering, a popular algorithm for partitioning data into clusters based on their similarities.
K
The "K" in K-means refers to the number of clusters you want to divide your data into. For example, if you choose K = 3, the algorithm will create three groups based on the similarities in the data.
Means
The "means" part refers to the centroids or averages of the clusters. In K-means clustering, each cluster has a central point, which is the average (or mean) of all the data points in that cluster. The algorithm calculates these means and adjusts them as it organizes the data into clusters.
Clustering
Clustering is the process of grouping similar data points together. In K-means clustering, data points are grouped into K clusters based on their similarity to the centroids. The goal is to minimize the distance between data points and their assigned cluster’s centroid.
By iteratively adjusting the clusters and centroids, K-means efficiently finds patterns and groups within data, making it one of the most widely used techniques for discovering hidden structures in datasets.
K-means Clustering is a method used to group data points into clusters based on their similarities. Each cluster has a center point called a centroid, and data points are grouped by how close they are to this centroid.
K-means clustering is a popular unsupervised machine learning algorithm that partitions data into 𝑘 clusters, where each point belongs to the cluster with the nearest mean (centroid). The algorithm iterates to minimize the variance within clusters, producing an optimal clustering result. This article will walk through its key concepts, applications, and provide answers to common questions.
Student Grade Example
Let’s imagine you’re a teacher with a class of students, and you want to group them based on their grades in subjects like Math, Science, English, Drama, Band, Choir, History, and P.E. You don’t know in advance which students are good at which subjects, so you use K-means clustering to let the data guide you. Using K-means clustering, we can find groups of students who are similar in their performance in these subjects, but unlike other methods, this is an unsupervised approach. Since K-means is an unsupervised clustering algorithm, we don't start with any assumptions about which students belong to which group. Instead, we let the data, in this case the students' grades, lead us to the grouping. The algorithm analyzes their performance across all subjects and forms clusters based on similarities in their grades.
Start with a Guess
At the beginning, you randomly place three imaginary "centers" (these are like starting points) anywhere in the grade data. You don't know yet which students belong to which group, so these centers are just rough guesses.
Group By grades
Next, you look at each student’s grades and see which center they are closest to. For example, a student who has high grades in Math and Science might be grouped with the "math and science" center, while a student with high Drama and Band grades might be closer to the "arts" center.
Move the Center
After assigning students to the nearest group, you update each center by moving it to the average of the grades in its group. For example, if the "math and science" group now has several students, the center moves to the average of their Math and Science grades.
Repeat
You keep repeating this process, assigning students to the nearest center and updating the centers, until the centers no longer move. At this point, the algorithm has found stable groups of students based on their grades. You keep repeating this process—grouping students by the nearest center, moving the centers to the average of each group—until the centers stop moving. This means the groups are now stable, and you’ve found clusters of students based on their grades.
In this way, K-means doesn’t start with any assumptions about which students are good in which subjects. The algorithm uses the grade data to create groups naturally, allowing you to find patterns in how students perform across subjects. Whether they are strong in math or enjoy the arts, the data leads to the final grouping! This is the power of unsupervised learning. Here is a simple example of how K-means clustering might group students based on their grades in different subjects:
- Group 1: Students with high grades in Math and Science might be grouped together. This could indicate a group of students who are strong in math and science subjects.
- Group 2: Students with high grades in Drama, Band, and Choir might naturally group together. These students may enjoy the arts or theatre-related activities.
- Group 3: Students with high grades in P.E. and History could form a group, reflecting students who excel in physical activities or sports.
In this way, K-means clustering allows the data itself to define the groups, helping teachers see patterns in student strengths without making any assumptions beforehand. K-means Clustering helps to identify patterns and group similar data points, making it easier to analyze and understand large datasets.
K-Means Clustering Nutrient Data
I recently analyzed a nutritional dataset from Kaggle using Google Colab, a platform that lets you write and execute Python code in a browser-based Jupyter notebook. The goal was to understand nutrient patterns across various foods and to segment these items based on their nutrient content. You can find the full code and analysis in my Google Drive folder.
Kaggle is an excellent resource for data scientists and enthusiasts to find and share datasets. It offers a vast collection of datasets across various domains, making it a great place to practice data analysis and machine learning techniques. You can find datasets on topics ranging from healthcare to finance, and even niche areas like nutritional data. Kaggle also hosts competitions where you can test your skills against others and learn from the community.
I got my hands on a dataset containing nutritional values for common foods and products, including protein, fat, vitamin C, and fiber content.
I have already performed data exploration and manipulation steps, so let's dive into the key concepts of K-means clustering. We have a clean dataset with the nutrient values of various foods, and we are ready to apply K-means clustering to segment these foods based on their nutrient content.
Random Initialization
K-means starts by randomly selecting 𝑘 initial centroids. Since this process is random, different runs of K-means can yield different results. This issue can be reduced by running the algorithm multiple times and selecting the result with the lowest sum of squared errors (SSE).
// Example pseudocode for random initialization
Initialize k random centroids;
while not converged:
Assign points to the nearest centroid;
Update centroid locations;
End while;Cluster Assignment and Centroid Update
After initialization, the algorithm assigns data points to the nearest centroid based on Euclidean distance. The centroids are then updated by calculating the mean of the points in each cluster. This process is repeated until the centroids stabilize.
// Pseudocode for cluster assignment and update
for each point in dataset:
Assign point to nearest centroid;
Update centroids;
Repeat until centroids no longer change;Sum of Squared Errors (SSE)
K-means aims to minimize the sum of squared errors (SSE), which is the total squared distance between points and their centroids. The lower the SSE, the better the clustering result.
// Formula for SSE
SSE = sum((x - centroid)^2 for all x in cluster);Choosing the Optimal Number of Clusters
Two common methods for selecting the optimal number of clusters are: - Elbow Method: Plot SSE for different values of 𝑘 and look for the "elbow," where adding more clusters no longer significantly reduces SSE. - Silhouette Score: Measures how similar a point is to its own cluster compared to other clusters. The higher the score, the better the clustering.
Scaling Data for K-Means
Scaling is critical in K-means to ensure that all features contribute equally to the distance calculations. Data should be scaled, especially when the features have different units or scales, to prevent larger-scale features from dominating the clustering.
Q1: Why might different runs of K-means produce slightly different results?
K-means relies on random initialization of centroids, which can lead to different clustering results. Running the algorithm multiple times and selecting the result with the lowest SSE can address this issue.
Q2: Does K-means automatically scale the data during the iterations?
No, K-means does not scale data automatically. It is necessary to scale the data manually before applying the algorithm.
Q3: How does K-means compute the centroids for clusters?
The centroids are calculated by taking the mean of all points assigned to each cluster after every iteration.
Q4: Why is it important to specify the number of clusters in K-means?
K-means requires specifying the number of clusters (𝑘) beforehand, as this determines how many centroids are initialized and how the data is partitioned.
Q5: What happens if the number of clusters is equal to the number of data points?
If 𝑘 equals the number of data points, each point will be its own cluster, and the SSE will be zero.
Q6: Is K-means sensitive to outliers?
Yes, K-means can be sensitive to outliers, as they can heavily influence the centroids. Handling outliers before applying K-means is important, or consider using other algorithms like K-medoids.
Q7: What kind of distance metric does K-means use?
K-means typically uses Euclidean distance for measuring the distance between points and centroids.
Q8: Can K-means be used for non-spherical clusters?
K-means works best for spherical clusters with relatively uniform variance. For non-spherical clusters, other algorithms like DBSCAN may be more appropriate.
Dimensionality Reduction with PCA
I applied Principal Component Analysis (PCA) to reduce the complexity of the data. PCA transforms the data into new dimensions, called principal components, which capture the most significant patterns. This allowed me to visualize nutrient relationships in just two dimensions, preserving about 68% of the data’s variance, or information.
Segmentation Using K-means Clustering
I applied K-means clustering, a technique that divides data into clusters based on their similarity. To determine the optimal number of clusters, I plotted an elbow curve using the inertia (sum of squared distances within clusters). The “elbow point” at three clusters indicated a good balance. I confirmed this choice with the silhouette score, which measures how well-separated the clusters are; a score closer to 1 suggests distinct and well-defined clusters.
Key Concepts
This project provided valuable experience with PCA and K-means clustering. While Google Colab allowed me to work interactively, PCA simplified the data by highlighting major patterns, and K-means grouped the data into meaningful clusters, which could be helpful for future analyses like customer segmentation or recommendation systems.
Principal Component Analysis (PCA)
Principal Component Analysis (PCA) is a dimensionality reduction technique that transforms data into a smaller number of uncorrelated variables (principal components) that capture the main patterns in the data.
Imagine you have a lot of data, like test scores in math, science, history, and art. It can be hard to see the big picture when you have many different variables, especially if there are a lot of patterns and connections between them. PCA is a tool that helps you simplify this information. It takes a big set of data with many variables and reduces it to a smaller set of "components" that still capture the most important information.
Principal Component Analysis (PCA)
How PCA Works
Example with Simple Data
Why PCA is Useful
In summary, PCA is a way to look at the big picture of your data by focusing on what’s most important. It turns a complex dataset into something simpler, allowing you to see patterns and relationships that would otherwise be hidden.
Principal Components
Principal Components are the new variables created by PCA that represent the directions of maximum variance in the data. Each principal component is a linear combination of the original variables, and they are uncorrelated with each other.
Here’s a more detailed look at Principal Components:
First Principal Component
Second Principal Component
Subsequent Principal Components
Eigenvalues and Eigenvectors
Interpretation
In summary, Principal Components are essential in PCA as they transform the data into a new set of variables that capture the most significant patterns, making it easier to analyze and visualize complex datasets.
Glossary of Terms
Google Colab
Jupyter Notebook
Dendrogram
Elbow Curve
Inertia
Silhouette Score
Dimensionality Reduction
Explore More
- When the First Visualization Answers the Wrong Question -- Why choosing a chart is part of the analysis
- Data Science for .NET Developers -- Why .NET Developers Should Consider Data Science
- Python: The Language of Data Science -- Understanding Python's Impact on Data Science
- Exploratory Data Analysis with Python -- Master the art of data exploration and visualization with Python's power
- Understanding Neural Networks -- A Beginner's Guide to Neural Networks
Related project evidence

Frogsfolly.com Main
Frogsfolly.com is the original website I created in 1999 when learning web technologies.

PromptSpark
PromptSpark.Chat is a real-time, workflow-driven chat application built with ASP.NET Core, SignalR, and Adaptive Cards. Users interact with branching multi-step workflows through conversational UI, with optional AI-driven responses via IChatCompletionService.

TeachSpark: AI-Driven Educational Platform
TeachSpark is an LLM-powered educational platform built with .NET 10 MVC and a Webpack 5 frontend build system. It provides adaptive learning with personalized content delivery, interactive curriculum with real-time feedback, progress analytics, and comprehensive code quality tooling (ESLint, Prettier, Husky).
Working through something similar?
If this maps to something you're working on, I'm glad to compare notes. Start with the system and the constraint that matters most.