如何使用FT.DFS结果联接到测试集?.



我知道功能工具有ft.calculate_feature_matrix方法,但它计算数据使用测试。我需要在获得特征时使用训练数据,并加入测试数据,而不是在测试数据上使用相同的功能。 例如: 训练数据:

id sex score
1 f 100
2 f 200
3 m 10
4 m 20

DFS之后,我得到:

id sex score sex.mean(score)
1 f 100 150
2 f 200 150
3 m 10 15
4 m 20 15

我想在测试集上得到这样的结果:

id sex score sex.mean(score)
5 f 30 150
6 f 40 150
7 m 50 15
8 m 60 15

id sex score sex.mean(score)
5 f 30 35
6 f 40 35
7 m 50 55
8 m 60 55

我怎么能实现它,谢谢。

Featuretools 最适合使用时间信息直接注释的数据来处理此类情况。在计算要素时,您可以指定一个"截止时间",之后要过滤掉数据。如果我们重组您的数据,并添加一些时间信息,Featuretools 可以实现您想要的效果。

首先,让我创建一个人员的数据帧

import pandas as pd
people = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6, 7, 8],
"sex": ['f', 'f', 'm', 'm', 'f', 'f', 'm', 'm']})

看起来像这样

id sex
0   1   f
1   2   f
2   3   m
3   4   m
4   5   f
5   6   f
6   7   m
7   8   m

然后,让我们创建一个单独的分数数据帧,在其中注释每个分数及其发生时间。这可以是日期时间或整数。为简单起见,在此示例中,我将使用时间0来训练数据,对测试数据使用时间1

scores = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6, 7, 8],
"person_id": [1, 2, 3, 4, 5, 6, 7, 8],
"time": [0, 0, 0, 0, 1, 1, 1, 1],
"score": [100, 200, 10, 20, 30, 40, 50, 60]})

看起来像这样

id  person_id  score  time
0   1          1    100     0
1   2          2    200     0
2   3          3     10     0
3   4          4     20     0
4   5          5     30     1
5   6          6     40     1
6   7          7     50     1
7   8          8     60     1

现在,让我们在功能工具中创建一个实体集,指定分数实体中的"时间索引">

import featuretools as ft
es = ft.EntitySet('example')
es.entity_from_dataframe(dataframe=people,
entity_id='people',
index='id')
es.entity_from_dataframe(dataframe=scores,
entity_id='scores',
index='id',
time_index= "time")
# create a sexes entity
es.normalize_entity(base_entity_id="people", new_entity_id="sexes", index="sex")
# add relationship for scores to person
scores_relationship = ft.Relationship(es["people"]["id"],
es["scores"]["person_id"])
es = es.add_relationship(scores_relationship)

es

这是我们的实体集

Entityset: example
Entities:
scores [Rows: 8, Columns: 4]
sexes [Rows: 2, Columns: 1]
people [Rows: 8, Columns: 2]
Relationships:
scores.person_id -> people.id
people.sex -> sexes.sex

接下来,让我们计算感兴趣的特征。请注意,当我们使用cutoff_time参数指定上次允许将数据用于计算的时间时。这可确保在计算过程中不会提供任何测试数据。

from featuretools.primitives import Mean
mean_by_sex = ft.Feature(Mean(es["scores"]["score"], es["sexes"]), es["people"])
ft.calculate_feature_matrix(entityset=es, features=[mean_by_sex], cutoff_time=0)

输出现在是

sexes.MEAN(scores.score)
id
1                        150
2                        150
3                         15
4                         15
5                        150
6                        150
7                         15
8                         15

此功能非常强大,因为我们能够以比单个训练/测试拆分更细粒度的方式处理时间。

有关时间索引在功能工具中的工作原理的信息,请阅读文档中的备货时间页面。

编辑

如果要自动定义许多特征,可以通过调用ft.dfs来使用深度特征合成

feature_list = ft.dfs(target_entity="people",
entityset=es,
agg_primitives=["count", "std", "max"],
features_only=True)
feature_list

这将返回可传递给ft.calculate_feature_matrix的特征定义

[<Feature: sex>,
<Feature: MAX(scores.score)>,
<Feature: STD(scores.time)>,
<Feature: STD(scores.score)>,
<Feature: COUNT(scores)>,
<Feature: MAX(scores.time)>,
<Feature: sexes.STD(scores.score)>,
<Feature: sexes.COUNT(people)>,
<Feature: sexes.STD(scores.time)>,
<Feature: sexes.MAX(scores.score)>,
<Feature: sexes.MAX(scores.time)>,
<Feature: sexes.COUNT(scores)>]

在这篇文章中阅读更多关于DFS的信息

最新更新