在 Databricks AutoML 中指定特征列的方法(特征,指定,方法,Databricks,AutoML.......)

feifei123 发布于 2025-08-26 阅读(1)

在 databricks automl 中指定特征列的方法

本文档介绍了在使用 Databricks AutoML 与 Feature Store 结合时,如何正确指定特征列。 当直接将 Feature Store lookups 传递给 databricks.automl.regress 或 databricks.automl.classify 函数时,可能会遇到问题,特别是当你只想使用 Feature Table 中的部分特征时。本文提供了一种解决方案,通过使用 fe.create_training_set 和 training_set.load_df() 来创建训练数据集,从而允许你在 AutoML 中指定要使用的特征列。

使用 Feature Store 创建训练数据集并应用于 AutoML

在使用 Databricks AutoML 时,如果你的数据依赖于 Feature Store,并且你希望精确控制哪些特征列被用于训练,直接将 Feature Store lookups 传递给 AutoML 函数可能无法满足需求。这是因为 AutoML 的 regress 和 classify 函数在直接使用 feature_store_lookups 参数时,可能无法让你指定要包含的特征名称,并且排除列的功能可能不适用于 Feature Store 的列。

解决此问题的推荐方法是首先使用 Feature Store 的 API 创建一个训练数据集,然后将该数据集加载到 DataFrame 中,最后将该 DataFrame 传递给 AutoML 函数。

以下步骤展示了如何实现这一目标:

  1. 定义 Feature Lookups:

    首先,你需要定义一个 FeatureLookup 对象的列表,用于指定要从 Feature Store 中查找的特征。 这些对象指定了 Feature Table 的名称、查找键以及要包含的特征名称。

    from databricks import feature_store as fe
    from databricks.feature_store import FeatureLookup
    
    model_feature_lookups = [
        FeatureLookup(
          table_name="lakehouse_in_action.favorita_forecasting.oil_10d_lag_ft",
          lookup_key="date",
          feature_names="lag10_oil_price"
        ),
        FeatureLookup(
          table_name="lakehouse_in_action.favorita_forecasting.store_holidays_ft",
          lookup_key=["date","store_nbr"]
        ),
        FeatureLookup(
          table_name="lakehouse_in_action.favorita_forecasting.stores_ft",
          lookup_key="store_nbr",
          feature_names=["cluster","store_type"]
        ),
    ]

    请注意,feature_names 参数允许你指定要从每个 Feature Table 中包含的特定特征。

  2. 创建训练数据集:

    使用 fe.create_training_set 函数创建一个训练数据集。 此函数接受原始数据 DataFrame、FeatureLookup 对象的列表以及目标列的名称。

    training_set = fe.create_training_set(
        df=raw_data,
        feature_lookups=model_feature_lookups,
        label=label_name,
    )
  3. 加载 DataFrame:

    使用 training_set.load_df() 方法将训练数据集加载到 DataFrame 中。 此 DataFrame 包含原始数据以及从 Feature Store 中查找的特征。

    training_df = training_set.load_df()
  4. 运行 AutoML:

    现在,你可以将加载的 DataFrame 传递给 databricks.automl.regress 或 databricks.automl.classify 函数。 你还可以使用 exclude_cols 参数排除不需要的列。

    automl_data = training_df.filter("date > '2016-12-31'") # Optional: Filter data for faster execution
    
    summary = databricks.automl.regress(automl_data,
                                        target_col=label_name,
                                        time_col="date",
                                        timeout_minutes=6,
                                        exclude_cols=['id']
                                        )

    注意: exclude_cols 参数用于排除原始数据中的列,而不是 Feature Store 中查找的列。

示例代码

以下是完整的示例代码,展示了如何使用 Feature Store 创建训练数据集并将其应用于 AutoML:

from databricks import feature_store as fe
from databricks.feature_store import FeatureLookup

# 1. Define Feature Lookups
model_feature_lookups = [
    FeatureLookup(
      table_name="lakehouse_in_action.favorita_forecasting.oil_10d_lag_ft",
      lookup_key="date",
      feature_names="lag10_oil_price"
    ),
    FeatureLookup(
      table_name="lakehouse_in_action.favorita_forecasting.store_holidays_ft",
      lookup_key=["date","store_nbr"]
    ),
    FeatureLookup(
      table_name="lakehouse_in_action.favorita_forecasting.stores_ft",
      lookup_key="store_nbr",
      feature_names=["cluster","store_type"]
    ),
]

# 2. Create Training Dataset
training_set = fe.create_training_set(
    df=raw_data,
    feature_lookups=model_feature_lookups,
    label=label_name,
)

# 3. Load DataFrame
training_df = training_set.load_df()

# 4. Run AutoML
automl_data = training_df.filter("date > '2016-12-31'") # Optional: Filter data for faster execution

summary = databricks.automl.regress(automl_data,
                                    target_col=label_name,
                                    time_col="date",
                                    timeout_minutes=6,
                                    exclude_cols=['id']
                                    )

注意事项

  • 确保你已正确安装和配置了 Databricks Feature Store SDK。
  • 在 FeatureLookup 对象中,lookup_key 必须与原始数据 DataFrame 和 Feature Table 中的键列名称匹配。
  • feature_names 参数允许你选择要从 Feature Table 中包含的特定特征。 如果省略 feature_names,则将包含 Feature Table 中的所有特征。
  • exclude_cols 参数仅用于排除原始数据中的列,而不是 Feature Store 中查找的列。
  • 根据数据量和计算资源,调整 timeout_minutes 参数以获得最佳 AutoML 运行时间。

总结

通过使用 Feature Store API 创建训练数据集,你可以更灵活地控制哪些特征被用于 Databricks AutoML 训练。 这种方法允许你指定要包含的特征名称,并避免因包含不需要的列而导致的问题。 按照本文档中的步骤,你可以有效地将 Feature Store 集成到你的 AutoML 工作流程中,并获得更好的模型性能。

以上就是在 Databricks AutoML 中指定特征列的方法的详细内容,更多请关注资源网其它相关文章!

标签:  ai 对象 table 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。