Skip to content

レシピ

クリーンアップ、準備、およびラングリングによってデータを目的の形にするため、DataRobotにはデータ準備用の再利用可能なレシピが用意されています。 各レシピはブループリントのように機能します。1つ以上のデータセットまたはデータソースを入力として受け取り、データのフィルター、修正、結合、または変換を行う一連の操作を適用します。 その後、そのレシピを使用して、すぐに利用可能な状態のデータセットを作成できます。 レシピによってデータ準備ワークフローを迅速に反復でき、簡単な操作APIを通じて再利用が可能です。

レシピの用語

レシピには以下の用語が使われています。

  • レシピ:再利用可能なブループリント。1つ以上のデータ入力を変換する操作を適用することによって、新しいデータセットを作成する方法を示します。
  • レシピダイアレクト:データラングリング操作でレシピ入力を扱う際に使用するダイアレクト。 たとえば、Snowflakeのデータアセットを扱う際には、Snowflakeダイアレクトを使用します。
  • 入力:レシピにデータを提供するデータセットまたはデータソース。 レシピには複数の入力が可能です。 レシピの入力は、すべてのデータセットであるか、同じデータストアを指すデータソースのすべてのテーブルである必要があります。
  • プライマリー入力:レシピのベースとして使用される入力。 レシピで操作が適用されない場合、プライマリー入力と同一のデータセットがレシピから出力されます。 レシピ内のプライマリー入力は1つだけです。
  • セカンダリー入力:レシピへの追加入力。 レシピには複数のセカンダリー入力が可能です。 セカンダリー入力からのデータは、結合または他の同様の操作によってレシピに導入する必要があります。
  • レシピプレビュー:レシピのデータのサンプルビュー。レシピ内の操作を入力データに適用して計算します。 レシピのプレビューに表示されるデータは、通常、そのレシピの完全に変換されたデータのサンプルです。
  • サンプリング:レシピのプレビューを計算する際に、レシピのプライマリー入力から読み取られる行数を変更する設定。
  • ダウンサンプリング:レシピによってパブリッシュされたデータセットに書き込まれる行数を変更する設定。
  • 操作:レシピが入力データをどのように処理するかを変更する方法。
  • ラングリング操作:レシピのデータに適用する変換。 レシピでは、複数のラングリング操作を積み重ねて、入力データを変換することができます。
  • ダウンサンプリング操作:パブリッシュ時にデータセットに書き込むレシピの行数の変更。 レシピでは、オプションで単一のダウンサンプリング操作を使用できます。
  • サンプリング操作:レシピのプライマリーデータ入力から読み取る行数の変更。 レシピでは、オプションでプライマリー入力に単一のサンプリング操作を設定できます。
  • パブリッシュ:レシピの操作を入力データに適用した結果を含む、新しいデータセットを作成するアクション。

以下のレシピを用いて作成、反復、パブリッシュを行うための推奨ワークフローを確認します。

  1. datarobot.Recipeを作成し、datarobot.Datasetまたは datarobot.DataSourceのデータを操作します。 レシピはdatarobot.Usecaseに属します。
  2. メタデータ、設定、入力、操作、またはダウンサンプリングを更新することによって、レシピを修正します。
  3. レシピのプレビューをリクエストして、レシピのデータを確認します。 結果に問題がある場合は、手順2に戻ります。
  4. レシピをパブリッシュして、レシピ内の変換に従って構築された新しいdatarobot.Datasetを作成します。

レシピの作成

レシピを作成するには2つの方法があります。 JDBCデータソースのデータセットまたはテーブルを使用できます。 それらがレシピのプライマリー入力となります。 各レシピはユースケースに属するため、datarobot.Usecaseも必要です。 データセットまたはデータソースのソースに最も適したDataWranglingDialectを選択します。

データセットからレシピを作成する

既存のdatarobot.Datasetからレシピを作成するには、Recipe.from_datasetメソッドを使用します。

>>> import datarobot as dr
>>> from datarobot.enums import DataWranglingDialect, RecipeType
>>> from datarobot.models.recipe_operation import RandomSamplingOperation
>>>
>>> # Get your use case and dataset
>>> my_use_case = dr.UseCase.list(search_params={"search": "My Use Case"})[0]
>>> dataset = dr.Dataset.get('5f43a1b2c9e77f0001e6f123')
>>>
>>> # Create a recipe from the dataset
>>> recipe = dr.Recipe.from_dataset(
...     use_case=my_use_case,
...     dataset=dataset,
...     dialect=DataWranglingDialect.SPARK,
...     recipe_type=RecipeType.WRANGLING,
...     sampling=RandomSamplingOperation(rows=500)
... ) 

