
本文档介绍了在使用 Databricks AutoML 与 Feature Store 结合时,如何正确指定特征列。 当直接将 Feature Store lookups 传递给 databricks.automl.regress 或 databricks.automl.classify 函数时,可能会遇到问题,特别是当你只想使用 Feature Table 中的部分特征时。本文提供了一种解决方案,通过使用 fe.create_training_set 和 training_set.load_df() 来创建训练数据集,从而允许你在 AutoML 中指定要使用的特征列。
在使用 Databricks AutoML 时,如果你的数据依赖于 Feature Store,并且你希望精确控制哪些特征列被用于训练,直接将 Feature Store lookups 传递给 AutoML 函数可能无法满足需求。这是因为 AutoML 的 regress 和 classify 函数在直接使用 feature_store_lookups 参数时,可能无法让你指定要包含的特征名称,并且排除列的功能可能不适用于 Feature Store 的列。
解决此问题的推荐方法是首先使用 Feature Store 的 API 创建一个训练数据集,然后将该数据集加载到 DataFrame 中,最后将该 DataFrame 传递给 AutoML 函数。
以下步骤展示了如何实现这一目标:
定义 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 中包含的特定特征。
创建训练数据集:
使用 fe.create_training_set 函数创建一个训练数据集。 此函数接受原始数据 DataFrame、FeatureLookup 对象的列表以及目标列的名称。
training_set = fe.create_training_set(
df=raw_data,
feature_lookups=model_feature_lookups,
label=label_name,
)加载 DataFrame:
使用 training_set.load_df() 方法将训练数据集加载到 DataFrame 中。 此 DataFrame 包含原始数据以及从 Feature Store 中查找的特征。
training_df = training_set.load_df()
运行 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']
)通过使用 Feature Store API 创建训练数据集,你可以更灵活地控制哪些特征被用于 Databricks AutoML 训练。 这种方法允许你指定要包含的特征名称,并避免因包含不需要的列而导致的问题。 按照本文档中的步骤,你可以有效地将 Feature Store 集成到你的 AutoML 工作流程中,并获得更好的模型性能。
以上就是在 Databricks AutoML 中指定特征列的方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号