符合時間軸的母體
假設你要為一個預測模型建立基礎資料表(basetable),用來預測捐款人在 2018 年是否會捐款。時間軸顯示母體應包含自 2013 年 1 月 1 日起至少捐過一次、且在 2017 年 1 月 1 日之後沒有任何捐款的所有捐款人。
已提供自 2010 年以來所有捐款紀錄的 pandas DataFrame gifts。在本練習中,你將建立一個集合,內含母體中所有捐款人的捐款人編號(donor id)。
本練習屬於課程
Python 中級預測分析
練習說明
- 建立一個 dataframe
gifts_include,包含 2013 年(含)之後的所有捐款;以及一個 dataframegifts_exclude,包含 2017 年(含)之後的所有捐款。 - 建立集合
donors_include,包含gifts_include中所有捐款人的捐款人編號;以及集合donors_exclude,包含gifts_exclude中所有捐款人的捐款人編號。 - 使用兩個集合的
.difference()方法來建立母體。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Gifts made in 2013 or later
gifts_include = ____[____[____].dt.year >= ____]
# Gifts made in 2017 or later
gifts_exclude = ____[____[____].dt.year >= ____]
# Set with ids in gifts_include
donors_include = ____(____[____])
# Set with ids in gifts_exclude
donors_exclude = ____(____[____])
# Population
population = ____.difference(____)
print(len(population))