開始使用免費開始

計算彙總後的 target

假設你要建立一個預測模型,用來找出哪些捐款者在某個月份最有可能捐超過 50 歐元。

已給定一個基礎表 basetable,其中人口中的每位捐款者各有一列,欄位 donor_id 代表捐款者。依照時間軸,若捐款者在 2017 年 1 月捐款超過 50 歐元,target 應為 1,否則為 0。

pandas 的 dataframe gifts_201701 包含 2017 年 1 月的所有捐款記錄。在本練習中,你會把 target 欄位加入到基礎表中。

本練習屬於課程

Python 中級預測分析

檢視課程

練習說明

  • 建立 gifts_summed,對 gifts_201701 中每位捐款者的捐款金額加總。
  • gifts_summed 推導出清單 targets,其中包含在 target 期間捐款超過 50 歐元的捐款者。
  • 將 target 加到基礎表。
  • 計算並列印 target 的發生率。

動手互動練習

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

# Sum of donations for each donor in gifts_201701
gifts_summed = ____.groupby("____")["____"].____().reset_index()

# List with targets
targets = list(gifts_summed["id"][____["____"] > ____])

# Add targets to the basetable
basetable["target"] = pd.Series([____ if donor_id in targets else ____ for donor_id in basetable["donor_id"]])

# Calculate and print the target incidence
print(round(____["____"].____() / ____(____), 2))
編輯並執行程式碼