Tuning Deep Feature Synthesis

There are several parameters that can be tuned to change the output of DFS.

In [1]: import featuretools as ft

In [2]: es = ft.demo.load_mock_customer(return_entityset=True)

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

Using “Seed Features”

Seed features are manually defined, problem specific, features a user provides to DFS. Deep Feature Synthesis will then automatically stack new features on top of these features when it can.

By using seed features, we can include domain specific knowledge in feature engineering automation.

In [4]: expensive_purchase = ft.Feature(es["transactions"]["amount"]) > 125

In [5]: feature_matrix, feature_defs = ft.dfs(entityset=es,
   ...:                                       target_entity="customers",
   ...:                                       agg_primitives=["percent_true"],
   ...:                                       seed_features=[expensive_purchase])
   ...: 

In [6]: feature_matrix[['PERCENT_TRUE(transactions.amount > 125)']]
Out[6]: 
             PERCENT_TRUE(transactions.amount > 125)
customer_id                                         
1                                           0.213740
2                                           0.172131
3                                           0.115385
4                                           0.144144
5                                           0.224138

We can now see that PERCENT_TRUE was automatically applied to this boolean variable.

Add “interesting” values to variables

Sometimes we want to create features that are conditioned on a second value before we calculate. We call this extra filter a “where clause”.

By default, where clauses are built using the interesting_values of a variable.

In [7]: es["sessions"]["device"].interesting_values = ["desktop", "mobile", "tablet"]

We then specify the aggregation primitive to make where clauses for using where_primitives

In [8]: feature_matrix, feature_defs = ft.dfs(entityset=es,
   ...:                                       target_entity="customers",
   ...:                                       agg_primitives=["count", "avg_time_between"],
   ...:                                       where_primitives=["count", "avg_time_between"],
   ...:                                       trans_primitives=[])
   ...: 

In [9]: feature_matrix
Out[9]: 
            zip_code  COUNT(sessions)                               ...                                AVG_TIME_BETWEEN(sessions.session_start WHERE device = desktop)  AVG_TIME_BETWEEN(sessions.session_start WHERE device = tablet)
customer_id                                                         ...                                                                                                                                                               
1              60091               10                               ...                                                                      5254.166667                                                              NaN             
2              02139                8                               ...                                                                     14755.000000                                                           9295.0             
3              02139                5                               ...                                                                      2827.500000                                                              NaN             
4              60091                8                               ...                                                                      4842.500000                                                           4160.0             
5              02139                4                               ...                                                                              NaN                                                          15860.0             

[5 rows x 11 columns]

Now, we have several new potentially useful features. For example, the two features below tell us how many sessions a customer completed on a tablet, and the time between those sessions.

In [10]: feature_matrix[["COUNT(sessions WHERE device = tablet)", "AVG_TIME_BETWEEN(sessions.session_start WHERE device = tablet)"]]
Out[10]: 
             COUNT(sessions WHERE device = tablet)  AVG_TIME_BETWEEN(sessions.session_start WHERE device = tablet)
customer_id                                                                                                       
1                                              1.0                                                NaN             
2                                              3.0                                             9295.0             
3                                              0.0                                                NaN             
4                                              3.0                                             4160.0             
5                                              2.0                                            15860.0             

We can see that customer who only had 0 or 1 sessions on a tablet, had NaN values for average time between such sessions.

Encoding categorical features

Machine learning algorithms typically expect all numeric data. When Deep Feature Synthesis generates categorical features, we need to encode them.

In [11]: feature_matrix, feature_defs = ft.dfs(entityset=es,
   ....:                                       target_entity="customers",
   ....:                                       agg_primitives=["mode"],
   ....:                                       max_depth=1)
   ....: 

In [12]: feature_matrix
Out[12]: 
            zip_code MODE(sessions.device)  DAY(join_date)  YEAR(join_date)  MONTH(join_date)  WEEKDAY(join_date)
customer_id                                                                                                      
1              60091               desktop               1             2008                 1                   1
2              02139                mobile              20             2008                 2                   2
3              02139               desktop              10             2008                 4                   3
4              60091               desktop              30             2008                 5                   4
5              02139                tablet              19             2008                 7                   5

This feature matrix contains 2 categorical variables, zip_code and MODE(sessions.device). We can use the feature matrix and feature definitions to encode these categorical values. Featuretools offers functionality to apply one hot encoding to the output of DFS.

In [13]: feature_matrix_enc, features_enc = ft.encode_features(feature_matrix, feature_defs)

In [14]: feature_matrix_enc
Out[14]: 
             zip_code = 02139  zip_code = 60091  zip_code = unknown  MODE(sessions.device) = desktop              ...               WEEKDAY(join_date) = 3  WEEKDAY(join_date) = 2  WEEKDAY(join_date) = 1  WEEKDAY(join_date) = unknown
customer_id                                                                                                       ...                                                                                                                   
1                           0                 1                   0                                1              ...                                    0                       0                       1                             0
2                           1                 0                   0                                0              ...                                    0                       1                       0                             0
3                           1                 0                   0                                1              ...                                    1                       0                       0                             0
4                           0                 1                   0                                1              ...                                    0                       0                       0                             0
5                           1                 0                   0                                0              ...                                    0                       0                       0                             0

[5 rows x 27 columns]

The returned feature matrix is now all numeric. Additionally, we get a new set of feature definitions that contain the encoded values.

In [15]: print(features_enc)
[<Feature: zip_code = 02139>, <Feature: zip_code = 60091>, <Feature: zip_code = unknown>, <Feature: MODE(sessions.device) = desktop>, <Feature: MODE(sessions.device) = tablet>, <Feature: MODE(sessions.device) = mobile>, <Feature: MODE(sessions.device) = unknown>, <Feature: DAY(join_date) = 30>, <Feature: DAY(join_date) = 20>, <Feature: DAY(join_date) = 19>, <Feature: DAY(join_date) = 10>, <Feature: DAY(join_date) = 1>, <Feature: DAY(join_date) = unknown>, <Feature: YEAR(join_date) = 2008>, <Feature: YEAR(join_date) = unknown>, <Feature: MONTH(join_date) = 7>, <Feature: MONTH(join_date) = 5>, <Feature: MONTH(join_date) = 4>, <Feature: MONTH(join_date) = 2>, <Feature: MONTH(join_date) = 1>, <Feature: MONTH(join_date) = unknown>, <Feature: WEEKDAY(join_date) = 5>, <Feature: WEEKDAY(join_date) = 4>, <Feature: WEEKDAY(join_date) = 3>, <Feature: WEEKDAY(join_date) = 2>, <Feature: WEEKDAY(join_date) = 1>, <Feature: WEEKDAY(join_date) = unknown>]

These features can be used to calculate the same encoded values on new data. For more information on feature engineering in production, read Deployment.