Create a linear regression model on the body mass of penguins using the BigQuery DataFrames API.

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Use ML and AI with BigQuery DataFrames](https://docs.cloud.google.com/bigquery/docs/dataframes-ml-ai)

## Code sample

### Python


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    from bigframes.ml.linear_model import LinearRegression
    import bigframes.pandas as bpd

    # Load data from BigQuery
    query_or_table = "bigquery-public-data.ml_datasets.penguins"
    bq_df = bpd.read_gbq(query_or_table)

    # Filter down to the data to the Adelie Penguin species
    adelie_data = bq_df[bq_df.species == "Adelie Penguin (Pygoscelis adeliae)"]

    # Drop the species column
    adelie_data = adelie_data.drop(columns=["species"])

    # Drop rows with nulls to get training data
    training_data = adelie_data.dropna()

    # Specify your feature (or input) columns and the label (or output) column:
    feature_columns = training_data[
        ["island", "culmen_length_mm", "culmen_depth_mm", "flipper_length_mm", "sex"]
    ]
    label_columns = training_data[["body_mass_g"]]

    test_data = adelie_data[adelie_data.body_mass_g.isnull()]

    # Create the linear model
    model = LinearRegression()
    model.fit(feature_columns, label_columns)

    # Score the model
    score = model.score(feature_columns, label_columns)

    # Predict using the model
    result = model.predict(test_data)

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).