Reasons for Modeling: Estimating Relationships
Another common application of modeling is to compare two data sets by building models for each, and then comparing the models. In this exercise, you are given data for a road trip two cars took together. The cars stopped for gas every 50 miles, but each car did not need to fill up the same amount, because the cars do not have the same fuel efficiency (MPG). Complete the function efficiency_model(miles, gallons)
to estimate efficiency as average miles traveled per gallons of fuel consumed. Use the provided dictionaries car1
and car2
, which both have keys car['miles']
and car['gallons']
.
This exercise is part of the course
Introduction to Linear Modeling in Python
Exercise instructions
- Complete the function definition for
efficiency_model(miles, gallons)
. - Use the function to compute the efficiency of the provided cars (dicts
car1
,car2
). - Store your answers as
car1['mpg']
andcar2['mpg']
. - Complete the following logic statement to print which car (if either) has the best efficiency.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Complete the function to model the efficiency.
def efficiency_model(miles, gallons):
return np.mean( ____ / ____ )
# Use the function to estimate the efficiency for each car.
car1['mpg'] = efficiency_model(car1['____'] , car1['____'] )
car2['mpg'] = efficiency_model(car2['____'] , car2['____'] )
# Finish the logic statement to compare the car efficiencies.
if car1['mpg'] ____ car2['mpg'] :
print('car1 is the best')
elif car1['mpg'] ____ car2['mpg'] :
print('car2 is the best')
else:
print('the cars have the same efficiency')