Creating Entity Set in Featuretools error TypeError: 'str' object does not support item assignment

Creating Entity Set in Featuretools error TypeError: 'str' object does not support item assignment

我有这 3 个数据帧:

df_train cortado:____________________ 
    SK_ID_CURR  TARGET  NAME_CONTRACT_TYPE_Cash loans  \
0      100002       1                              1   
1      100003       0                              1   
2      100004       0                              0   
3      100006       0                              1   
4      100007       0                              1   

   NAME_CONTRACT_TYPE_Revolving loans  CODE_GENDER_F  CODE_GENDER_M  
0                                   0              0              1  
1                                   0              1              0  
2                                   1              0              1  
3                                   0              1              0  
4                                   0              0              1  

df_bureau cortado:____________________ 
    SK_ID_CURR  SK_ID_BUREAU  CREDIT_ACTIVE_Active
0      100002       5714464                     1
1      100002       5714465                     1
2      215354       5714466                     1
3      215354       5714467                     1
4      215354       5714468                     1

bureau_balance cortado 3:____________________ 
    SK_ID_BUREAU  MONTHS_BALANCE  STATUS_C
0       5715448               0         1
1       5715448              -1         1
2       5715448              -2         1
3       5715448              -3         1
4       5715448              -4         1 

这是我正在尝试 运行 特征合成的脚本:

entities = {
    "train"          : (df_train,         "SK_ID_CURR"),
    "bureau"         : (df_bureau,        "SK_ID_BUREAU"),
    "bureau_balance" : (df_bureau_balance,"MONTHS_BALANCE", "STATUS", "SK_ID_BUREAU")                       , 
    }

relationships = [
    ("bureau", "SK_ID_BUREAU", "bureau_balance", "SK_ID_BUREAU"),
    ("train", "SK_ID_CURR", "bureau", "SK_ID_CURR")
             ]

feature_matrix_customers, features_defs = ft.dfs(entities=entities,
                                             relationships=relationships,
                                             target_entity="train"
                                             )

但是,每当我引入列 "STATUS" 时,都会发生此错误: 类型错误:'str' 对象不支持项目分配

如果我不放置 "STATUS" 列,数据框的几行就可以了。 当行数增加时(并且只将 STATUS 作为键可以解决它),会发生另一个错误: AssertionError:索引在数据帧上不是唯一的(实体bureau_balance)

提前致谢!!

你是对的,数据框需要一个唯一的索引才能成为一个实体。一个简单的选择是使用

将唯一索引添加到 df_bureau_balance

df_bureau_balance.reset_index(inplace = True)

然后制作实体:

entities = {
    "train"          : (df_train,         "SK_ID_CURR"),
    "bureau"         : (df_bureau,        "SK_ID_BUREAU"),
    "bureau_balance" : (df_bureau_balance, "index")
    }

更好的选择是 use entitysets to represent your data。当我们从 df_bureau_balance 创建一个实体时,因为它没有唯一索引,我们传入 make_index = True 和索引的名称(这可以是任何名称,前提是它还不是数据。)其余的与您的工作非常相似,只是语法略有不同!这是一个完整的工作示例:

# Create the entityset
es = ft.EntitySet('customers')

# Add the entities to the entityset
es = es.entity_from_dataframe('train', df_train, index = 'SK_ID_CURR')
es = es.entity_from_dataframe('bureau', df_bureau, index = 'SK_ID_BUREAU')
es = es.entity_from_dataframe('bureau_balance', df_bureau_balance, 
                               make_index = True, index = 'bureau_balance_index')

# Define the relationships
r_train_bureau = ft.Relationship(es['train']['SK_ID_CURR'], es['bureau']['SK_ID_CURR'])
r_bureau_balance = ft.Relationship(es['bureau']['SK_ID_BUREAU'], 
                                   es['bureau_balance']['SK_ID_BUREAU'])

# Add the relationships
es = es.add_relationships([r_train_bureau, r_bureau_balance])

# Deep feature synthesis
feature_matrix_customers, feature_defs = ft.dfs(entityset=es, target_entity = 'train')

Entitysets 帮助您在单一结构中跟踪所有数据! Featuretools documentation 有助于了解使用实体集的基础知识,我建议您阅读它。

caseWestern 的回答是在 Featuretools 中创建 EntitySet 的推荐方法。

也就是说,您看到的错误是因为 Featuretools 期望实体的 4 个值是变量类型是字典 dict[str -> Variable] 的地方。现在,您只为第 4 个参数传递一个字符串,因此 Featuretools 在尝试添加条目时会失败,因为它实际上不是字典。

您可以查看documentation for Entity Set了解更多信息。