BigQuery DataFrames testen

In dieser Kurzanleitung werden die folgenden Analyse- und ML-Aufgaben mit der BigQuery DataFrames API in einem BigQuery-Notebook ausgeführt:

  • DataFrame für das öffentliche Dataset bigquery-public-data.ml_datasets.penguins erstellen
  • Durchschnittliche Körpermasse eines Pinguins berechnen
  • Erstellen Sie ein lineares Regressionsmodell.
  • DataFrame für eine Teilmenge der Pinguindaten erstellen, die als Trainingsdaten verwendet werden sollen
  • Trainingsdaten bereinigen
  • Modellparameter festlegen
  • Modell anpassen
  • Bewerten Sie das Modell.

Hinweise

  1. In the Trusted Cloud console, on the project selector page, select or create a Trusted Cloud project.

    Go to project selector

  2. Make sure that billing is enabled for your Trusted Cloud project.

  3. Die BigQuery API muss aktiviert sein.

    API aktivieren

    Wenn Sie ein neues Projekt erstellt haben, wird die BigQuery API automatisch aktiviert.

  4. Erforderliche Berechtigungen

    Zum Erstellen und Ausführen von Notebooks benötigen Sie die folgenden IAM-Rollen (Identity and Access Management):

    Notebook erstellen

    Folgen Sie der Anleitung unter Notebook mit dem BigQuery-Editor erstellen, um ein neues Notebook zu erstellen.

    BigQuery DataFrames testen

    So testen Sie BigQuery DataFrames:

    1. Erstellen Sie im Notebook eine neue Codezelle.
    2. Kopieren Sie den folgenden Code und fügen Sie ihn in die Codezelle ein:

      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()
      
      # 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. Ändern Sie die Zeile bpd.options.bigquery.project = your_gcp_project_id, um Ihr Projekt anzugeben, z. B. in bpd.options.bigquery.project = "myproject".

    4. Führen Sie die Codezelle aus.

      Die Codezelle gibt die durchschnittliche Textmasse der Pinguine im Dataset und dann die Bewertungsmesswerte für das Modell zurück.

    Bereinigen

    Am einfachsten vermeiden Sie weitere Kosten durch Löschen des für die Anleitung erstellten Projekts.

    So löschen Sie das Projekt:

    1. In the Trusted 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.

    Nächste Schritte

    Notebook "Erste Schritte mit BigQuery DataFrames" testen