【问题标题】:Keep Feature Definitions in Dictionary and Rerturn the feature to the client将特征定义保留在字典中并将特征返回给客户端
【发布时间】:2021-02-24 21:03:55
【问题描述】:

我有以下字典,用于将特征定义保存为字符串。

    features = {
  "journey_email_been_sent_flag": "F.when(F.col('email_14days') > 0,F.lit(1)).otherwise(F.lit(0))",
  "journey_opened_flag": "F.when(F.col('opened_14days') > 0, F.lit(1)).otherwise(F.lit(0))"
}
retrieved_features = {}
non_retrieved_features = {}

或将其作为定义本身。

    features = {
  "journey_email_been_sent_flag": F.when(F.col('email_14days') > 0,F.lit(1)).otherwise(F.lit(0)),
  "journey_opened_flag": F.when(F.col('opened_14days') > 0, F.lit(1)).otherwise(F.lit(0))
}

下面是检索特征定义的代码

 def feature_extract(*featurenames):
  for featurename in featurenames:
    if featurename in features:
      print(f"{featurename} : {features[featurename]}")
      retrieved_features[featurename] = features[featurename]
    else:
      print('failure')
      non_retrieved_features[featurename] = "Not Found in the feature defenition"
  return retrieved_features

这就是我如何调用检索特征的函数

feature_extract('journey_email_been_sent_flag','journey_opened_flag')

但是当我试图检索未来时它不起作用,当我在字典中保留定义时收到以下结果

Out[19]: {'journey_email_been_sent_flag': Column<b'CASE WHEN (email_14days > 0) THEN 1 ELSE 0 END'>}

当我在数据框中调用如下特征检索时。

.withColumn('journey_email_been_sent_flag', feature_extract('journey_email_been_sent_flag'))

遇到错误

AssertionError: col should be Column

【问题讨论】:

  • 请修正您的缩进并准确地描述什么不适合您。正如给定的那样,这是您想要对其进行代码审查的非工作代码 - 而不是 Stack Overflow。
  • 你好@Prune,你有机会看看我的问题吗?当我在功能中使用列定义时,我将其检索为 Out[19]: {'journey_email_been_sent_flag': Column 0) THEN 1 ELSE 0 END'>} 而当我在我想获得该功能的地方调用它,我得到以下错误。有什么办法可以解决。 AssertionError: col 应该是 Column .withColumn('journey_email_been_sent_flag', feature_extract('journey_email_been_sent_flag'))
  • 我明白了,问题是投了反对票,因此没有得到回应。你能建议@prune
  • @UninformedUser 你有什么想法吗
  • @mck 你有什么想法吗

标签: python pyspark feature-extraction feature-selection


【解决方案1】:

我可以通过这种方式解决它

我将特征定义保留为定义

    features = {
  "journey_email_been_sent_flag": F.when(F.col('email_14days') > 0,F.lit(1)).otherwise(F.lit(0)),
  "journey_opened_flag": F.when(F.col('opened_14days') > 0, F.lit(1)).otherwise(F.lit(0))
}

并使用 F.lit 调用 feature_extract 函数

F.lit(feature_extract('journey_email_been_sent_flag').get('journey_email_been_sent_flag'))

【讨论】:

    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2017-08-14
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多