JDBCテーブルからレシピを作成する

接続されたデータソースのテーブルから直接レシピを作成するには、Recipe.from_data_storeメソッドを使用します。

>>> import datarobot as dr
>>> from datarobot.enums import DataWranglingDataSourceTypes, DataWranglingDialect, RecipeType
>>> from datarobot.models.recipe import DataSourceInput
>>> from datarobot.models.recipe_operation import LimitSamplingOperation
>>>
>>> # Configure your data source input
>>> data_source_input = DataSourceInput(
...     canonical_name='Sales_Data_Connection', # data connection name
...     table='sales_transactions',
...     schema='PUBLIC',
...     sampling=LimitSamplingOperation(rows=1000)
... )
>>>
>>> # Get your use case and data store
>>> my_use_case = dr.UseCase.list(search_params={"search": "Sales Analysis"})[0]
>>> data_store = dr.DataStore.get('2g33a1b2c9e88f0001e6f657')
>>>
>>> # Create recipe from data source
>>> recipe = dr.Recipe.from_data_store(
...     use_case=my_use_case,
...     data_store=data_store,
...     data_source_type=DataWranglingDataSourceTypes.JDBC,
...     dialect=DataWranglingDialect.POSTGRES,
...     data_source_inputs=[data_source_input],
...     recipe_type=RecipeType.WRANGLING
... ) 

レシピの取得

IDで特定のレシピを取得することも、すべてのレシピのリストを取得し、必要に応じてリストをフィルターすることもできます。

>>> import datarobot as dr
>>>
>>> # Get a specific recipe by ID
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # List all recipes
>>> all_recipes = dr.Recipe.list()
>>>
>>> # Filter recipes. Use any number of params to filter.
>>> filtered_recipes = dr.Recipe.list(
...     search="My Recipe Name",
...     dialect=dr.enums.DataWranglingDialect.SPARK,
...     status="draft",
...     recipe_type=dr.enums.RecipeType.WRANGLING,
...     order_by="-updatedAt",  # Most recently updated first
...     created_by_username="data_scientist_user"
... ) 

レシピに関する情報を取得する

レシピオブジェクトには、以下に示すように、クエリー可能なレシピに関する基本情報が含まれています。

>>> import datarobot as dr
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> recipe.id
u'690bbf77aa31530d8287ae5f'
>>> recipe.name
u"Customer Segmentation Dataset Recipe" 

また、入力と操作のリスト、ダウンサンプリングの設定、一般的なレシピ設定も取得できます。

>>> import datarobot as dr
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>> # Access inputs, operations, downsampling and settings
>>> inputs = recipe.inputs
>>> primary_input = inputs[0] # First input is the primary input
>>> secondary_inputs = inputs[1:] # All others in the list are secondary inputs
>>>
>>> operations = recipe.operations
>>> downsampling_operation = recipe.downsampling
>>> settings = recipe.settings 

レシピのメタデータフィールドを更新する

レシピのメタデータフィールド(名前、説明など)は、以下のようにRecipe.updateで更新できます。

>>> import datarobot as dr
>>>
>>> # Retrieve an existing recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Update metadata fields
>>> recipe.update(
...     name="Customer Segmentation Dataset Recipe",
...     description="Recipe to create customer segmentation dataset."
... ) 

レシピ入力の更新

レシピの入力リストは、以下に示すようにRecipe.updateメソッドで更新できます。 入力データのリストを更新することで、変換のためにレシピに入力されるデータを変更します。 リスト内の最初の入力データがレシピのプライマリー入力となり、残りはセカンダリー入力となります。

レシピ入力に関する注意事項

Recipe.updateメソッドは、既存の入力をすべて置き換えます。 入力を追加する場合は、レシピが壊れないように、常に既存のプライマリー入力を含めます。

セカンダリー入力のデータは、プライマリー入力のデータと何らかの方法で結合または組み合わされない限り、レシピプレビューに表示されません。

レシピの入力は、すべてのデータセットであるか、同じデータストアを指すデータソースのすべてのテーブルである必要があります。

