開始使用免費開始

建模的理由:估計關係

建模的另一個常見用途是:先為兩個資料集各自建立模型,然後比較這兩個模型。在本練習中,提供了兩台車一起公路旅行的資料。兩台車每行駛 50 英里就加油一次,但各自需要加的油量不同,因為兩台車的油耗效率(MPG)不同。請完成函式 efficiency_model(miles, gallons),將效率估計為「平均每加侖燃料可行駛的英里數」。使用已提供的字典 car1car2,它們都有鍵 car['miles']car['gallons']

context figure

本練習屬於課程

Python 線性建模入門

檢視課程

練習說明

  • 完成 efficiency_model(miles, gallons) 的函式定義。
  • 使用該函式計算提供的兩台車(字典 car1car2)的效率。
  • 將結果分別儲存為 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')
編輯並執行程式碼