【问题标题】:FeatureTools - How to Add 2 Columns Together?FeatureTools - 如何一起添加 2 列?
【发布时间】:2020-11-01 13:07:47
【问题描述】:

我被困住了。使用 Featuretools,我要做的就是创建一个新列,将我的数据集中的两列加在一起,创建一个“堆叠”的排序特征。对我数据集中的所有列执行此操作。

我的代码如下所示:

# Define the function
def feature_engineering_dataset(df):

    es = ft.EntitySet(id = 'stockdata')
    
    # Make the "Date" index an actual column cuz defining it as the index below throws
    # a "can't find Date in index" error for some reason.
    df = df.reset_index()

    # Save some columns not used in Featuretools to concat back later
    dates = df['Date']
    tickers = df['Ticker']
    dailychange = df['DailyChange']
    classes = df['class']

    dataframe = df.drop(['Date', 'Ticker', 'DailyChange', 'class'],axis=1)

    # Define the entity
    es.entity_from_dataframe(entity_id='data', dataframe=dataframe, index='Date') # Won't find Date so uses a numbered index. We'll re-define date as index later

    # Pesky warnings
    warnings.filterwarnings("ignore", category=RuntimeWarning) 
    warnings.filterwarnings("once", category=ImportWarning)

    # Run deep feature synthesis
    feature_matrix, feature_defs = ft.dfs(n_jobs=-2,entityset=es, target_entity='data', 
                                           chunk_size=0.015,max_depth=2,verbose=True,
                    agg_primitives = ['sum'],
                    trans_primitives = []
                    ) 

    # Now re-add previous columnes because featuretools...
    df = pd.concat([dates, tickers, feature_matrix, dailychange, classes], axis=1)
    
    df = df.set_index(['Date'])
    
    # Return our new dataset!
    return(df)

# Now run that defined function
df = feature_engineering_dataset(df)

我不确定这里到底发生了什么,但我已将深度定义为 2,因此我的理解是,对于我的数据集中的每对列的组合,它都会创建一个新列,将两者相加在一起吗?

我的初始数据框形状有 3101 列,当我运行此命令时,它显示 Built 3098 features,而最终的 df 在连接后有 3098 列,这是不对的,它应该具有我所有的原始功能,加上工程设计的。

我怎样才能实现我所追求的目标?功能工具页面和 API 文档上的示例非常令人困惑,并且处理了很多过时的示例,例如“time_since_last”trans 原语和其他似乎不适用于此处的内容。谢谢!

【问题讨论】:

    标签: python-3.x featuretools


    【解决方案1】:

    感谢您的提问。您可以使用转换原语add_numeric 创建一个对两列求和的新列。我将通过一个使用此数据的快速示例。

    id                time      open      high       low     close
     0 2019-07-10 07:00:00  1.053362  1.053587  1.053147  1.053442
     1 2019-07-10 08:00:00  1.053457  1.054057  1.053457  1.053987
     2 2019-07-10 09:00:00  1.053977  1.054192  1.053697  1.053917
     3 2019-07-10 10:00:00  1.053902  1.053907  1.053522  1.053557
     4 2019-07-10 11:00:00  1.053567  1.053627  1.053327  1.053397
    

    首先,我们为数据创建实体集。

    import featuretools as ft
    
    es = ft.EntitySet('stockdata')
    
    es.entity_from_dataframe(
        entity_id='data',
        dataframe=df,
        index='id',
        time_index='time',
    )
    

    现在,我们使用转换原语应用 DFS 来添加数字列。

    feature_matrix, feature_defs = ft.dfs(
        entityset=es,
        target_entity='data',
        trans_primitives=['add_numeric'],
    )
    

    然后,新的工程特征与原始特征一起返回。

    feature_matrix
    
            open      high       low     close  close + high  low + open  high + low  close + open  high + open  close + low
    id
    0   1.053362  1.053587  1.053147  1.053442      2.107029    2.106509    2.106734      2.106804     2.106949     2.106589
    1   1.053457  1.054057  1.053457  1.053987      2.108044    2.106914    2.107514      2.107444     2.107514     2.107444
    2   1.053977  1.054192  1.053697  1.053917      2.108109    2.107674    2.107889      2.107894     2.108169     2.107614
    3   1.053902  1.053907  1.053522  1.053557      2.107464    2.107424    2.107429      2.107459     2.107809     2.107079
    4   1.053567  1.053627  1.053327  1.053397      2.107024    2.106894    2.106954      2.106964     2.107194     2.106724
    

    您可以通过调用函数ft.list_primitives()查看所有内置原语的列表。

    【讨论】:

    • 谢谢!我知道我很接近。我会将 ft.list_primitives() 添加到我的工作流程中,以便熟悉这些转换。
    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2011-03-15
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    相关资源
    最近更新 更多