建模的理由:估计关系
建模的另一个常见用途是先为两个数据集分别构建模型,然后比较模型,从而比较两个数据集。在本练习中,您将获得两辆车一起自驾游的加油数据。两车每行驶 50 英里就加油一次,但每次加的油量不同,因为两车的燃油效率(MPG)不同。请完成函数 efficiency_model(miles, gallons),将效率估计为单位燃油消耗下的平均行驶里程(即每加仑行驶的平均英里数)。使用已提供的字典 car1 和 car2,它们都包含键 car['miles'] 和 car['gallons']。

本练习是课程的一部分
Python 线性建模入门
练习说明
- 完成
efficiency_model(miles, gallons)的函数定义。 - 使用该函数计算已提供两辆车(字典
car1、car2)的燃油效率。 - 将结果分别存为
car1['mpg']和car2['mpg']。 - 完成以下逻辑语句,打印哪辆车(如果有的话)拥有更高的效率。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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')