Create a file, fastfood.py, that loads the fastfood.csv file and runs a regression predicting calories from total_fat, sat_fat, cholesterol, and sodium, in that order and then add a constant using sm.add_constant(data).
fastfood.py
import sys
import pandas
import statsmodels.api as sm
df = pandas.read_csv('fastfood.csv')
data = df[['total_fat', 'sat_fat', 'cholesterol', 'sodium', 'calories']]
data.head()
x_value = data[['total_fat', 'sat_fat', 'cholesterol', 'sodium']]
y_value = data[['calories']].values
x_value = sm.add_constant(x_value)
sm_model = sm.OLS(y_value, x_value)
model = sm_model.fit()
print(model.mse_total.round(2))
print(model.rsquared.round(2))
print(model.params.round(2))
print(model.pvalues.round(2))
Test Case Examples:
print(model.mse_total.round(2))
print(model.rsquared.round(2))
print(model.params.round(2))
print(model.pvalues.round(2))
Test Case
test Run python3 fastfood.py and match its output to an expected value.
Expected Output:
79770.18
0.9
.{0,}71.73
total_fat 9.10
sat_fat 0.60
cholesterol 0.21
sodium 0.16
dtype: float64
.{0,}0.00
total_fat 0.00
sat_fat 0.64
cholesterol 0.07
sodium 0.00
dtype: float64
Error Message
Traceback (most recent call last):
File "fastfood.py", line 4, in <module>
data = pandas.read_csv(sys.argv[1])
IndexError: list index out of range
Fastfood.csv File (Snippet)
"","restaurant","item","calories","cal_fat","total_fat","sat_fat","trans_fat","cholesterol","sodium","total_carb","fiber","sugar","protein","vit_a","vit_c","calcium","salad"
"1","Mcdonalds","Artisan Grilled Chicken Sandwich",380,60,7,2,0,95,1110,44,3,11,37,4,20,20,"Other"
"2","Mcdonalds","Single Bacon Smokehouse Burger",840,410,45,17,1.5,130,1580,62,2,18,46,6,20,20,"Other"
"3","Mcdonalds","Double Bacon Smokehouse Burger",1130,600,67,27,3,220,1920,63,3,18,70,10,20,50,"Other"
"4","Mcdonalds","Grilled Bacon Smokehouse Chicken Sandwich",750,280,31,10,0.5,155,1940,62,2,18,55,6,25,20,"Other"
"5","Mcdonalds","Crispy Bacon Smokehouse Chicken Sandwich",920,410,45,12,0.5,120,1980,81,4,18,46,6,20,20,"Other"
"6","Mcdonalds","Big Mac",540,250,28,10,1,80,950,46,3,9,25,10,2,15,"Other"
"7","Mcdonalds","Cheeseburger",300,100,12,5,0.5,40,680,33,2,7,15,10,2,10,"Other"
"8","Mcdonalds","Classic Chicken Sandwich",510,210,24,4,0,65,1040,49,3,6,25,0,4,2,"Other"
"9","Mcdonalds","Double Cheeseburger",430,190,21,11,1,85,1040,35,2,7,25,20,4,15,"Other"
"10","Mcdonalds","Double Quarter Pounder® with Cheese",770,400,45,21,2.5,175,1290,42,3,10,51,20,6,20,"Other"
"11","Mcdonalds","Filet-O-Fish®",380,170,18,4,0,40,640,38,2,5,15,2,0,15,"Other"
"12","Mcdonalds","Garlic White Cheddar Burger",620,300,34,13,1.5,95,790,48,3,11,32,10,10,35,"Other"
"13","Mcdonalds","Grilled Garlic White Cheddar Chicken Sandwich",530,180,20,7,0,125,1150,48,3,11,42,10,20,35,"Other"
"14","Mcdonalds","Crispy Garlic White Cheddar Chicken Sandwich",700,300,34,9,0,85,1190,67,5,11,33,10,15,35,"Other"
"15","Mcdonalds","Hamburger",250,70,8,3,0,30,480,31,2,6,13,2,2,4,"Other"
"16","Mcdonalds","Lobster Roll",290,50,5,1.5,0,65,630,35,2,3,24,4,6,15,"Other"
"17","Mcdonalds","Maple Bacon Dijon 1/4 lb Burger",640,330,36,14,1.5,110,1260,40,3,10,37,6,15,15,"Other"
"18","Mcdonalds","Grilled Maple Bacon Dijon Chicken Sandwich",580,190,21,8,0,135,1890,50,3,14,48,4,30,30,"Other"
"19","Mcdonalds","Crispy Maple Bacon Dijon Chicken Sandwich",740,310,35,9,0.5,95,1780,69,5,14,39,4,20,290,"Other"
"20","Mcdonalds","McChicken",350,130,15,3.5,0,40,600,40,2,5,15,2,2,4,"Other"
"21","Mcdonalds","McDouble",380,160,18,8,1,70,840,34,2,7,23,10,2,10,"Other"
"22","Mcdonalds","McRib",480,200,22,7,0,80,870,45,2,12,25,2,2,6,"Other"
"23","Mcdonalds","Pico Guacamole 1/4 lb Burger",580,300,33,12,1.5,95,920,41,4,7,29,8,15,15,"Other"
"24","Mcdonalds","Grilled Pico Guacamole Chicken Sandwich",520,160,18,6,0,115,1540,50,4,12,40,8,25,30,"Other"
"25","Mcdonalds","Crispy Pico Guacamole Chicken Sandwich",680,280,32,7,0,80,1430,69,6,12,31,8,15,30,"Other"
"26","Mcdonalds","Premium Buttermilk Crispy Chicken Deluxe Sandwich",570,200,23,5,0,60,1050,64,4,11,28,4,10,20,"Other"
"27","Mcdonalds","Premium Crispy Chicken Deluxe Sandwich",530,200,22,4,0,45,1000,59,3,13,25,6,10,20,"Other"
"28","Mcdonalds","Quarter Pounder® with Cheese",530,240,27,13,1.5,100,1090,41,3,10,31,20,6,15,"Other"
"29","Mcdonalds","Signature Sriracha Burger",670,320,35,12,1.5,95,1010,56,4,13,32,20,15,30,"Other"
"30","Mcdonalds","Grilled Signature Sriracha Chicken Sandwich",560,180,20,5,0,115,1550,56,4,14,41,20,25,30,"Other"
"31","Mcdonalds","Crispy Signature Sriracha Chicken Sandwich",730,300,33,7,0,80,1430,75,5,13,32,20,20,30,"Other"
"32","Mcdonalds","Sweet BBQ Bacon 1/4 lb Burger",690,340,37,14,1.5,110,1310,51,3,14,38,6,15,15,"Other"
"33","Mcdonalds","Grilled Sweet BBQ Bacon Chicken Sandwich",630,200,22,7,0,135,1930,61,4,18,48,4,30,25,"Other"
"34","Mcdonalds","Crispy Sweet BBQ Bacon Chicken Sandwich",800,320,36,9,0.5,95,1820,80,5,18,39,4,20,30,"Other"
"35","Mcdonalds","3 piece Buttermilk Crispy Chicken Tenders",370,190,21,3.5,0,70,910,16,0,0,28,0,0,2,"Other"
"36","Mcdonalds","4 piece Buttermilk Crispy Chicken Tenders",480,250,28,4.5,0,95,1290,21,0,1,38,0,0,2,"Other"
"37","Mcdonalds","6 piece Buttermilk Crispy Chicken Tenders",760,390,44,8,0.5,145,1890,32,1,1,58,0,0,2,"Other"
"38","Mcdonalds","10 piece Buttermilk Crispy Chicken Tenders",1210,630,70,12,1,240,3230,52,1,4,94,0,0,4,"Other"
"39","Mcdonalds","12 piece Buttermilk Crispy Chicken Tenders",1510,790,88,15,1,295,3770,64,1,2,115,0,2,6,"Other"
"40","Mcdonalds","20 piece Buttermilk Crispy Chicken Tenders",2430,1270,141,24,2,475,6080,103,2,3,186,0,2,8,"Other"
"41","Mcdonalds","4 Piece Chicken McNuggets",180,100,11,2,0,30,340,11,1,0,10,0,2,0,"Other"
"42","Mcdonalds","6 Piece Chicken McNuggets",270,140,16,2.5,0,45,510,16,1,0,15,0,2,0,"Other"
"43","Mcdonalds","10 Piece Chicken McNuggets",440,240,27,4.5,0,75,840,26,2,0,24,0,4,2,"Other"
"44","Mcdonalds","20 Piece Chicken McNuggets",890,480,53,9,0,145,1680,53,4,0,49,0,8,4,"Other"
"45","Mcdonalds","40 piece Chicken McNuggets",1770,960,107,18,0.5,295,3370,105,7,1,98,0,15,6,"Other"
"46","Mcdonalds","4 piece Sweet N' Spicy Honey BBQ Glazed Tenders",640,240,27,4,0,105,1780,63,2,35,39,4,15,4,"Other"
"47","Mcdonalds","6 piece Sweet N' Spicy Honey BBQ Glazed Tenders",960,360,40,6,0,160,2670,94,3,52,58,4,25,8,"Other"
"48","Mcdonalds","10 piece Sweet N' Spicy Honey BBQ Glazed Tenders",1600,600,66,10,0,265,4450,156,5,87,97,8,40,10,"Other"
"49","Mcdonalds","Premium Asian Salad w/o Chicken",140,70,7,0.5,0,0,20,13,5,7,7,180,45,10,"Other"
"50","Mcdonalds","Premium Asian Salad w/ Grilled Chicken",270,80,9,1,0,80,740,18,5,10,31,180,70,10,"Other"
"51","Mcdonalds","Premium Asian Salad w/ Crispy Chicken",490,250,28,8,0,95,1120,28,4,4,33,180,60,15,"Other"
"52","Mcdonalds","Premium Bacon Ranch Salad w/o Chicken",190,110,12,5,0,40,660,9,3,3,14,180,50,15,"Other"
"53","Mcdonalds","Premium Bacon Ranch Salad w/ Grilled Chicken",320,120,14,6,0,45,1230,9,3,4,42,180,60,15,"Other"
"54","Mcdonalds","Premium Bacon Ranch Salad w/ Crispy Chicken",490,250,28,8,0,95,1120,28,4,4,33,180,60,15,"Other"
"55","Mcdonalds","Premium Southwest Salad w/o Chicken",220,90,10,3.5,0,15,500,26,6,9,8,180,40,20,"Other"
"56","Mcdonalds","Premium Southwest Salad w/ Grilled Chicken",350,100,12,4.5,0,110,1070,27,6,9,37,180,50,20,"Other"
"57","Mcdonalds","Premium Southwest Salad w/ Crispy Chicken",520,230,25,6,0,75,960,46,8,9,28,180,40,20,"Other"
"58","Chick Fil-A","Chargrilled Chicken Club Sandwich",430,144,16,8,0,85,1120,37,3,7,37,30,40,25,"Other"
"59","Chick Fil-A","Chargrilled Chicken Sandwich",310,54,6,2,0,55,820,36,3,7,29,25,40,10,"Other"
"60","Chick Fil-A","Chick-n-Slider",270,99,11,2.5,0,45,800,26,1,4,16,NA,0,2,"Other"
"61","Chick Fil-A","1 Piece Chick-n-Strips",120,54,6,3,0,25,320,6,0,1,11,0,0,2,"Other"
"62","Chick Fil-A","2 Piece Chick-n-Strips",230,108,12,3,0,55,630,13,1,1,22,0,2,4,"Other"
"63","Chick Fil-A","3 Piece Chick-n-Strips",350,153,17,3,0,70,940,22,1,3,28,2,2,6,"Other"
"64","Chick Fil-A","4 piece Chick-n-Strips",470,207,23,3,0,90,1250,29,1,4,37,2,4,8,"Other"
"65","Chick Fil-A","Chicken Deluxe",500,207,23,7,0,75,1590,42,3,6,31,30,10,20,"Other"
"66","Chick Fil-A","4 piece Chicken Nuggets",130,54,6,1.5,0,40,490,5,1,0,14,0,2,2,"Other"
"67","Chick Fil-A","6 piece Chicken Nuggets",190,81,9,1.5,0,55,730,7,1,0,21,0,4,2,"Other"
"68","Chick Fil-A","8 piece Chicken Nuggets",260,110,12,3,0,70,990,9,1,1,28,0,2,4,"Other"
"69","Chick Fil-A","12 piece Chicken Nuggets",390,162,18,1.5,0,115,1460,14,2,1,41,0,8,4,"Other"
"70","Chick Fil-A","30 piece Chicken Nuggets",970,414,46,2.5,0,285,3660,35,4,1,103,NA,20,10,"Other"
"71","Chick Fil-A","Chicken Salad Sandwich",490,170,19,3,0,80,1130,55,5,12,28,35,8,15,"Other"
"72","Chick Fil-A","Chicken Sandwich",440,171,19,4,0,60,1350,40,2,5,28,2,4,15,"Other"
"73","Chick Fil-A","4 Piece Grilled Chicken Nuggets",70,18,2,1,0,35,220,1,0,0,13,0,6,0,"Other"
"74","Chick Fil-A","6 Piece Grilled Chicken Nuggets",110,27,3,1,0,50,330,2,0,0,19,0,8,0,"Other"
"75","Chick Fil-A","8 piece Grilled Chicken Nuggets",140,36,4,1,0,70,440,2,0,0,25,0,10,2,"Other"
"76","Chick Fil-A","12 Piece Grilled Chicken Nuggets",210,45,5,1,0,100,670,3,0,1,38,0,20,2,"Other"
"77","Chick Fil-A","Spicy Grilled Chicken Sub Sandwich",430,108,12,4.5,0,85,1310,47,5,9,33,NA,25,25,"Other"
"78","Chick Fil-A","Regular Grilled Chicken Sub Sandwich",450,117,13,6,0,75,1000,48,4,10,34,NA,50,25,"Other"
"79","Chick Fil-A","Smokehouse BBQ Bacon Sandwich",500,162,18,0,0,95,1200,46,2,10,33,45,40,20,"Other"
"80","Chick Fil-A","Spicy Chicken Sandwich",450,171,19,4,0,60,1620,41,1,5,29,4,2,15,"Other"
CodePudding user response:
Are you sure that you have something in sys.argv
import sys
>>> sys.argv[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> sys.argv
['']