建立特徵

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

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

特徵群組必須先與特徵資料來源建立關聯,才能建立特徵。如果特徵群組沒有相關聯的資料來源,您必須更新特徵群組,建立關聯的 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,然後使用使用者憑證設定應用程式預設憑證。

    安裝 Google Cloud CLI。

    如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    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.

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

REST

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

    安裝 Google Cloud CLI。

    如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

在特徵群組中建立特徵

請使用下列範例在特徵群組中建立特徵,並從為特徵群組註冊的 BigQuery 資料來源,關聯包含特徵值的資料欄。

控制台

按照下列操作說明,使用 Google Cloud 控制台將特徵新增至現有特徵群組。

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

    前往 Feature Store 頁面

  2. 在「功能群組」部分中,按一下要新增功能的該功能群組列中的 ,然後按一下「新增功能」

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

  4. 點選「建立」

Python

如要瞭解如何安裝或更新 Python 適用的 Vertex AI SDK,請參閱「安裝 Python 適用的 Vertex AI SDK」。 詳情請參閱 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"
    }
  }
}

後續步驟