Skip to content
What comprises good food
  • AI Chat
  • Code
  • Report
  • What is good food?

    📖 Background

    You and your friend have gotten into a debate about nutrition. Your friend follows a high-protein diet and does not eat any carbohydrates (no grains, no fruits). You claim that a balanced diet should contain all nutrients but should be low in calories. Both of you quickly realize that most of what you know about nutrition comes from mainstream and social media.

    Being the data scientist that you are, you offer to look at the data yourself to answer a few key questions.

    💾 The data

    You source nutrition data from USDA's FoodData Central website. This data contains the calorie content of 7,793 common foods, as well as their nutritional composition. Each row represents one food item, and nutritional values are based on a 100g serving. Here is a description of the columns:

    • FDC_ID: A unique identifier for each food item in the database.
    • Item: The name or description of the food product.
    • Category: The category or classification of the food item, such as "Baked Products" or "Vegetables and Vegetable Products".
    • Calories: The energy content of the food, presented in kilocalories (kcal).
    • Protein: The protein content of the food, measured in grams.
    • Carbohydrate: The carbohydrate content of the food, measured in grams.
    • Total fat: The total fat content of the food, measured in grams.
    • Cholesterol: The cholesterol content of the food, measured in milligrams.
    • Fiber: The dietary fiber content of the food, measured in grams.
    • Water: The water content of the food, measured in grams.
    • Alcohol: The alcohol content of the food (if any), measured in grams.
    • Vitamin C: The Vitamin C content of the food, measured in milligrams.

    💪 Competition challenge

    Create a report that covers the following:

    1. Which fruit has the highest vitamin C content? What are some other sources of vitamin C?
    2. Describe the relationship between the calories and water content of a food item.
    3. What are the possible drawbacks of a zero-carb diet? What could be the drawbacks of a very high-protein diet?
    4. According to the Cleveland Clinic website, a gram of fat has around 9 kilocalories, and a gram of protein and a gram of carbohydrate contain 4 kilocalories each. Fit a linear model to test whether these estimates agree with the data.
    5. Analyze the errors of your linear model to see what could be the hidden sources of calories in food.
    import pandas as pd
    import numpy as np
    import statsmodels.api as sm
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    sns.set_style('whitegrid')
    pd.set_option('display.float_format', '{:,.2f}'.format)
    food = pd.read_csv('data/nutrition.csv')
    nutrition_cols = ['Calories', 'Protein', 'Carbohydrate', 'Total fat', 'Cholesterol', 'Fiber', 'Water', 'Alcohol', 'Vitamin C']
    
    for col in nutrition_cols:
        food[f'{col}'] = food[col].str.extract("(\d+.\d+)").astype(float)
    
    food['tags'] = food['Item'].apply(lambda x: x.split(','))

    Which fruit has the highest vitamin C content? What are some other sources of vitamin C?

    Highest vitamin C content

    df = food.copy()
    fruits = food[(food['Category'] == 'Fruits and Fruit Juices')]
    fruits.sort_values(by = 'Vitamin C', ascending = False).dropna(subset=['Vitamin C']).iloc[:1]

    What are some other sources of vitamin C

    df = food.sort_values(by = 'Vitamin C', ascending = False).dropna(subset=['Vitamin C'])
    df = df[df['Vitamin C'] > 0]
    df.head(10)[df.columns[:-1]]

    Describe the relationship between the calories and water content of a food item.

    plt.figure(figsize = (16,6))
    df = food[['Item','Calories', 'Water']]
    
    sns.regplot(data = df, x = 'Calories', y = 'Water', scatter_kws={'alpha': 0.3}, line_kws={'color': 'red'})
    plt.xlabel("Calories (kcal)")
    plt.ylabel("Water (g)")
    
    plt.tight_layout()
    # Correlation between Calories and Water
    df.corr(numeric_only = True).iloc[0][1]
    X = df['Calories']
    y = df['Water']
    X = sm.add_constant(X)
    model = sm.OLS(y, X).fit()
    model.summary()