התנסות ב-BigQuery DataFrames
במדריך הזה לתחילת העבודה נסביר איך לבצע את משימות הניתוח והלמידה החישובית (ML) הבאות באמצעות BigQuery DataFrames API במחברת BigQuery:
- יוצרים DataFrame על
bigquery-public-data.ml_datasets.penguinsמערך הנתונים הציבורי. - חשבו את המסה הממוצעת של פינגווין.
- יוצרים מודל רגרסיה ליניארית.
- יוצרים DataFrame על קבוצת משנה של נתוני הפינגווינים כדי להשתמש בה כנתוני אימון.
- ניקוי נתוני האימון.
- מגדירים את הפרמטרים של המודל.
- מתאימים את המודל.
- מדרגים את המודל.
לפני שמתחילים
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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.
מוודאים ש-BigQuery API מופעל.
אם יצרתם פרויקט חדש, BigQuery API מופעל באופן אוטומטי.
ההרשאות הנדרשות
כדי ליצור ולהריץ מחברות, אתם צריכים את התפקידים הבאים בניהול זהויות והרשאות גישה (IAM):
- BigQuery User (
roles/bigquery.user) - משתמש בהרצת מחברת (
roles/aiplatform.notebookRuntimeUser) - Code Creator (
roles/dataform.codeCreator)
יצירת Notebook
פועלים לפי ההוראות שבקטע יצירת נוטבוק מתוך עורך BigQuery כדי ליצור נוטבוק חדש.
התנסות ב-BigQuery DataFrames
כדי לנסות את BigQuery DataFrames, פועלים לפי השלבים הבאים:
- יוצרים תא קוד חדש במחברת.
מוסיפים את הקוד הבא לתא הקוד:
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()משנים את השורה
bpd.options.bigquery.project = your_gcp_project_idכדי לציין את מזהה הפרויקט שלכם ב- Cloud de Confiance . לדוגמה:bpd.options.bigquery.project = "myProjectID".מריצים את תא הקוד.
הקוד מחזיר אובייקט
DataFrameעם נתונים על פינגווינים.יוצרים תא קוד חדש ב-Notebook ומוסיפים את הקוד הבא:
# 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}")מריצים את תא הקוד.
הקוד מחשב את המסה הממוצעת של גוף הפינגווינים ומדפיס אותה במסוףCloud de Confiance .
יוצרים תא קוד חדש ב-Notebook ומוסיפים את הקוד הבא:
# 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)מריצים את תא הקוד.
הקוד מחזיר את מדדי ההערכה של המודל.
הסרת המשאבים
הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך.
כדי למחוק את הפרויקט:
- במסוף Cloud de Confiance , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.