Representing Data with EntitySets

An EntitySet is a collection of entities and the relationships between them. They are useful for preparing raw, structured datasets for feature engineering. While many functions in Featuretools take entities and relationships as separate arguments, it is recommended to create an EntitySet, so you can more easily manipulate your data as needed.

The Raw Data

Below we have a two tables of data (represented as Pandas DataFrames) related to customer transactions. The first is a merge of transactions, sessions, and customers so that the result looks like something you might see in a log file:

In [1]: import featuretools as ft

In [2]: data = ft.demo.load_mock_customer()

In [3]: transactions_df = data["transactions"].merge(data["sessions"]).merge(data["customers"])

In [4]: transactions_df.sample(10)
Out[4]: 
     transaction_id  session_id    transaction_time product_id  amount  customer_id   device       session_start zip_code  join_date
194             495           4 2014-01-01 00:48:45          4   90.69            3   mobile 2014-01-01 00:43:20    02139 2008-04-10
326             157          19 2014-01-01 04:30:50          1  110.87            2   tablet 2014-01-01 04:29:45    02139 2008-02-20
443             465          21 2014-01-01 05:07:40          1   54.66            4  desktop 2014-01-01 05:03:20    60091 2008-05-30
211              91           4 2014-01-01 01:07:10          2  143.93            3   mobile 2014-01-01 00:43:20    02139 2008-04-10
280             225           7 2014-01-01 01:42:55          3   71.53            2  desktop 2014-01-01 01:40:45    02139 2008-02-20
460               7          22 2014-01-01 05:26:05          3   83.33            4   tablet 2014-01-01 05:22:50    60091 2008-05-30
19               85           2 2014-01-01 00:20:35          4  148.14            1  desktop 2014-01-01 00:17:20    60091 2008-01-01
265             438          34 2014-01-01 08:43:15          4  100.04            3  desktop 2014-01-01 08:28:05    02139 2008-04-10
146             462          11 2014-01-01 02:49:00          1   27.46            5   tablet 2014-01-01 02:47:55    02139 2008-07-19
104             379          27 2014-01-01 06:40:50          4  131.83            1  desktop 2014-01-01 06:37:35    60091 2008-01-01

And the second dataframe is a list of products involved in those transactions.

In [5]: products_df = data["products"]

In [6]: products_df
Out[6]: 
  product_id brand
0          1     B
1          2     B
2          3     C
3          4     A
4          5     C

Creating an EntitySet

First, we initialize an EntitySet and give it an id

In [7]: es = ft.EntitySet(id="transactions")

Adding entities

To get started, we load the transactions dataframe as an entity.

In [8]: es = es.entity_from_dataframe(entity_id="transactions",
   ...:                               dataframe=transactions_df,
   ...:                               index="transaction_id",
   ...:                               time_index="transaction_time",
   ...:                               variable_types={"product_id": ft.variable_types.Categorical})
   ...: 

In [9]: es
Out[9]: 
Entityset: transactions
  Entities:
    transactions [Rows: 500, Columns: 10]
  Relationships:
    No relationships

This method loads each column in the dataframe in as a variable. We can see the variables in an entity using the code below.

In [10]: es["transactions"].variables
Out[10]: 
[<Variable: transaction_id (dtype = index)>,
 <Variable: session_start (dtype: datetime, format: None)>,
 <Variable: product_id (dtype = categorical)>,
 <Variable: transaction_time (dtype: datetime_time_index, format: None)>,
 <Variable: join_date (dtype: datetime, format: None)>,
 <Variable: session_id (dtype = numeric)>,
 <Variable: amount (dtype = numeric)>,
 <Variable: device (dtype = categorical)>,
 <Variable: customer_id (dtype = numeric)>,
 <Variable: zip_code (dtype = categorical)>]

In the call to entity_from_dataframe, we specified three important parameters

  • The index parameter specifies the column that uniquely identifies rows in the dataframe
  • The time_index parameter tells Featuretools when the data was created.
  • The variable_types parameter indicates that “product_id” should be interpreted as a Categorical variable, even though it just an integer in the underlying data.

Now, we can do that same thing with our products dataframe

In [11]: es = es.entity_from_dataframe(entity_id="products",
   ....:                               dataframe=products_df,
   ....:                               index="product_id")
   ....: 

In [12]: es
Out[12]: 
Entityset: transactions
  Entities:
    products [Rows: 5, Columns: 2]
    transactions [Rows: 500, Columns: 10]
  Relationships:
    No relationships

With two entities in our entity set, we can add a relationship between them.

Adding a Relationship

We want to relate these two entities by the columns called “product_id” in each entity. Each product has multiple transactions associated with it, so it is called it the parent entity, while the transactions entity is known as the child entity. When specifying relationships we list the variable in the parent entity first. Note that each ft.Relationship must denote a one-to-many relationship rather than a relationship which is one-to-one or many-to-many.

In [13]: new_relationship = ft.Relationship(es["products"]["product_id"],
   ....:                                    es["transactions"]["product_id"])
   ....: 

In [14]: es = es.add_relationship(new_relationship)

In [15]: es
Out[15]: 
Entityset: transactions
  Entities:
    products [Rows: 5, Columns: 2]
    transactions [Rows: 500, Columns: 10]
  Relationships:
    transactions.product_id -> products.product_id