>>> import datarobot as dr
>>> from datarobot.models.recipe import RecipeDatasetInput, JDBCTableDataSourceInput
>>>
>>> # Get the recipe and additional datasets
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>> secondary_dataset = dr.Dataset.get('5f43a1b2c9e77f0001e6f456')
>>>
>>> # Add a secondary dataset input if the primary input is also a dataset
>>> recipe.update(
...     inputs=[
...         recipe.inputs[0],  # Keep the original primary input
...         RecipeDatasetInput.from_dataset(
...             dataset=secondary_dataset,
...             alias='customers_data'
...         )
...     ]
... )
>>>
>>> # You can also add data from a table in a data store
>>> data_store = dr.DataStore.get('5e1b4f8f2a3c4d5e6f7g8h9i')
>>> data_source = DataSource.create(
...     data_source_type="jdbc",
...     canonical_name="My Snowflake connection",
...     params=dr.DataSourceParameters(
...         data_store_id=data_store.id,
...         schema="PUBLIC",
...         table="stock_prices"
...     )
... )
>>> table = data_source.create_dataset()
>>> # Add data from a table in a data store if the primary input is also a table from the same data store
>>> recipe.update(
...     inputs=[
...         recipe.inputs[0],  # Primary input
...         JDBCTableDataSourceInput(
...             input_type=RecipeInputType.DATASOURCE,
...             data_source_id=data_source.id,
...             data_store_id=data_store.id,
...             dataset_id=table.id,
...             sampling=LimitSamplingOperation(rows=250),
...             alias='my_table_alias'
...         )
...     ]
... ) 

プライマリー入力のサンプリングを更新する

レシピ操作を反復処理する際、処理対象となる行数を制限することを選択できます。 レシピのプライマリー入力に対してサンプリング操作を指定することで、レシピプレビューの計算を高速化できます。 サンプリング操作では、データセットへのパブリッシュ時に行数は変更されません。 サンプリング操作は、プライマリー入力に対してのみ指定する必要があります。 セカンダリー入力は常にプライマリー入力と結合または統合されるため、レシピプレビューに表示する行数を決定するのはプライマリー入力のみとなります。

>>> from datarobot.models.recipe_operation import LimitSamplingOperation
>>>
>>> # Configure sampling for an input
>>> my_dataset = dr.Dataset.get('5f43a1b2c9e77f0001e6f456')
>>> dataset_input = RecipeDatasetInput.from_dataset(
...     dataset=my_dataset,
...     alias='sampled_data',
...     sampling=LimitSamplingOperation(rows=100)
... )
>>> # Update recipe with sampled input
>>> recipe.update(inputs=[dataset_input]) 

レシピのラングリング操作を更新する

ラングリング操作はレシピの構成要素であり、データに適用される変換を定義します。 操作は順次処理されます。ある操作の出力が次の操作の入力になり、変換パイプラインが作成されます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import *
>>> from datarobot.enums import FilterOperationFunctions, AggregationFunctions
>>>
>>> # Get your recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create a series of operations
>>> operations = [
...     # Filter rows where age > 18
...     FilterOperation(
...         conditions=[
...             FilterCondition(
...                 column="age",
...                 function=FilterOperationFunctions.GREATER_THAN,
...                 function_arguments=[18]
...             )
...         ],
...         keep_rows=True
...     ),
...     # Then create new column with full name
...     ComputeNewOperation(
...         expression="CONCAT(first_name, " ", last_name)",
...         new_feature_name="full_name"
...     ),
...     # Then group by department and calculate average salary
...     AggregationOperation(
...         aggregations=[
...             AggregateFeature(
...                 feature="salary",
...                 functions=[AggregationFunctions.AVERAGE]
...             )
...         ],
...         group_by_columns=["department"]
...     ),
... ]
>>>
>>> # Update the recipe with new list of wrangling operations
>>> recipe.update(operations=operations) 

以下に、利用可能なデータラングリング操作のリストとその変換例を示します。

ラグ操作

LagsOperationは、日時の順番に基づいて、列の時間差バージョンを作成します。 この操作では、指定のラグごとに新しい列が作成されます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import LagsOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create lags for 1 and 2 days for stock price analysis
>>> lags_op = LagsOperation(
...     column="stock_price",
...     orders=[1, 2],
...     datetime_partition_column="trade_date",
...     multiseries_id_column="ticker_symbol"  # For multiseries data (multiple stocks in this example)
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[lags_op]) 

