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 date_of_birth
264 380 21 2014-01-01 05:14:10 5 57.09 4 desktop 2014-01-01 05:02:15 60091 2011-04-08 20:08:14 2006-08-15
19 244 10 2014-01-01 02:34:55 2 116.95 2 tablet 2014-01-01 02:31:40 13244 2012-04-15 23:31:04 1986-08-18
314 299 6 2014-01-01 01:32:05 4 64.99 1 tablet 2014-01-01 01:23:25 60091 2011-04-17 10:48:33 1994-07-18
290 78 4 2014-01-01 00:54:10 1 37.50 1 mobile 2014-01-01 00:44:25 60091 2011-04-17 10:48:33 1994-07-18
379 457 27 2014-01-01 06:37:35 1 19.16 1 mobile 2014-01-01 06:34:20 60091 2011-04-17 10:48:33 1994-07-18
335 477 9 2014-01-01 02:30:35 3 41.70 1 desktop 2014-01-01 02:15:25 60091 2011-04-17 10:48:33 1994-07-18
293 103 4 2014-01-01 00:57:25 5 20.79 1 mobile 2014-01-01 00:44:25 60091 2011-04-17 10:48:33 1994-07-18
271 390 22 2014-01-01 05:21:45 2 54.83 4 desktop 2014-01-01 05:21:45 60091 2011-04-08 20:08:14 2006-08-15
404 476 29 2014-01-01 07:24:10 4 121.59 1 mobile 2014-01-01 07:10:05 60091 2011-04-17 10:48:33 1994-07-18
179 90 3 2014-01-01 00:35:45 1 75.73 4 mobile 2014-01-01 00:28:10 60091 2011-04-08 20:08:14 2006-08-15
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 B
3 4 B
4 5 A
Creating an EntitySet¶
First, we initialize an EntitySet. If you’d like to give it name, you can optionally provide an id
to the constructor.
In [7]: es = ft.EntitySet(id="customer_data")
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,
...: "zip_code": ft.variable_types.ZIPCode})
...:
In [9]: es
Out[9]:
Entityset: customer_data
Entities:
transactions [Rows: 500, Columns: 11]
Relationships:
No relationships
Note
You can also display your entity set structure graphically by calling EntitySet.plot()
.
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_id (dtype = numeric)>,
<Variable: transaction_time (dtype: datetime_time_index, format: None)>,
<Variable: amount (dtype = numeric)>,
<Variable: customer_id (dtype = numeric)>,
<Variable: device (dtype = categorical)>,
<Variable: session_start (dtype: datetime, format: None)>,
<Variable: join_date (dtype: datetime, format: None)>,
<Variable: date_of_birth (dtype: datetime, format: None)>,
<Variable: product_id (dtype = categorical)>,
<Variable: zip_code (dtype = zipcode)>]
In the call to entity_from_dataframe
, we specified three important parameters
The
index
parameter specifies the column that uniquely identifies rows in the dataframeThe
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: customer_data
Entities:
transactions [Rows: 500, Columns: 11]
products [Rows: 5, Columns: 2]
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: customer_data
Entities:
transactions [Rows: 500, Columns: 11]
products [Rows: 5, Columns: 2]
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: customer_data
Entities:
transactions [Rows: 500, Columns: 6]
products [Rows: 5, Columns: 2]
sessions [Rows: 35, Columns: 6]
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
It created a new entity called “sessions” based on the “session_id” and “session_start” variables in “transactions”
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: session_id (dtype = id)>,
<Variable: transaction_time (dtype: datetime_time_index, format: None)>,
<Variable: amount (dtype = numeric)>,
<Variable: date_of_birth (dtype: datetime, format: None)>,
<Variable: product_id (dtype = id)>]
In [19]: es["sessions"].variables