Now, we see the relationship has been added to our entity set.

Creating entity from existing table

When working with raw data, it is common to have sufficient information to justify the creation of new entities. In order to create a new entity and relationship for sessions, we “normalize” the transaction entity.

In [16]: es = es.normalize_entity(base_entity_id="transactions",
   ....:                          new_entity_id="sessions",
   ....:                          index="session_id",
   ....:                          make_time_index="session_start",
   ....:                          additional_variables=["device", "customer_id", "zip_code", "session_start", "join_date"])
   ....: 

In [17]: es
Out[17]: 
Entityset: transactions
  Entities:
    sessions [Rows: 35, Columns: 6]
    products [Rows: 5, Columns: 2]
    transactions [Rows: 500, Columns: 5]
  Relationships:
    transactions.product_id -> products.product_id
    transactions.session_id -> sessions.session_id

Looking at the output above, we see this method did two operations

  1. It created a new entity called “sessions” based on the “session_id” variable in “transactions”
  2. It added a relationship connecting “transactions” and “sessions”.

If we look at the variables in transactions and the new sessions entity, we see two more operations that were performed automatically.

In [18]: es["transactions"].variables
Out[18]: 
[<Variable: transaction_id (dtype = index)>,
 <Variable: product_id (dtype = categorical)>,
 <Variable: transaction_time (dtype: datetime_time_index, format: None)>,
 <Variable: session_id (dtype = id)>,
 <Variable: amount (dtype = numeric)>]

In [19]: es["sessions"].variables
Out[19]: 
[<Variable: session_id (dtype = index)>,
 <Variable: join_date (dtype: datetime, format: None)>,
 <Variable: session_start (dtype: datetime_time_index, format: None)>,
 <Variable: device (dtype = categorical)>,
 <Variable: customer_id (dtype = numeric)>,
 <Variable: zip_code (dtype = categorical)>]
  1. It removed “device”, “customer_id”, “zip_code”, “session_start” and “join_date” from “transactions” and created a new variables in the sessions entity. This reduces redundant information as the those properties of a session don’t change between transactions.
  2. It marked “session_start” as a time index in the new sessions entity to indicate the beginning of a session. By default, unless it’s explicitly set to another variable, normalize_entity would have made a “first_transactions_time” in this entity. If we don’t want this variable to be created, we can set make_time_index=False.

If we look at the dataframes, can see what the normalize_entity did to the actual data.

In [20]: es["sessions"].df.head(5)
Out[20]: 
   session_id  join_date       session_start   device  customer_id zip_code
1           1 2008-01-01 2014-01-01 00:00:00  desktop            1    60091
2           2 2008-01-01 2014-01-01 00:17:20  desktop            1    60091
3           3 2008-07-19 2014-01-01 00:28:10   mobile            5    02139
4           4 2008-04-10 2014-01-01 00:43:20   mobile            3    02139
5           5 2008-02-20 2014-01-01 01:10:25   tablet            2    02139

In [21]: es["transactions"].df.head(5)
Out[21]: 
     transaction_id product_id    transaction_time  session_id  amount
352             352          4 2014-01-01 00:00:00           1    7.39
186             186          4 2014-01-01 00:01:05           1  147.23
319             319          2 2014-01-01 00:02:10           1  111.34
256             256          4 2014-01-01 00:03:15           1   78.15
449             449          3 2014-01-01 00:04:20           1   33.93

To finish preparing this dataset, create a “customers” entity using the same method call.

In [22]: es = es.normalize_entity(base_entity_id="sessions",
   ....:                          new_entity_id="customers",
   ....:                          index="customer_id",
   ....:                          make_time_index="join_date",
   ....:                          additional_variables=["zip_code", "join_date"])
   ....: 

In [23]: es
Out[23]: 
Entityset: transactions
  Entities:
    customers [Rows: 5, Columns: 3]
    sessions [Rows: 35, Columns: 4]
    products [Rows: 5, Columns: 2]
    transactions [Rows: 500, Columns: 5]
  Relationships:
    transactions.product_id -> products.product_id
    transactions.session_id -> sessions.session_id
    sessions.customer_id -> customers.customer_id

Using the EntitySet

Finally, we are ready to use this EntitySet with any functionality within Featuretools. For example, let’s build a feature matrix for each product in our dataset.

In [24]: feature_matrix, feature_defs = ft.dfs(entityset=es,
   ....:                                       target_entity="products")
   ....: 

In [25]: feature_matrix
Out[25]: 
           brand  MAX(transactions.amount)                     ...                      NUM_UNIQUE(transactions.MONTH(transaction_time))  MODE(transactions.MONTH(transaction_time))
product_id                                                     ...                                                                                                                  
1              B                    148.86                     ...                                                                     1                                           1
2              B                    147.86                     ...                                                                     1                                           1
3              C                    149.95                     ...                                                                     1                                           1
4              A                    149.02                     ...                                                                     1                                           1
5              C                    149.56                     ...                                                                     1                                           1

[5 rows x 22 columns]

As we can see, the features from DFS use the relational structure of our entity set. Therefore it is important to think carefully about the entities that we create.