符合时间线的人群(population)
假设您要为一个预测模型构建基表(basetable),该模型用于预测捐赠者是否会在 2018 年捐赠。时间线表明,人群应包含自 2013 年 1 月 1 日以来至少捐赠过一次、但在 2017 年 1 月 1 日之后未再捐赠的所有捐赠者。
给定一个 pandas 数据框 gifts,其中包含自 2010 年以来的所有捐赠记录。在本练习中,您将构建一个集合,包含人群中所有捐赠者的捐赠者 ID。
本练习是课程的一部分
Python 预测分析进阶
练习说明
- 构建数据框
gifts_include,其中包含 2013 年或之后的所有捐赠;并构建数据框gifts_exclude,其中包含 2017 年或之后的所有捐赠。 - 构建集合
donors_include,其中包含gifts_include中所有捐赠者的捐赠者 ID;以及集合donors_exclude,其中包含gifts_exclude中所有捐赠者的捐赠者 ID。 - 使用这两个集合的
.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))