プライマリー入力データセット:

ticker_symbol trade_date stock_price
AAPL 2024-01-01 150.00
AAPL 2024-01-02 152.50
AAPL 2024-01-03 149.75
AAPL 2024-01-04 153.20
MSFT 2024-01-01 380.00
MSFT 2024-01-02 385.75
MSFT 2024-01-03 382.30
MSFT 2024-01-04 388.90

レシピのプレビュー:

ticker_symbol trade_date stock_price stock_price (1st lag) stock_price (2nd lag)
AAPL 2024-01-01 150.00
AAPL 2024-01-02 152.50 150.00
AAPL 2024-01-03 149.75 152.50 150.00
AAPL 2024-01-04 153.20 149.75 152.50
MSFT 2024-01-01 380.00
MSFT 2024-01-02 385.75 380.00
MSFT 2024-01-03 382.30 385.75 380.00
MSFT 2024-01-04 388.90 382.30 385.75

ウィンドウのカテゴリー統計操作

WindowCategoricalStatsOperationは、ローリングウィンドウのカテゴリー統計を計算し、統計方法ごとに新しい列を作成します。 これにより、時間の経過に伴うカテゴリーデータの傾向を追跡できます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import WindowCategoricalStatsOperation
>>> from datarobot.enums import CategoricalStatsMethods
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Compute most frequent purchase in last 3 purchases
>>> window_cat_op = WindowCategoricalStatsOperation(
...     column="product_category",
...     window_size=3,  # Last 3 purchases
...     methods=[CategoricalStatsMethods.MOST_FREQUENT],
...     datetime_partition_column="purchase_date",
...     multiseries_id_column="customer_id"
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[window_cat_op]) 

プライマリー入力データセット:

customer_id purchase_date product_category
CUST001 2024-01-01 Electronics
CUST001 2024-01-02 Clothing
CUST001 2024-01-03 Electronics
CUST001 2024-01-04 Electronics
CUST002 2024-01-01 Books
CUST002 2024-01-02 Books
CUST002 2024-01-03 Electronics
CUST002 2024-01-04 Books

レシピのプレビュー:

customer_id purchase_date product_category product_category (3 rows most frequent)
CUST001 2024-01-01 Electronics Electronics
CUST001 2024-01-02 Clothing Electronics
CUST001 2024-01-03 Electronics Electronics
CUST001 2024-01-04 Electronics Electronics
CUST002 2024-01-01 Books Books
CUST002 2024-01-02 Books Books
CUST002 2024-01-03 Electronics Books
CUST002 2024-01-04 Books Books

ウィンドウの数値統計操作

WindowNumericStatsOperationは、ローリングウィンドウの数値統計を計算し、統計方法ごとに新しい列を作成します。 この操作は、時間の経過に伴う移動平均、最大値、最小値、およびその他の統計を計算する場合に便利です。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import WindowNumericStatsOperation
>>> from datarobot.enums import NumericStatsMethods
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Track max and average of last 3 transactions
>>> window_num_op = WindowNumericStatsOperation(
...     column="sales_amount",
...     window_size=3,  # Last 3 transactions
...     methods=[NumericStatsMethods.AVG, NumericStatsMethods.MAX],
...     datetime_partition_column="transaction_date",
...     multiseries_id_column="store_id"
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[window_num_op]) 

プライマリー入力データセット:

store_id transaction_date sales_amount
STORE01 2024-01-01 100.00
STORE01 2024-01-02 150.00
STORE01 2024-01-03 120.00
STORE01 2024-01-04 200.00
STORE02 2024-01-01 80.00
STORE02 2024-01-02 90.00
STORE02 2024-01-03 110.00
STORE02 2024-01-04 95.00

レシピのプレビュー:

store_id transaction_date sales_amount sales_amount (3 rows avg) sales_amount (3 rows max)
STORE01 2024-01-01 100.00 100.00 100.00
STORE01 2024-01-02 150.00 125.00 150.00
STORE01 2024-01-03 120.00 123.33 150.00
STORE01 2024-01-04 200.00 156.67 200.00
STORE02 2024-01-01 80.00 80.00 80.00
STORE02 2024-01-02 90.00 85.00 90.00
STORE02 2024-01-03 110.00 93.33 110.00
STORE02 2024-01-04 95.00 98.33 110.00

時系列の操作

