開始使用免費開始

將數值轉換成機率

在這個練習中,你要把機率分配函式套用到一個 pandas DataFrame,並針對各組相關參數,將小費變數轉換為機率。

這次的轉換是指數轉換。指數分配定義為:

$$ e^{-\lambda * x} * \lambda $$

其中 λ(lambda)是觀測值 x 所屬群組的平均數。

你將在依用餐時間分組之後,把這個指數分配轉換套用到資料集中每張桌子的 size。別忘了 λ 要使用各組的平均值。

在 Python 中,你可以使用 NumPy 函式庫的 np.exp() 來計算指數,並用 .mean() 取得平均值。

本練習屬於課程

使用 pandas 撰寫高效程式碼

檢視課程

練習說明

  • 定義指數分配轉換 exp_tr
  • 依照用餐發生的時間對資料分組。
  • 將該轉換套用到分組後的資料。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Define the exponential transformation
exp_tr = lambda x: ____(____*____) * x.mean()

# Group the data according to the time
restaurant_grouped = restaurant_data.____(____)

# Apply the transformation
restaurant_exp_group = restaurant_grouped['tip'].____(____)
print(restaurant_exp_group.head())
編輯並執行程式碼