試用 BigQuery DataFrames

BigQuery DataFrames 可將可擴充的 Python 分析和機器學習 (ML) 帶入 BigQuery。運算作業會在 BigQuery 中執行,並採用伺服器端處理方式,因此您可分析及建立大型資料集模型,不受本機或筆記本記憶體限制。您可以使用類似 pandas (bigframes.pandas) 和 BigQuery ML (bigframes.bigquery) 的語法,不必編寫 SQL。

在本快速入門導覽課程中,您將在 BigQuery 筆記本中使用 BigQuery DataFrames API,執行下列分析和 ML 工作:

  • 在公開資料集上建立 DataFrame。bigquery-public-data.ml_datasets.penguins
  • 計算企鵝的平均體重。
  • 清理並準備企鵝資料子集,以供訓練。
  • 使用 bigframes.bigquery.ml.create_model 訓練線性迴歸模型
  • 使用 bigframes.bigquery.ml.evaluate 評估模型。

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  4. 確認專案已啟用計費功能 Google Cloud

  5. 確認已啟用 BigQuery API。

    啟用 API

    如果您建立新專案,系統會自動啟用 BigQuery API。

所需權限

如要建立及執行 Notebook,您需要下列 Identity and Access Management (IAM) 角色:

建立筆記本

按照「從 BigQuery 編輯器建立筆記本」一文中的操作說明,建立新的筆記本。

試用 BigQuery DataFrames

如要試用 BigQuery DataFrames,請按照下列步驟操作:

  1. 在筆記本中建立新的程式碼儲存格。
  2. 在程式碼儲存格中新增下列程式碼:

    import bigframes.pandas as bpd
    
    # Set BigQuery DataFrames options
    # Note: The project option is not required in all environments.
    # On BigQuery Studio, the project ID is automatically detected.
    bpd.options.bigquery.project = your_gcp_project_id
    
    # Use "partial" ordering mode to generate more efficient queries, but the
    # order of the rows in DataFrames may not be deterministic if you have not
    # explictly sorted it. Some operations that depend on the order, such as
    # head() will not function until you explictly order the DataFrame. Set the
    # ordering mode to "strict" (default) for more pandas compatibility.
    bpd.options.bigquery.ordering_mode = "partial"
    
    # Create a DataFrame from a BigQuery table
    query_or_table = "bigquery-public-data.ml_datasets.penguins"
    df = bpd.read_gbq(query_or_table)
    
    # Efficiently preview the results using the .peek() method.
    df.peek()
    
  3. 修改 bpd.options.bigquery.project = your_gcp_project_id 行,指定您的 Google Cloud 專案 ID。例如:bpd.options.bigquery.project = "myProjectID"

  4. 執行程式碼儲存格。

    程式碼會傳回 DataFrame 物件,其中包含企鵝的相關資料。

  5. 在筆記本中建立新的程式碼儲存格,並加入下列程式碼:

    # Use the DataFrame just as you would a pandas DataFrame, but calculations
    # happen in the BigQuery query engine instead of the local system.
    average_body_mass = df["body_mass_g"].mean()
    print(f"average_body_mass: {average_body_mass}")
    
  6. 執行程式碼儲存格。

    這段程式碼會計算企鵝的平均體重,並將結果列印到控制台。Google Cloud

  7. 在筆記本中建立新的程式碼儲存格,並加入下列程式碼:

    import bigframes.bigquery as bbq
    from google.cloud import bigquery
    
    # Ensure a dataset exists to store the model
    client = bigquery.Client(project=bpd.options.bigquery.project)
    client.create_dataset("bq_quickstart", exists_ok=True)
    
    # Filter down to the Adelie Penguin species
    adelie_data = df[df.species == "Adelie Penguin (Pygoscelis adeliae)"]
    
    # Drop the columns that are not needed
    adelie_data = adelie_data.drop(columns=["species"])
    
    # Drop rows with nulls to get the training data
    training_data = adelie_data.dropna()
    
    # Train a linear regression model
    model_name = f"{bpd.options.bigquery.project}.bq_quickstart.penguin_weight"
    model_metadata = bbq.ml.create_model(
        model_name,
        replace=True,
        options={"model_type": "LINEAR_REG"},
        training_data=training_data.rename(columns={"body_mass_g": "label"}),
    )
    
    # Evaluate the model
    evaluation = bbq.ml.evaluate(model_name)
    print(evaluation)
    
  8. 執行程式碼儲存格。

    這段程式碼會直接在 BigQuery 中訓練線性迴歸模型,並傳回模型的評估指標。

清除所用資源

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

刪除專案的方法如下:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

後續步驟