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.
import pandas as pd
df_food = pd.read_csv('nutrition.csv')
df_food
SELECT item, "Vitamin C"
FROM df_food
WHERE Category LIKE 'Fruit%'
ORDER BY "Vitamin C" DESC;
💪 Competition challenge
Create a report that covers the following:
- Which fruit has the highest vitamin C content? What are some other sources of vitamin C?
- Describe the relationship between the calories and water content of a food item.
- What are the possible drawbacks of a zero-carb diet? What could be the drawbacks of a very high-protein diet?
- 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.
- Analyze the errors of your linear model to see what could be the hidden sources of calories in food.
🧑⚖️ Judging criteria
This competition is for helping to understand how competitions work. This competition will not be judged.
✅ Checklist before publishing into the competition
- Rename your workspace to make it descriptive of your work. N.B. you should leave the notebook name as notebook.ipynb.
- Remove redundant cells like the judging criteria, so the workbook is focused on your story.
- Make sure the workbook reads well and explains how you found your insights.
- Try to include an executive summary of your recommendations at the beginning.
- Check that all the cells run without error
⌛️ Time is ticking. Good luck!
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df_food = pd.read_csv('nutrition.csv')
df_food['Water'] = df_food['Water'].str.replace(' g','').astype(float)
df_food['Calories'] = df_food['Calories'].str.replace(' kcal','').astype(float)
fig, ax = plt.subplots(nrows = 1, ncols = 1)
# I want to either color code this by the nine product categories or I want to break it into different plots"
plt.scatter(df_food['Water'],df_food['Calories'])
plt.xlabel('Water Content (g)')
plt.ylabel('Calorides (kcal)')
plt.title('Water Content vs Calories')
plt.show()
df_zero_carb = df_food[df_food["Carbohydrate"] == "0.0 g"]
# Ensure that the "Calories" column is of type string before using .str accessor
df_zero_carb["Calories"] = df_zero_carb["Calories"].astype(str).str.replace(' kcal', '').astype(float)
df_zero_carb["Protein"] = df_zero_carb["Protein"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Carbohydrate"] = df_zero_carb["Carbohydrate"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Total fat"] = df_zero_carb["Total fat"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Cholesterol"] = df_zero_carb["Cholesterol"].astype(str).str.replace(' mg', '').astype(float)
df_zero_carb["Fiber"] = df_zero_carb["Fiber"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Water"] = df_zero_carb["Water"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Alcohol"] = df_zero_carb["Alcohol"].astype(str).str.replace(' g', '').astype(float)
df_zero_carb["Vitamin C"] = df_zero_carb["Vitamin C"].astype(str).str.replace(' mg', '').astype(float)
# Clean up df_food to compare
df_food["Calories"] = df_food["Calories"].astype(str).str.replace(' kcal', '').astype(float)
df_food["Protein"] = df_food["Protein"].astype(str).str.replace(' g', '').astype(float)
df_food["Carbohydrate"] = df_food["Carbohydrate"].astype(str).str.replace(' g', '').astype(float)
df_food["Total fat"] = df_food["Total fat"].astype(str).str.replace(' g', '').astype(float)
df_food["Cholesterol"] = df_food["Cholesterol"].astype(str).str.replace(' mg', '').astype(float)
df_food["Fiber"] = df_food["Fiber"].astype(str).str.replace(' g', '').astype(float)
df_food["Water"] = df_food["Water"].astype(str).str.replace(' g', '').astype(float)
df_food["Alcohol"] = df_food["Alcohol"].astype(str).str.replace(' g', '').astype(float)
df_food["Vitamin C"] = df_food["Vitamin C"].astype(str).str.replace(' mg', '').astype(float)
# heres the plan - create a new dataframe with nutrition from both current dataframes. Keep columns separate. Relabel with the diet. Then plot histograms to compare similar nutrition info#
new_diet_df = pd.DataFrame({
'reg_diet_cals': df_food['Calories'],
'zero_carb_cals': df_zero_carb['Calories'],
'reg_diet_cals': df_food['Calories'],
'zero_carb_cals': df_zero_carb['Calories'],
'reg_diet_total_fat': df_food['Total fat'],
'zero_carb_total_fat': df_zero_carb['Total fat'],
'reg_diet_protein': df_food['Protein'],
'zero_carb_protein': df_zero_carb['Protein'],
'reg_diet_cholesterol': df_food['Cholesterol'],
'zero_carb_cholesterol': df_zero_carb['Cholesterol'],
'reg_diet_fiber': df_food['Fiber'],
'zero_carb_fiber': df_zero_carb['Fiber'],
'reg_diet_water': df_food['Water'],
'zero_carb_water': df_zero_carb['Water'],
'reg_diet_vit_c': df_food['Vitamin C'],
'zero_carb_vit_c': df_zero_carb['Vitamin C']
})
#print(new_diet_df.head())
#fig, ax = plt.subplot()
#plt.hist(new_diet_df['reg_diet_cals'], bins = 20)
#plt.show()
# Create a single plot with both histograms
plt.figure(figsize=(10, 6))
# Histogram for 'reg_diet_cals'
plt.hist(new_diet_df['reg_diet_cals'], bins=20, color='blue', alpha=0.7, label='Regular Diet Calories')
# Histogram for 'zero_carb_cals'
plt.hist(new_diet_df['zero_carb_cals'], bins=20, color='green', alpha=0.7, label='Zero Carb Diet Calories')
plt.title('Calories Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Calories')
plt.ylabel('Frequency')
plt.legend()
plt.show()
#Histograms for 'reg_diet_fat'& 'zeor_carb_fat'
plt.hist(new_diet_df['reg_diet_total_fat'], bins=15, color='blue', alpha=0.7, label='Regular Diet Total Fat')
plt.hist(new_diet_df['zero_carb_total_fat'], bins=15, color='green', alpha=0.7, label='Zero Carb Diet Total Fat')
plt.title('Fat Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Total Fat')
plt.ylabel('Frequency')
plt.legend()
plt.show()
#Histograms for 'reg_diet_protein'& 'zeor_carb_protein'
plt.hist(new_diet_df['reg_diet_protein'], bins=50, color='blue', alpha=0.7, label='Regular Diet Protein')
plt.hist(new_diet_df['zero_carb_protein'], bins=50, color='orange', alpha=0.7, label='Zero Carb Diet Protein')
plt.title('Cholesterol Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Total Protein')
plt.ylabel('Frequency')
plt.legend()
plt.show()
#Histograms for 'reg_diet_cholesterol'& 'zeor_carb_cholesterol'
plt.hist(new_diet_df['reg_diet_cholesterol'], bins=50, color='blue', alpha=0.7, label='Regular Diet Cholesterol')
plt.hist(new_diet_df['zero_carb_cholesterol'], bins=50, color='orange', alpha=0.7, label='Zero Carb Diet Cholesterol')
plt.title('Cholesterol Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Total Cholesterol')
plt.ylabel('Frequency')
plt.legend()
plt.show()
#Histograms for 'reg_diet_fiber'& 'zeor_carb_fiber'
plt.hist(new_diet_df['reg_diet_fiber'], bins=50, color='blue', alpha=0.7, label='Regular Diet Fiber')
plt.hist(new_diet_df['zero_carb_fiber'], bins=50, color='red', alpha=0.7, label='Zero Carb Diet Fiber')
plt.title('Cholesterol Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Total Fiber')
plt.ylabel('Frequency')
plt.legend()
plt.show()
#Histograms for 'reg_diet_vit_c'& 'zeor_carb_vit_c'
plt.hist(new_diet_df['reg_diet_vit_c'], bins=50, color='blue', alpha=0.7, label='Regular Diet Vitamin C')
plt.hist(new_diet_df['zero_carb_vit_c'], bins=50, color='orange', alpha=0.7, label='Zero Carb Diet Vitamin C')
plt.title('Cholesterol Distribution: Regular Diet vs Zero Carb Diet')
plt.xlabel('Total Vitamin C')
plt.ylabel('Frequency')
plt.legend()
plt.show()
import statsmodels.api as sm
# Define the independent variables (predictors)
X = df_food[['Total fat', 'Protein', 'Carbohydrate']]
# Add a constant to the model (intercept)
X = sm.add_constant(X)
# Define the dependent variable (response)
y = df_food['Calories']
# Fit the linear model
model = sm.OLS(y, X).fit()
# Display the model summary
model.summary()
The model works pretty well. The R-squared is .990, p stat is well uner 0.05 for each variable, and the coeeficisnts are very close to what was theorized. Possible limitiations include interaction between the terms, and the fact that the variables do not fit a gaussian distribution