本文档介绍了在使用 Databricks AutoML 和 Feature Store 时,如何正确地指定特征列。直接使用 Feature Store lookups 可能会导致问题,本文提供了一种通过创建训练集并加载 DataFrame 来解决此问题的方法,并展示了完整的代码示例和注意事项,帮助你成功运行 AutoML 实验。
使用 Feature Store 和 Databricks AutoML
Databricks AutoML 可以自动地进行模型选择、超参数调优和特征工程,从而简化机器学习流程。当结合 Databricks Feature Store 使用时,可以方便地将预先计算好的特征加入到训练数据中。然而,直接在 databricks.automl.regress 或 databricks.automl.classify 函数中使用 feature_store_lookups 参数时,可能会遇到一些问题,尤其是在需要精确控制哪些特征被包含时。
问题描述
直接将 Feature Store lookups 传递给 AutoML 函数时,feature_names 参数可能不会按照预期工作。此外,尝试使用 exclude_cols 排除来自 Feature Store lookup tables 的列也会失败。
解决方案:创建训练集并加载 DataFrame
解决这个问题的推荐方法是首先使用 Feature Store API 创建一个训练集,然后将该训练集加载为 DataFrame,最后将这个 DataFrame 传递给 AutoML 函数。
以下是详细步骤和代码示例:
- 定义 Feature Lookups:
首先,需要定义 FeatureLookup 对象,指定要从 Feature Store 中查找的特征。
from databricks import feature_store as fe model_feature_lookups = [ fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.oil_10d_lag_ft", lookup_key="date", feature_names="lag10_oil_price" ), fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.store_holidays_ft", lookup_key=["date","store_nbr"] ), fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.stores_ft", lookup_key="store_nbr", feature_names=["cluster","store_type"] ), ]
注意:这里使用了 databricks.feature_store.FeatureLookup 而不是直接的字典。
- 创建训练集:
使用 fe.create_training_set 函数创建训练集。这个函数需要原始数据 DataFrame、Feature Lookups 和目标列名。
training_set = fe.create_training_set( df=raw_data, feature_lookups=model_feature_lookups, label=label_name, )
- 加载 DataFrame:
将训练集加载为 DataFrame,以便 AutoML 可以使用它。
training_df = training_set.load_df()
- 运行 AutoML:
现在,可以将加载的 DataFrame 传递给 AutoML 函数。
automl_data = training_df #.filter("date > '2016-12-31'") #Optional filter summary = databricks.automl.regress(automl_data, target_col=label_name, time_col="date", timeout_minutes=6, exclude_cols=['id'] )
代码示例
以下是完整的代码示例:
from databricks import feature_store as fe model_feature_lookups = [ fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.oil_10d_lag_ft", lookup_key="date", feature_names="lag10_oil_price" ), fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.store_holidays_ft", lookup_key=["date","store_nbr"] ), fe.FeatureLookup( table_name="lakehouse_in_action.favorita_forecasting.stores_ft", lookup_key="store_nbr", feature_names=["cluster","store_type"] ), ] training_set = fe.create_training_set( df=raw_data, feature_lookups=model_feature_lookups, label=label_name, ) training_df = training_set.load_df() automl_data = training_df #.filter("date > '2016-12-31'") #Optional filter summary = databricks.automl.regress(automl_data, target_col=label_name, time_col="date", timeout_minutes=6, exclude_cols=['id'] )
注意事项
- databricks.feature_store.FeatureLookup: 确保使用 databricks.feature_store.FeatureLookup 对象定义 Feature Lookups,而不是简单的字典。
- 特征过滤: 在创建训练集时,可以使用 feature_names 参数来精确指定要包含的特征。
- 数据过滤: 在将 DataFrame 传递给 AutoML 之前,可以对其进行过滤,例如按日期范围过滤。
- 列排除: 可以使用 exclude_cols 参数排除不需要的列。
- 时间限制: timeout_minutes 参数用于限制 AutoML 实验的运行时间。
总结
通过创建训练集并加载 DataFrame,可以更灵活地控制在使用 Databricks AutoML 和 Feature Store 时,哪些特征被包含在训练数据中。这种方法可以避免直接使用 feature_store_lookups 参数可能导致的问题,并允许你更好地利用 Feature Store 的优势。
以上就是在 Databricks AutoML 中指定特征列的详细内容,更多请关注资源网其它相关文章!
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。