logo
  • 安装
  • 开始
  • 教程
  • 用户指南
  • 资源
  • API 参考
  • 发行说明
  • 控制标签搜索中的截止时间
  • 使用标签转换
  • 数据切片生成器
本页内容
  • 标注客户交易
    • 定义第一个和最后一个截止时间
    • 更改每个客户的第一个截止时间

控制标签搜索中的截止时间¶

标注过程的开始时间称为第一个截止时间。你需要第一个截止时间之前存在的数据来构建特征。你可以在标签搜索中使用 minimum_data 直接定义第一个截止时间,或者定义在第一个截止时间之前所需的数据量。类似地,你可以使用 maximum_data 直接定义最后一个截止时间。这些参数允许你控制标注过程何时开始和结束。

标注客户交易¶

例如,假设你拥有 2021 年第一季度的客户交易数据。

[2]:
import composeml as cp

transactions.head()
[2]:
customer_id transaction_time amount
0 3 2021-03-31 18:51:27 52.29
1 5 2021-03-22 06:56:05 33.81
2 5 2021-03-20 23:45:21 76.30
3 2 2021-03-30 10:06:59 32.72
4 1 2021-02-17 11:01:22 59.16

你想要计算客户在仅限二月的两个星期内花费的总金额。首先定义一个标注函数来汇总交易金额。然后,创建一个标签生成器,该生成器将使用交易时间在两个星期内标注数据。

[3]:
def total_amount(ds):
    return ds.amount.sum()


lm = cp.LabelMaker(
    labeling_function=total_amount,
    time_index='transaction_time',
    target_dataframe_index='customer_id',
    window_size='14d',
)

定义第一个和最后一个截止时间¶

现在,你可以在标签搜索中使用 minimum_data 直接将 2 月 1 日设置为第一个截止时间。由于你正在标注两个星期内的数据,你可以将最后一个截止时间定义为 15 日。

[4]:
lt = lm.search(
    df=transactions.sort_values('transaction_time'),
    num_examples_per_instance=-1,
    minimum_data='2021-02-01',
    maximum_data='2021-02-15',
    drop_empty=False,
    verbose=False,
)

lt
[4]:
customer_id time total_amount
0 1 2021-02-01 31.29
1 1 2021-02-15 59.16
2 2 2021-02-01 49.70
3 2 2021-02-15 86.15
4 3 2021-02-01 0.00
5 3 2021-02-15 28.96
6 4 2021-02-01 128.70
7 4 2021-02-15 59.67
8 5 2021-02-01 0.00
9 5 2021-02-15 0.00

更改每个客户的第一个截止时间¶

假设你有一个查找表,其中包含客户注册和创建账户的日期。现在,你对计算客户在仅限创建账户后的两个星期内花费的总金额感兴趣。

[5]:
created_account
[5]:
customer_id
1   2021-01-10
2   2021-02-12
3   2021-01-23
4   2021-02-13
5   2021-01-24
Name: created_account, dtype: datetime64[ns]

你可以直接使用注册日期列作为标注过程中的第一个截止时间。每个客户应该只有一个截止时间。

[6]:
lt = lm.search(
    df=transactions.sort_values('transaction_time'),
    num_examples_per_instance=-1,
    minimum_data=created_account,
    drop_empty=False,
    verbose=False,
)

lt.head(10)
[6]:
customer_id time total_amount
0 1 2021-01-10 0.00
1 1 2021-01-24 26.15
2 1 2021-02-07 90.45
3 1 2021-02-21 0.00
4 1 2021-03-07 49.64
5 2 2021-02-12 86.15
6 2 2021-02-26 0.00
7 2 2021-03-12 41.08
8 2 2021-03-26 32.72
9 3 2021-01-23 0.00

有关标注特定时期数据的更多详细信息,你可以查看关于生成数据切片的指南。

上一页

用户指南

下一页

使用标签转换

Alteryx Open Source
GitHub Twitter

Copyright