התנסות ב-BigQuery DataFrames

‫BigQuery DataFrames מאפשר ניתוח נתונים ולימוד מכונה (ML) ב-Python ב-BigQuery, עם יכולת התאמה לקנה מידה. החישובים מתבצעים ב-BigQuery עם עיבוד בצד השרת, כך שאתם יכולים לנתח ולבנות מודלים של מערכי נתונים גדולים בלי להיות מוגבלים על ידי הזיכרון המקומי או הזיכרון של ה-Notebook. אתם יכולים להשתמש בתחביר שדומה ל-pandas ‏ (bigframes.pandas) ול-BigQuery ML ‏(bigframes.bigquery) בלי לכתוב SQL.

במדריך הזה לומדים איך לבצע את משימות הניתוח וה-ML הבאות באמצעות BigQuery DataFrames API במחברת BigQuery:

  • יוצרים 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 מופעל באופן אוטומטי.

ההרשאות הנדרשות

כדי ליצור ולהריץ מחברות, אתם צריכים את התפקידים הבאים בניהול זהויות והרשאות גישה (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 . לדוגמה: bpd.options.bigquery.project = "myProjectID".

  4. מריצים את תא הקוד.

    הקוד מחזיר אובייקט DataFrame עם נתונים על פינגווינים.

  5. יוצרים תא קוד חדש ב-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}")
    
  6. מריצים את תא הקוד.

    הקוד מחשב את המסה הממוצעת של גוף הפינגווינים ומדפיס אותה במסוףGoogle Cloud .

  7. יוצרים תא קוד חדש ב-Notebook ומוסיפים את הקוד הבא:

    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. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים