Prova BigQuery DataFrames

Utilizza questa guida rapida per eseguire le seguenti attività di analisi e machine learning (ML) utilizzando l'API BigQuery DataFrames in un notebook BigQuery:

  • Crea un DataFrame sul set di dati pubblico bigquery-public-data.ml_datasets.penguins.
  • Calcola la massa corporea media di un pinguino.
  • Crea un modello di regressione lineare.
  • Crea un DataFrame su un sottoinsieme di dati sui pinguini da utilizzare come dati di addestramento.
  • Pulisci i dati di addestramento.
  • Imposta i parametri del modello.
  • Adatta il modello.
  • Assegna un punteggio al modello.

Prima di iniziare

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Verifica che l'API BigQuery sia attivata.

    Abilita l'API

    Se hai creato un nuovo progetto, l'API BigQuery viene attivata automaticamente.

  6. Autorizzazioni obbligatorie

    Per creare ed eseguire i notebook, devi disporre dei seguenti ruoli IAM (Identity and Access Management):

    Crea un notebook

    Segui le istruzioni riportate in Creare un blocco note dall'editor BigQuery per creare un nuovo blocco note.

    Prova BigQuery DataFrames

    Prova BigQuery DataFrames seguendo questi passaggi:

    1. Crea una nuova cella di codice nel notebook.
    2. Copia il seguente codice e incollalo nella cella di codice:

      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. Modifica la riga bpd.options.bigquery.project = your_gcp_project_id per specificare il progetto, ad esempio bpd.options.bigquery.project = "myproject".

    4. Esegui la cella di codice.

      La cella di codice restituisce la massa corporea media dei pinguini nel set di dati, quindi restituisce le metriche di valutazione per il modello.

    Esegui la pulizia

    Il modo più semplice per eliminare la fatturazione è eliminare il progetto che hai creato per il tutorial.

    Per eliminare il progetto:

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

    Passaggi successivi

    Prova il notebook Inizia a utilizzare BigQuery DataFrames.