【发布时间】:2020-01-22 10:00:01
【问题描述】:
背景
对于 CRM 项目,我有机会的快照。我已经能够使用featuretools 构建许多功能,但我真正想要的是拥有历史胜利计数和比率。换句话说,我想知道:
对于给定的机会,在最后一次修改机会之前已经赢得了多少笔交易?
示例数据
import pandas as pd
import featuretools as ft
df = pd.DataFrame(
{'OpportunityId': [111, 111, 222, 222, 222, 333, 333, 333],
'UpdateId': [1,3,2,5,7,4,6,8],
'Label': ['Open', 'Win', 'Open', 'Open', 'Win', 'Open', 'Open', 'Open'],
'CreatedOn': pd.to_datetime(['9/27/18','9/27/18','9/28/18','9/28/18','9/28/18','10/2/18','10/2/18','10/2/18']),
'ModifiedOn': pd.to_datetime(['9/27/18','10/1/18','9/28/18','10/3/18','10/7/18','10/2/18','10/6/18','10/10/18']),
'EstRevenue': [2000, 2000, 80000, 84000, 78000, 100000, 95000, 110000]})
df
| OpportunityId | UpdateId | Label | CreatedOn | ModifiedOn | EstRevenue |
|---------------|----------|-------|------------|------------|------------|
| 111 | 1 | Open | 2018-09-27 | 2018-09-27 | 2000 |
| 111 | 3 | Win | 2018-09-27 | 2018-10-01 | 2000 |
| 222 | 2 | Open | 2018-09-28 | 2018-09-28 | 80000 |
| 222 | 5 | Open | 2018-09-28 | 2018-10-03 | 84000 |
| 222 | 7 | Win | 2018-09-28 | 2018-10-07 | 78000 |
| 333 | 4 | Open | 2018-10-02 | 2018-10-02 | 100000 |
| 333 | 6 | Open | 2018-10-02 | 2018-10-06 | 95000 |
| 333 | 8 | Open | 2018-10-02 | 2018-10-10 | 110000 |
期望的输出
| OPPORTUNITIES | Label | CreatedOn | Max( ModifiedOn ) | AVG( EstRevenue ) | Wins |
|---------------|-------|-----------|-------------------|------------------:|------|
| 111 | Win | 9/27/18 | 10/1/18 | 2000 | 0 |
| 222 | Win | 9/28/18 | 10/7/18 | 80667 | 1 |
| 333 | Open | 10/2/18 | 10/10/18 | 101667 | 2 |
目前的尝试
让我难以理解的是……
- 依赖于多个机会的功能...我需要一个单独的实体吗?
- 如何聚合同时提供两者的
Label:-
Label的当前值,以及 -
Label列为0时的计数
-
我面临的挑战是Label 专栏...虽然通常我会创建一个CurrentLabel 专栏,但我很确定ft 可以处理这个...
es = (ft.EntitySet(id='CRM')
.entity_from_dataframe(
entity_id='updates',
dataframe=df,
index='UpdateId',
time_index='ModifiedOn')
.normalize_entity(
base_entity_id='updates',
new_entity_id='opportunities',
index='OpportunityId',
make_time_index='CreatedOn',
copy_variables=['Label'],
additional_variables=['CreatedOn']
)
)
es['updates']['Label'].interesting_values = ['Win']
Entityset: CRM
Entities:
updates [Rows: 8, Columns: 5]
opportunities [Rows: 3, Columns: 3]
Relationships:
updates.OpportunityId -> opportunities.OpportunityId
feature_matrix, feature_defs = ft.dfs(
entityset=es,
target_entity="opportunities",
agg_primitives=[
"mean","count","num_unique","time_since_first"],
trans_primitives=[
'time_since_previous'],
where_primitives=[
"sum","count"],
max_depth=2,
verbose=1
)
【问题讨论】:
-
'Wins'和'Label'列中聚合结果的逻辑是什么?我了解其他列,但不了解这些列。 -
Label是机会是“赢”还是“输”,我试图预测。Wins列是问题的答案:在最后一次修改机会之前已经赢得了多少笔交易?
标签: python pandas python-3.6 featuretools