宣言的な特徴を備えたモデルをトレーニングする
ベータ版
この機能はベータ版であり、以下の地域で利用可能です: us-east-1およびus-west-2 。
このページでは、モデルのトレーニングに宣言型機能を使用する方法について説明します。 宣言型機能の定義に関する情報については、 「宣言型機能」を参照してください。
要件
- 機能は宣言型機能APIを使用して作成する必要があります。宣言型機能を参照してください。
APIメソッド
create_training_set()
宣言型特徴量を作成した後は、次のステップとしてモデル用のトレーニングデータを作成します。これを行うには、ラベル付きデータセットをcreate_training_setに渡します。これにより、各特徴量の値が特定の時点で正確に計算されることが自動的に保証されます。
例えば:
FeatureEngineeringClient.create_training_set(
df: DataFrame, # DataFrame with training data
features: Optional[List[Feature]], # List of Feature objects
label: Union[str, List[str], None], # Label column name(s)
exclude_columns: Optional[List[str]] = None, # Optional: columns to exclude
) -> TrainingSet
TrainingSet.load_dfを呼び出して、元のトレーニング データをポイントインタイムの動的コンピュート機能と結合します。
引数dfは、以下の要件を満たす必要があります。
- フィーチャーデータソースからの
entity_columnsすべて含んでいる必要があります。 - 特徴データソースには
timeseries_column含まれている必要があります。 - ラベル列が含まれている必要があります。
ポイントインタイムの正確性: 機能は、将来のモデル トレーニングへのデータ漏洩を防ぐために、各行のタイムスタンプより前に利用可能なソース データのみを使用してコンピュートされます。 計算処理には、効率化のためにSparkのウィンドウ関数が使用されます。
log_model()
MLflowを使用すると、リネージ追跡や推論中の自動特徴検索のために、特徴メタデータを含むモデルをログに記録できます。
FeatureEngineeringClient.log_model(
model, # Trained model object
artifact_path: str, # Path to store model artifact
flavor: ModuleType, # MLflow flavor module (e.g., mlflow.sklearn)
training_set: TrainingSet, # TrainingSet used for training
registered_model_name: Optional[str], # Optional: register model in Unity Catalog
)
flavorでは、使用するMLflowモデル フレーバーモジュール ( mlflow.sklearnやmlflow.xgboostなど) を指定します。
TrainingSetでログに記録されたモデルは、トレーニングで使用された機能へのリネージを自動的に追跡します。 詳細については、特徴量テーブルでトレーニングするモデルをご覧ください。
score_batch()
自動特徴量検索によるバッチ推論を実行する:
FeatureEngineeringClient.score_batch(
model_uri: str, # URI of logged model
df: DataFrame, # DataFrame with entity keys and timestamps
) -> DataFrame
score_batch モデルとともに保存された特徴メタデータを使用して、推論用の特定時点の正しい特徴を自動的にコンピュートし、トレーニングとの一貫性を確保します。 詳細については、特徴量テーブルでトレーニングするモデルをご覧ください。
ワークフローの例
import mlflow
from databricks.feature_engineering import FeatureEngineeringClient
from sklearn.ensemble import RandomForestClassifier
fe = FeatureEngineeringClient()
# Assume features were created with fe.create_feature()
# labeled_df should have columns "user_id", "transaction_time", and "is_fraud"
# 1. Create training set using declarative features
training_set = fe.create_training_set(
df=labeled_df,
features=features,
label="is_fraud",
)
# 2. Load training data with computed features
training_df = training_set.load_df()
X = training_df.drop("is_fraud").toPandas()
y = training_df.select("is_fraud").toPandas().values.ravel()
# 3. Train model
model = RandomForestClassifier().fit(X, y)
# 4. Log model with feature metadata
with mlflow.start_run():
fe.log_model(
model=model,
artifact_path="fraud_model",
flavor=mlflow.sklearn,
training_set=training_set,
registered_model_name="main.ecommerce.fraud_model",
)
# 5. Batch scoring with automatic feature lookup
# inference_df must contain the same entity_columns and timeseries_column
# used during training. Features are automatically computed.
predictions = fe.score_batch(
model_uri="models:/main.ecommerce.fraud_model/1",
df=inference_df,
)
predictions.display()