Mencoba BigQuery DataFrames
BigQuery DataFrames menghadirkan analisis Python dan machine learning (ML) yang skalabel ke BigQuery. Komputasi dijalankan di BigQuery dengan pemrosesan sisi server, yang memungkinkan Anda menganalisis dan memodelkan set data besar tanpa dibatasi oleh memori lokal atau notebook. Anda dapat menggunakan sintaksis yang mirip dengan pandas (bigframes.pandas) dan BigQuery ML (bigframes.bigquery) tanpa menulis SQL.
Gunakan panduan memulai ini untuk melakukan analisis dan tugas 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.
- Bersihkan dan siapkan subset data penguin untuk pelatihan.
- Latih
model regresi linear
menggunakan
bigframes.bigquery.ml.create_model. - Evaluasi
model menggunakan
bigframes.bigquery.ml.evaluate.
Sebelum memulai
- Sign in to your Google Cloud account. 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.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Pastikan penagihan diaktifkan untuk Google Cloud project Anda.
Pastikan BigQuery API diaktifkan.
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:
- Pengguna BigQuery (
roles/bigquery.user) - Pengguna Runtime Notebook (
roles/aiplatform.notebookRuntimeUser) - Pembuat Kode (
roles/dataform.codeCreator)
Membuat notebook
Ikuti petunjuk di Membuat notebook dari editor BigQuery untuk membuat notebook baru.
Mencoba BigQuery DataFrames
Coba BigQuery DataFrames dengan mengikuti langkah-langkah berikut:
- Buat sel kode baru di notebook.
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()Ubah baris
bpd.options.bigquery.project = your_gcp_project_iduntuk menentukan project ID Anda. Google Cloud Contohnya,bpd.options.bigquery.project = "myProjectID".Jalankan sel kode.
Kode ini akan menampilkan objek
DataFramedengan data tentang penguin.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}")Jalankan sel kode.
Kode ini menghitung massa tubuh rata-rata penguin dan mencetaknya ke Google Cloud konsol.
Buat sel kode baru di notebook dan tambahkan kode berikut:
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)Jalankan sel kode.
Kode ini melatih model regresi linear langsung di BigQuery dan menampilkan metrik evaluasi model.
Pembersihan
Cara termudah untuk menghilangkan penagihan adalah dengan menghapus project yang Anda buat untuk tutorial.
Untuk menghapus project:
- Di Google Cloud konsol, buka halaman Manage resources.
- Pada daftar project, pilih project yang Anda ingin Anda hapus, lalu klik Delete.
- Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.
Langkah berikutnya
- Lanjutkan mempelajari BigQuery DataFrames.
- Pelajari cara me mvisualisasikan grafik menggunakan BigQuery DataFrames.
- Pelajari cara me nggunakan notebook BigQuery DataFrames.