TimeSeriesOperationは、予測ポイント、距離、さまざまな時間認識特徴量を作成することで、時系列モデリングに対応したデータセットを生成します。 タスクプランを定義することで、ラグやローリング統計のような複数の時系列変換が実行され、特徴量としてレシピデータに追加されます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import TimeSeriesOperation, TaskPlanElement, Lags
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Define task plan for feature engineering
>>> task_plan = [
...     TaskPlanElement(
...         column="sales_amount",
...         task_list=[Lags(orders=[1])]
...     )
... ]
>>>
>>> # Create time series operation
>>> time_series_op = TimeSeriesOperation(
...     target_column="sales_amount",
...     datetime_partition_column="sale_date",
...     forecast_distances=[1],  # Predict 1 period ahead
...     task_plan=task_plan
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[time_series_op]) 

プライマリー入力データセット:

store_id sale_date sales_amount
STORE01 2024-01-01 1000
STORE01 2024-01-02 1200
STORE01 2024-01-03 1100
STORE01 2024-01-04 1300

レシピのプレビュー:

store_id (actual) sale_date (actual) sales_amount (actual) 予測ポイント 予測距離 sales_amount (1st lag) sales_amount (naive 1 row seasonal value)
STORE01 2024-01-02 1200 2024-01-01 1 1000 1000
STORE01 2024-01-03 1100 2024-01-02 1 1200 1200
STORE01 2024-01-04 1300 2024-01-03 1 1100 1100

新規計算操作

ComputeNewOperationは、SQL式を使用して新しい特徴量を作成し、既存の列から計算フィールドを派生させることができます。 この操作は、カスタムビジネスロジック、数学的変換、および特徴量の組み合わせを作成する場合に便利です。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import ComputeNewOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create compute new operation to compute total cost, factoring in a discount %
>>> compute_op = ComputeNewOperation(
...     expression="ROUND(quantity * unit_price * (1 - discount), 2)",
...     new_feature_name="total_cost"
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[compute_op]) 

プライマリー入力データセット:

order_id quantity unit_price discount
ORD001 3 25.50 0.10
ORD002 1 15.00 0.00
ORD003 2 40.00 0.15
ORD004 5 12.25 0.05

レシピのプレビュー:

order_id quantity unit_price discount total_cost
ORD001 3 25.50 0.10 68.85
ORD002 1 15.00 0.00 15.00
ORD003 2 40.00 0.15 68.00
ORD004 5 12.25 0.05 58.19

列名の変更操作

RenameColumnsOperationは1つ以上の列の名前を変更します。 この操作は、多くの場合、列名を標準化したり、列名をよりわかりやすくしたり、特定の下流工程で列名の一貫性を確保したりするのに役立ちます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import RenameColumnsOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Rename customer id, product name and quantity columns
>>> rename_op = RenameColumnsOperation(
...     column_mappings={
...         'cust_id': 'customer_id',
...         'prod_name': 'product_name',
...         'qty': 'quantity'
...     }
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[rename_op]) 

プライマリー入力データセット:

cust_id prod_name qty price
C001 Widget A 3 25.99
C002 Gadget B 1 15.50
C001 Tool C 2 45.00
C003 Widget A 5 25.99

レシピのプレビュー:

customer_id product_name quantity price
C001 Widget A 3 25.99
C002 Gadget B 1 15.50
C001 Tool C 2 45.00
C003 Widget A 5 25.99

フィルター操作

FilterOperationは、1つ以上のフィルター条件に基づいて行を削除または保持します。 複雑なフィルタールールを作成するには、AND/ORロジックを使用して複数の条件を適用します。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import FilterOperation, FilterCondition
>>> from datarobot.enums import FilterOperationFunctions
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create filter conditions to keep customers over 18 with active status
>>> conditions = [
...     FilterCondition(
...         column="age",
...         function=FilterOperationFunctions.GREATER_THAN_OR_EQUALS,
...         function_arguments=[18]
...     ),
...     FilterCondition(
...         column="status",
...         function=FilterOperationFunctions.EQUALS,
...         function_arguments=["active"]
...     )
... ]
>>>
>>> # Create filter operation
>>> filter_op = FilterOperation(
...     conditions=conditions,
...     keep_rows=True,  # Keep matching rows
...     operator="and"   # Both conditions must be true
... )
>>>
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[filter_op]) 

プライマリー入力データセット:

