Cluster Analysis of Food Ingredients with Python
- Aug 7, 2024
- 1 min read
Updated: Sep 23, 2024
Food and nutrition are very important to human brain development and cognitive functioning. In this article we explore patterns in food ingredients using unsupervised learning technique called clustering.
The data contains the following nutritional values:
Caloric Value
Fat
Saturated Fats
Monounsaturated Fats
Polyunsaturated Fats
Carbohydrates
Sugars
Protein
Iron
Magnesium
Etc.
Cluster analysis is unsupervised learning technique and helps to find hidden trends in the data. For this analysis, we will focused on Caloric Value, Protein and Iron. But many more combinations can be made in order to uncover hidden relationships in the food data.
Overview of the 3 selected parameters in a scatter plot - this is not a cluster plot yet!

As we can see, Caloric Value and Protein levels have some clustering. The data most likely contains hidden trends that we can explore further with cluster analysis.
In Python, we can use the following code with KMeans to create clusters' model.

And this is a plot of our 3 clusters:

Obviously foods with higher Protein tend to have more Iron. Not only that - the Caloric Intake does not have to be so high. As a matter of fact, high Protein foods tend to have less Calories.
How to select the optimum amount of clusters? In Python, this can be done using the following code:


At 5 clusters the errors are the smallest. So we can repeat the same model building and the results will be the following:

Once again, we can see that high protein foods form clusters with higher Iron. Also, once again - the Calories Intake of these foods is not so high.



Comments