【问题标题】:How to write seed_features that include a conditional statement如何编写包含条件语句的种子特征
【发布时间】:2020-01-19 16:02:30
【问题描述】:

我正在尝试编写一个种子功能,它会产生 reward if place == 1 else 0

placereward 都是ft.variable_types.Numeric

Entity: results
  Variables:
    id (dtype: index)
    place (dtype: numeric)
    reward (dtype: numeric)

我尝试了以下替代方法,但没有成功:

备选方案 1

roi = (ft.Feature(es['results']['reward'])
       if (ft.Feature(es['results']['place']) == 1)
       else 0).rename('roi')

产生AssertionError: Column "roi" missing frome dataframe 生成特征时。

备选方案 2

roi = ((ft.Feature(es['results']['place']) == 1) *
       ft.Feature(es['results']['reward'])).rename('roi')

在分配种子特征时产生AssertionError: Provided inputs don't match input type requirements

替代方案 2 应该可以在 Python 中使用:

>>> True * 3.14
3.14
>>> False * 3.14
0.0

完整的堆栈跟踪:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-211-94dd07d98076> in <module>()
     23 
     24
---> 25 roi = ((ft.Feature(es['results']['place']) == 1) * ft.Feature(es['results']['reward'])).rename('roi')

~/dev/venv/lib/python3.6/site-packages/featuretools/feature_base/feature_base.py in __mul__(self, other)
    287     def __mul__(self, other):
    288         """Multiply by other"""
--> 289         return self._handle_binary_comparision(other, primitives.MultiplyNumeric, primitives.MultiplyNumericScalar)
    290 
    291     def __rmul__(self, other):

~/dev/venv/lib/python3.6/site-packages/featuretools/feature_base/feature_base.py in _handle_binary_comparision(self, other, Primitive, PrimitiveScalar)
    230     def _handle_binary_comparision(self, other, Primitive, PrimitiveScalar):
    231         if isinstance(other, FeatureBase):
--> 232             return Feature([self, other], primitive=Primitive)
    233 
    234         return Feature([self], primitive=PrimitiveScalar(other))

~/dev/venv/lib/python3.6/site-packages/featuretools/feature_base/feature_base.py in __new__(self, base, entity, groupby, parent_entity, primitive, use_previous, where)
    755                                                primitive=primitive,
    756                                                groupby=groupby)
--> 757             return TransformFeature(base, primitive=primitive)
    758 
    759         raise Exception("Unrecognized feature initialization")

~/dev/venv/lib/python3.6/site-packages/featuretools/feature_base/feature_base.py in __init__(self, base_features, primitive, name)
    660                                                relationship_path=RelationshipPath([]),
    661                                                primitive=primitive,
--> 662                                                name=name)
    663 
    664     @classmethod

~/dev/venv/lib/python3.6/site-packages/featuretools/feature_base/feature_base.py in __init__(self, entity, base_features, relationship_path, primitive, name, names)
     56         self._names = names
     57 
---> 58         assert self._check_input_types(), ("Provided inputs don't match input "
     59                                            "type requirements")
     60 

AssertionError: Provided inputs don't match input type requirements

【问题讨论】:

  • 备选方案 2 应该可以工作。你能做print(es["results"]) 并分享输出吗?
  • 请分享完整的堆栈跟踪
  • @MaxKanter 我已经包含了堆栈跟踪和类型信息。
  • 这里的问题实际上是乘法原语只支持数字输入,而它可能也应该允许布尔值。我在 Featuretools 中创建了一个问题供我们解决。应该在下一个版本之前完成。 github.com/Featuretools/featuretools/issues/752

标签: pandas featuretools


【解决方案1】:

这应该适用于功能工具v0.11.0。这是一个使用演示数据集的示例。 unit_pricetotal 都是数字。

import featuretools as ft

es = ft.demo.load_retail(nrows=100)
es['order_products']
Entity: order_products
  Variables:
    ...
    unit_price (dtype: numeric)
    total (dtype: numeric)
    ...

我创建了种子特征。

unit_price = ft.Feature(es['order_products']['unit_price'])
total = ft.Feature(es['order_products']['total'])
seed = ((total == 1) * unit_price).rename('seed')

然后,计算特征矩阵。

fm, fd = ft.dfs(target_entity='customers', entityset=es, seed_features=[seed])
fm.filter(regex='seed').columns.tolist()[:5]
['SUM(order_products.seed)',
 'STD(order_products.seed)',
 'MAX(order_products.seed)',
 'SKEW(order_products.seed)',
 'MIN(order_products.seed)']

在您的情况下,这将是种子功能。

place = ft.Feature(es['results']['place'])
reward = ft.Feature(es['results']['reward'])
roi = ((reward == 1) * place).rename('roi')

如果有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 2016-01-07
    • 2022-01-17
    • 2017-02-03
    • 1970-01-01
    • 2022-12-12
    • 2018-12-17
    • 2011-12-31
    相关资源
    最近更新 更多