Mencoba DataFrames BigQuery

Gunakan panduan memulai ini untuk melakukan analisis dan tugas machine learning (ML) berikut menggunakan BigQuery DataFrames API di notebook BigQuery:

  • Buat DataFrame melalui set data publik bigquery-public-data.ml_datasets.penguins.
  • Hitung massa tubuh rata-rata penguin.
  • Buat model regresi linear.
  • Buat DataFrame di atas subset data penguin untuk digunakan sebagai data pelatihan.
  • Bersihkan data pelatihan.
  • Setel parameter model.
  • Sesuaikan modelnya.
  • Beri skor modelnya.

Sebelum memulai

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  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. Verifikasi bahwa penagihan diaktifkan untuk project Google Cloud Anda.

  5. Pastikan BigQuery API diaktifkan.

    Mengaktifkan API

    Jika Anda membuat project baru, BigQuery API akan otomatis diaktifkan.

Izin yang diperlukan

Untuk membuat dan menjalankan notebook, Anda memerlukan peran Identity and Access Management (IAM) berikut:

Membuat notebook

Ikuti petunjuk di Membuat notebook dari editor BigQuery untuk membuat notebook baru.

Mencoba DataFrames BigQuery

Coba DataFrames BigQuery dengan mengikuti langkah-langkah berikut:

  1. Buat sel kode baru di notebook.
  2. Tambahkan kode berikut ke sel kode:

    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. Ubah baris bpd.options.bigquery.project = your_gcp_project_id untuk menentukan Google Cloud project ID Anda. Contohnya, bpd.options.bigquery.project = "myProjectID".

  4. Jalankan sel kode.

    Kode ini menampilkan objek DataFrame dengan data tentang penguin.

  5. Buat sel kode baru di notebook dan tambahkan kode berikut:

    # 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. Jalankan sel kode.

    Kode menghitung massa tubuh rata-rata penguin dan mencetaknya ke konsolGoogle Cloud .

  7. Buat sel kode baru di notebook dan tambahkan kode berikut:

    # 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)
    
  8. Jalankan sel kode.

    Kode menampilkan metrik evaluasi model.

Pembersihan

Cara termudah untuk menghilangkan penagihan adalah dengan menghapus project yang Anda buat untuk tutorial.

Untuk menghapus project:

  1. Di Konsol Google Cloud , buka halaman Manage resources.

    Buka Kelola resource

  2. Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
  3. Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.

Langkah berikutnya