customer_id age ステータス purchase_amount
C001 25 有効 150.00
C002 17 有効 75.00
C003 30 inactive 200.00
C004 22 有効 95.00

レシピのプレビュー:

customer_id age ステータス purchase_amount
C001 25 有効 150.00
C004 22 有効 95.00

列の削除操作

DropColumnsOperationは1つ以上の列を削除します。 この操作は、不要なフィールドや機密情報、あるいは下流工程で使用されない列を削除するのに役立ちます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import DropColumnsOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create operation to drop 2 extra columns
>>> drop_op = DropColumnsOperation(
...     columns=['internal_notes', 'legacy_id']
... )
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[drop_op]) 

プライマリー入力データセット:

customer_id メールアドレス internal_notes legacy_id
C001 John Doe john@email.com VIP customer L001
C002 Jane Doe jane@email.com New customer L002
C003 Bob Lee bob@email.com Frequent buyer L003

レシピのプレビュー:

customer_id メールアドレス
C001 John Doe john@email.com
C002 Jane Doe jane@email.com
C003 Bob Lee bob@email.com

行の重複除外操作

DedupeRowsOperationは重複する行を削除し、一意な値の組み合わせのみを保持します。 この操作では、すべての列の値が参照されます。 この操作は、冗長なレコードを削除してデータをクリーンアップするのに役立ちます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import DedupeRowsOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Create dedupe rows operation
>>> dedupe_op = DedupeRowsOperation()
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[dedupe_op]) 

プライマリー入力データセット:

customer_id product quantity price
C001 Widget A 2 25.99
C002 Gadget B 1 15.50
C001 Widget A 2 25.99
C003 Tool C 3 45.00
C002 Gadget B 1 15.50

レシピのプレビュー:

customer_id product quantity price
C001 Widget A 2 25.99
C002 Gadget B 1 15.50
C003 Tool C 3 45.00

検索と置換の操作

FindAndReplaceOperationは列内の特定の文字列やパターンを検索し、新しい値に置き換えます。 この操作では、完全一致、部分一致、または正規表現をサポートしており、柔軟なテキスト操作が可能です。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import FindAndReplaceOperation
>>> from datarobot.enums import FindAndReplaceMatchMode
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Replace instances of 'In Progress' (case insensitive) with 'Active'
>>> replace_op = FindAndReplaceOperation(
...     column="status",
...     find="In Progress",
...     replace_with="Active",
...     match_mode=FindAndReplaceMatchMode.EXACT,
...     is_case_sensitive=False
... )
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[replace_op]) 

プライマリー入力データセット:

order_id ステータス customer_name
ORD001 進行中 John Smith
ORD002 完了 Jane Doe
ORD003 進行中 Bob Johnson
ORD004 キャンセル済み Alice Brown

レシピのプレビュー:

order_id ステータス customer_name
ORD001 アクティブ John Smith
ORD002 完了 Jane Doe
ORD003 アクティブ Bob Johnson
ORD004 キャンセル済み Alice Brown

集計操作

AggregationOperationは、指定された列でデータをグループ化し、合計、平均、カウントなどのサマリー特徴量を計算します。 この操作は、分析サマリーの作成や派生特徴量の計算に役立ちます。 集計のために選択された各特徴量に適用される集計関数ごとに、新しい列が作成されます。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import AggregationOperation
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Group by customer id and product category
>>> # Compute the sum of orders and customer's average order amount
>>> agg_op = AggregationOperation(
...     group_by_columns=['customer_id', 'product_category'],
...     aggregations=[
...         AggregateFeature(
...             feature="order_amount",
...             functions=[AggregationFunctions.SUM, AggregationFunctions.AVERAGE]
...         )
...     ]
... )
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[agg_op]) 

プライマリー入力データセット:

customer_id product_category order_id order_amount
C001 Electronics ORD001 150.00
C001 Electronics ORD002 200.00
C001 Clothing ORD003 75.00
C002 Electronics ORD004 300.00
C002 Electronics ORD005 125.00

レシピのプレビュー:

customer_id product_category order_amount_sum order_amount_avg
C001 Electronics 350.00 175.00
C001 Clothing 75.00 75.00
C002 Electronics 425.00 212.50

結合操作

JoinOperationでは、レシピの現在のデータに追加のデータ入力を結合できます。 この操作により、セカンダリーデータセットからの追加情報でプライマリーデータセットを強化できます。 結合操作では、結合条件として1つ以上の等価述語のみがサポートされます。

