建立特徵

建立特徵群組並將 BigQuery 資料表或 BigQuery 檢視表與之建立關聯後,即可建立特徵。您可以為特徵群組建立多個特徵,並將每個特徵與 BigQuery 資料來源中的特定欄建立關聯。如要瞭解如何使用 BigQuery,請參閱 BigQuery 說明文件

舉例來說,如果特徵群組 featuregroup1 與 BigQuery 資料表 datasource_1 建立關聯,且該資料表的 fval1fval2 欄包含特徵值,您就可以在 featuregroup1 下方建立特徵 feature_1,並將其與 fval1 欄中的特徵值建立關聯。同樣地,您可以建立另一個名為 feature_2 的特徵,並將其與 fval2 資料欄中的特徵值建立關聯。

使用特徵群組和特徵登錄資料來源有以下優點:

  • 您可以使用多個 BigQuery 資料來源的特定特徵欄,定義線上服務的特徵檢視畫面。

  • 只要加入 feature_timestamp 欄,即可將資料格式化為時間序列。Vertex AI 特徵儲存庫只會提供特徵資料的最新特徵值,並排除歷史值。

  • 在資料目錄中搜尋特徵資源時,您可以發現 BigQuery 來源做為相關特徵資料來源。

  • 您可以設定功能監控,擷取功能統計資料並偵測功能偏移。

事前準備

除非您已驗證 Vertex AI,否則請先完成這項操作。

Select the tab for how you plan to use the samples on this page:

Console

When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

Python

如要在本機開發環境中使用本頁面上的 Python 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。

  1. Install the Google Cloud CLI.

  2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  5. 詳情請參閱 Set up authentication for a local development environment

REST

如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

    After installing the Google Cloud CLI, initialize it by running the following command:

    gcloud init

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

詳情請參閱 Google Cloud 驗證說明文件中的「驗證以使用 REST」。

在特徵群組中建立特徵

請使用以下範例,在特徵群組中建立特徵,並連結包含特徵值的資料欄,這些資料欄來自為特徵群組註冊的 BigQuery 資料來源。

控制台

請按照下列操作說明,使用 Google Cloud 主控台為現有地圖項目群組新增地圖項目。

  1. 在 Google Cloud 控制台的 Vertex AI 專區中,前往「Feature Store」頁面。

    前往「Feature Store」頁面

  2. 在「特徵群組」部分,按一下要新增特徵的對應特徵群組列中的 ,然後點選「新增特徵」

  3. 針對每個功能輸入「功能名稱」,然後在清單中按一下對應的 BigQuery 來源欄名稱。如要新增更多功能,請按一下「新增其他功能」

  4. 按一下 [建立]。

Python 適用的 Vertex AI SDK

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 Vertex AI SDK for Python API 參考說明文件


from google.cloud import aiplatform
from vertexai.resources.preview import feature_store


def create_feature_sample(
    project: str,
    location: str,
    existing_feature_group_id: str,
    feature_id: str,
    version_column_name: str,
):
    aiplatform.init(project=project, location=location)
    feature_group = feature_store.FeatureGroup(existing_feature_group_id)
    feature = feature_group.create_feature(
        name=feature_id, version_column_name=version_column_name
    )
    return feature

  • project:您的專案 ID。
  • location:地圖項目所在的區域,例如 us-central1
  • existing_feature_group_id:您要建立地圖項目的現有地圖項目群組名稱。
  • version_column_name:選用:您要與功能建立關聯的 BigQuery 資料表或檢視畫面中的資料欄。如果未指定此參數,則預設會設為 FEATURE_NAME
  • feature_id:您要建立的新功能名稱

REST

如要建立 Feature 資源,請使用 features.create 方法傳送 POST 要求。

使用任何要求資料之前,請先替換以下項目:

  • LOCATION_ID:地圖項目所在的區域,例如 us-central1
  • PROJECT_ID:您的專案 ID。
  • FEATUREGROUP_NAME:您要建立地圖項目的地圖項目群組名稱。
  • FEATURE_NAME:您要建立的新功能名稱。
  • VERSION_COLUMN_NAME:選用:您要與功能建立關聯的 BigQuery 資料表或檢視畫面中的資料欄。如果未指定此參數,則預設會設為 FEATURE_NAME

HTTP 方法和網址:

POST https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureGroups/FEATUREGROUP_NAME/features?feature_id=FEATURE_NAME

JSON 要求主體:

{
  "version_column_name": "VERSION_COLUMN_NAME"
}

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureGroups/FEATUREGROUP_NAME/features?feature_id=FEATURE_NAME"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/featureGroups/FEATUREGROUP_NAME/features?feature_id=FEATURE_NAME" | Select-Object -Expand Content

您應該會收到如下的 JSON 回應:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/featureGroups/FEATUREGROUP_NAME/features/FEATURE_NAME/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.aiplatform.v1.UpdateFeatureOperationMetadata",
    "genericMetadata": {
      "createTime": "2023-09-18T02:36:22.870679Z",
      "updateTime": "2023-09-18T02:36:22.870679Z"
    }
  }
}

後續步驟