ML之FE:基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生

326 篇文章 72 订阅

ML之FE:基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生

目录

基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生

设计思路

输出结果

实现代码


推荐文章
ML之FE:基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生
ML之FE:基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生实现
ML之FE:基于load_mock_customer数据集(模拟客户,单个DataFrame)利用featuretools工具实现自动构造特征/特征衍生
ML之FE:基于load_mock_customer数据集(模拟客户,单个DataFrame)利用featuretools工具实现自动构造特征/特征衍生生实现

基于load_mock_customer数据集(模拟客户)利用featuretools工具实现自动构造特征/特征衍生

设计思路

输出结果

   customer_id zip_code           join_date date_of_birth
0            1    60091 2011-04-17 10:48:33    1994-07-18
1            2    13244 2012-04-15 23:31:04    1986-08-18
2            3    13244 2011-08-13 15:42:34    2003-11-21
3            4    60091 2011-04-08 20:08:14    2006-08-15
4            5    60091 2010-07-17 05:27:50    1984-07-28
    session_id  customer_id   device       session_start
13          14            1   tablet 2014-01-01 03:28:00
6            7            3   tablet 2014-01-01 01:39:40
1            2            5   mobile 2014-01-01 00:17:20
28          29            1   mobile 2014-01-01 07:10:05
24          25            3  desktop 2014-01-01 05:59:40
     transaction_id  session_id    transaction_time product_id  amount
74              232           5 2014-01-01 01:20:10          1  139.20
231              27          17 2014-01-01 04:10:15          2   90.79
434              36          31 2014-01-01 07:50:10          3   62.35
420              56          30 2014-01-01 07:35:00          3   72.70
54              444           4 2014-01-01 00:58:30          4   43.59
            zip_code  ...  NUM_UNIQUE(transactions.sessions.device)
customer_id           ...                                          
1              60091  ...                                         3
2              13244  ...                                         3
3              13244  ...                                         3
4              60091  ...                                         3
5              60091  ...                                         3

[5 rows x 77 columns]
            customer_id  ... customers.YEAR(join_date)
session_id               ...                          
1                     2  ...                      2012
2                     5  ...                      2010
3                     4  ...                      2011
4                     1  ...                      2011
5                     4  ...                      2011

[5 rows x 44 columns]
<Feature: MODE(transactions.WEEKDAY(transaction_time))>
The most frequently occurring value of the day of the week of the "transaction_time" of all instances of "transactions" for each "session_id" in "sessions".

实现代码


import featuretools as ft


# 1、定义数据集
data = ft.demo.load_mock_customer()
'''
在这个玩具数据集中,有3个表。每个表在Featuretools中称为一个实体。
customers:拥有会话的独特客户
sessions:唯一的会话和相关属性
transactions:此会话中的事件列表
'''
customers_df = data["customers"]
sessions_df = data["sessions"]
transactions_df = data["transactions"]
print(customers_df)
print(sessions_df.sample(5))
print(transactions_df.sample(5))


# 2、DFS设计
# (1)、指定一个包含数据集中所有实体的字典
entities = {"customers" : (customers_df, "customer_id"),
            "sessions" : (sessions_df, "session_id", "session_start"),
            "transactions" : (transactions_df, "transaction_id", "transaction_time")
            }

# (2)、指定实体间如何关联:当两个实体有一对多关系时,我们称之为“one”实体,即“parent entity”。
# 父类和子类之间的关系是这样定义的:
# (parent_entity, parent_variable, child_entity, child_variable)
# 在这个数据集中,我们有两个关系
relationships = [("sessions", "session_id", "transactions", "session_id"),
                 ("customers", "customer_id", "sessions", "customer_id")]

# 为了管理实体和关系的设置,我们建议使用EntitySet类,它为管理这样的数据提供了方便的api




# (3)、运行深度特征合成
'''
DFS的最小输入是一组实体、一组关系和计算特性的“target_entity”。DFS的输出是一个特征矩阵和相应的特征定义列表。
让我们首先为数据中的每个客户创建一个特性矩阵,那么现在有几十个新特性来描述客户的行为。
'''
feature_matrix_customers, features_defs = ft.dfs(entities=entities,
                                                 relationships=relationships,
                                                 target_entity="customers")
print(feature_matrix_customers)



# (4)、改变目标的实体
# DFS如此强大的原因之一是它可以为我们的数据中的任何实体创建一个特征矩阵。例如,如果我们想为会话构建特性
feature_matrix_sessions, features_defs = ft.dfs(entities=entities,
                                                relationships=relationships,
                                                target_entity="sessions")
print(feature_matrix_sessions.head(5))



# (5)、理解特征输出
'''
一般来说,Featuretools通过特性名称引用生成的特性。
为了让特性更容易理解,Featuretools提供了两个额外的工具,Featuretools .graph_feature()和Featuretools .describe_feature(),
来帮助解释什么是特性以及Featuretools生成特性的步骤。
'''

feature = features_defs[18]
print(feature)


# (6)、特征谱系图
#特征谱系图可视地遍历功能生成过程。从基本数据开始,它们一步一步地展示应用的原语和生成的中间特征,以创建最终特征。
import matplotlib.pyplot as plt
ft.graph_feature(feature)
plt.show()




# (7)、特征描述
'''
功能工具还可以自动生成功能的英文句子描述。特性描述有助于解释什么是特性,并且可以通过包含手动定义的自定义来进一步改进。
有关如何自定义自动生成的特性描述的详细信息,请参见生成特性描述。
'''
print(ft.describe_feature(feature))



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值