BigQuery DataFrames を試す

プレビュー版のサポートについては、bigframes-feedback@google.com までメールでお問い合わせください。

このクイックスタートでは、BigQuery ノートブックBigQuery DataFrames API を使用して、次の分析と機械学習(ML)タスクを行います。

  • bigquery-public-data.ml_datasets.penguins 一般公開データセット上に DataFrame を作成します。
  • ペンギンの平均体重を計算します。
  • 線形回帰モデルを作成する。
  • トレーニング データとして使用するペンギンデータのサブセット上で DataFrame を作成する。
  • トレーニング データをクリーンアップする。
  • モデル パラメータを設定する。
  • モデルを適合する。
  • モデルのスコア付けを行う。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  4. Google Cloud プロジェクトで課金が有効になっていることを確認します

  5. BigQuery API が有効になっていることを確認します。

    API の有効化

    新しいプロジェクトを作成している場合は、BigQuery API が自動的に有効になっています。

必要な権限

ノートブックを作成して実行するには、次の Identity and Access Management(IAM)ロールが必要です。

ノートブックを作成する

BigQuery エディタからノートブックを作成するの手順に沿って、新しいノートブックを作成します。

BigQuery DataFrames を試す

次の手順で BigQuery DataFrame をお試しください。

  1. ノートブックに新しいコードセルを作成します。
  2. 次のコードをコピーして、コードセルに貼り付けます。

    import bigframes.pandas as bpd
    
    # Set BigQuery DataFrames options
    bpd.options.bigquery.project = your_gcp_project_id
    bpd.options.bigquery.location = "us"
    
    # Create a DataFrame from a BigQuery table
    query_or_table = "bigquery-public-data.ml_datasets.penguins"
    df = bpd.read_gbq(query_or_table)
    
    # 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}")
    
    # Create the Linear Regression model
    from bigframes.ml.linear_model import LinearRegression
    
    # Filter down to the data we want to analyze
    adelie_data = df[df.species == "Adelie Penguin (Pygoscelis adeliae)"]
    
    # Drop the columns we don't care about
    adelie_data = adelie_data.drop(columns=["species"])
    
    # Drop rows with nulls to get our training data
    training_data = adelie_data.dropna()
    
    # Pick feature columns and label column
    X = training_data[
        [
            "island",
            "culmen_length_mm",
            "culmen_depth_mm",
            "flipper_length_mm",
            "sex",
        ]
    ]
    y = training_data[["body_mass_g"]]
    
    model = LinearRegression(fit_intercept=False)
    model.fit(X, y)
    model.score(X, y)
    
  3. bpd.options.bigquery.project = your_gcp_project_id 行を変更してプロジェクトを指定します(例: bpd.options.bigquery.project = "myproject")。

  4. コードセルを実行します。

    コードセルは、データセット内のペンギンの平均体重とモデルの評価指標を返します。

クリーンアップ

課金をなくす最も簡単な方法は、チュートリアル用に作成したプロジェクトを削除することです。

プロジェクトを削除するには:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

次のステップ

BigQuery DataFrames ノートブック スタートガイドを試してみる。