備考

追加のデータ入力は、結合の右辺として扱われます。

>>> import datarobot as dr
>>> from datarobot.models.recipe import RecipeDatasetInput
>>> from datarobot.models.recipe_operation import JoinOperation
>>> from datarobot.enums import JoinType
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Get the secondary dataset and add it as an input
>>> dataset = dr.Dataset.get('5f43a1b2c9e77f0001e6f123')
>>> recipe.update(
...     inputs=[
...         recipe.inputs[0],  # Keep the original primary input
...         RecipeDatasetInput.from_dataset(
...             dataset=dataset,
...             alias='customers'
...         )
...     ]
... )
>>>
>>> # Join secondary dataset on customer id
>>> # Right dataset in join will always be the new dataset
>>> join_op = JoinOperation.join_dataset(
...     dataset=dataset,
...     join_type=JoinTypes.INNER,
...     right_prefix='cust_',
...     left_keys=['customer_id'],
...     right_keys=['id']
... )
>>> # Apply the operation to the recipe
>>> recipe.update(operations=[join_op]) 

Primary input dataset (orders)

order_id customer_id amount
ORD001 C001 150.00
ORD002 C002 200.00
ORD003 C001 75.00

Secondary input dataset (customers)

id city
C001 John Smith New York
C002 Jane Doe Los Angeles
C003 Bob Lee Chicago

レシピのプレビュー:

order_id customer_id amount cust_id cust_name cust_city
ORD001 C001 150.00 C001 John Smith New York
ORD002 C002 200.00 C002 Jane Doe Los Angeles
ORD003 C001 75.00 C001 John Smith New York

レシピのSQL変換を直接設定する

高度なユースケースでは、SQL式を使用してレシピの変換を設定できます。 これにより、標準的なラングリング操作では実現できない複雑な処理に最大限の柔軟性が提供されます。

重要:SQLを設定すると、レシピタイプがSQLに直接変更され、既存のラングリング操作が省略されます。

>>> import datarobot as dr
>>>
>>> # Get your recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Define your SQL transformation
>>> sql_query = "MY SQL EXPRESSION HERE"
>>> # Update the recipe with SQL
>>> recipe.update(sql=sql_query) 

レシピデータのプレビュー

レシピをパブリッシュする前に、変換されたデータをプレビューして変換処理を検証し、期待どおりの結果が生成されることをRecipe.get_previewで確認できます。

>>> import datarobot as dr
>>>
>>> # Get your recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Generate a preview of the transformed data
>>> preview = recipe.get_preview()
>>> # View preview data as a DataFrame
>>> preview.df 

レシピのダウンサンプリングを更新する

ダウンサンプリングでは、レシピによってパブリッシュされるデータセットのサイズが変更されます。これにより、大規模なデータセットでのパフォーマンスが向上し、開発とテストの速度が向上します。 特に数百万行のデータを扱う場合、データセットへのパブリッシュ時には代表的なサンプルで十分であるため、これは非常に有用です。 ダウンサンプリングはレシピプレビューの行数には影響しません。 レシピのダウンサンプリングをダウンサンプリング操作で設定します。

>>> import datarobot as dr
>>> from datarobot.models.recipe_operation import RandomDownsamplingOperation
>>>
>>> # Get your recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Configure random downsampling to 50,000 rows
>>> downsampling = RandomDownsamplingOperation(max_rows=50_000)
>>> # Apply downsampling to the recipe
>>> recipe.update(downsampling=downsampling)
>>> # Disable downsampling
>>> recipe.update(downsampling=None) 

レシピをデータセットにパブリッシュする

レシピが完成したら、Recipe.publish_to_datasetでレシピをパブリッシュし、変換されたデータを含むデータセットを作成できます。

>>> import datarobot as dr
>>>
>>> # Get your recipe
>>> recipe = dr.Recipe.get('690bbf77aa31530d8287ae5f')
>>>
>>> # Publish recipe to create a new dataset
>>> dataset = recipe.publish_to_dataset(
...     name="Customer Segmentation Data",
...     do_snapshot=True
... )
>>>
>>> # Publish and attach to an existing use case
>>> use_case = dr.UseCase.get('5e1b4f8f2a3c4d5e6f7g8h9i')
>>> dataset_with_use_case = recipe.publish_to_dataset(
...     name="Advanced Customer Analytics",
...     use_cases=use_case
... )