Below we will use Clustergrammer's
Network to load the data, swap in zero for missing data, normalize the nutrient columns (so that they can be more easily compared), and finally filter the foods (rows) to only show the top 4000 foods (out of ~7,000) based on their sum across nutrients. This row filtering makes our visualization easier to work with and will tend to remove foods with a large amount of missing nutrient data.
# import clustergrammer_widget and instantiate network instance from clustergrammer_widget import * net = Network(clustergrammer_widget) # load matrix file net.load_file('USDA_nutrients_clean.txt') # swap missing values for zero net.swap_nan_for_zero() # normalize nutrient columns so they can be more easily compared net.normalize(axis='col', norm_type='zscore', keep_orig=True) # set the maximum absolute value of any matrix cell to 10 # since we do not care about extreme outliers (this also improves the look of the visualization) net.clip(-10, 10) # filter down the foods to only keep those with the highest sum across all nutrients (e.g. most nutritious) # keep top 4,000 foods out of ~7,000 net.filter_N_top('row', 4000, 'sum') # cluster the data net.cluster()
Once we have loaded and pre-processed the data, we can visualize it with Clustergrammer (you can increase the opacity with the slider to improve visibility). We can see some broad trends:
# generate the widget visualization net.widget()
Clustergrammer allows us to interactively explore the dataset:
Here we are viewing the nutritional data of only 'Fast Foods'. Again, we see the same two clusters of foods characterized in part by high and low water content.
net.filter_cat('row', 1, 'Fast Foods') net.cluster(enrichrgram=False) net.widget()
We can double-click the saturated [fat] (g) nutrient column to find the fast food with the greatest level of saturated fat per serving, which is nachos with cinnamon and sugar.