노트북에서 쿼리 결과 탐색

BigQuery에서 Colab Enterprise 노트북을 사용하여 BigQuery 쿼리 결과를 탐색할 수 있습니다.

이 튜토리얼에서는 BigQuery 공개 데이터 세트에서 데이터를 쿼리하고 노트북에서 쿼리 결과를 탐색합니다.

필수 권한

노트북을 만들고 실행하려면 다음 Identity and Access Management(IAM) 역할이 필요합니다.

노트북에서 쿼리 결과 열기

SQL 쿼리를 실행한 후 노트북을 사용하여 데이터를 탐색할 수 있습니다. 이 방법은 데이터를 사용하기 전에 BigQuery에서 데이터를 수정하려는 경우나 테이블에서 필드 하위 집합만 필요한 경우에 유용합니다.

  1. Google Cloud 콘솔에서 BigQuery 페이지로 이동합니다.

    BigQuery로 이동

  2. 검색할 유형 필드에 bigquery-public-data를 입력합니다.

    프로젝트가 표시되지 않으면 검색창에 bigquery를 입력한 후 모든 프로젝트 검색을 클릭하여 검색 문자열과 기존 프로젝트를 일치시킵니다.

  3. bigquery-public-data > ml_datasets > penguins를 선택합니다.

  4. penguins 테이블의 경우 작업 보기를 클릭한 후 쿼리를 클릭합니다.

  5. 다음 예시와 같이 읽도록 생성된 쿼리에 필드 선택에 필요한 별표(*)를 추가합니다.

    SELECT * FROM `bigquery-public-data.ml_datasets.penguins` LIMIT 1000;
  6. 실행을 클릭합니다.

  7. 쿼리 결과 섹션에서 데이터 탐색을 클릭한 후 Python 노트북으로 탐색을 클릭합니다.

사용할 노트북 준비

런타임에 연결하고 애플리케이션 기본값을 설정하여 사용할 노트북을 준비합니다.

  1. 노트북 헤더에서 연결을 클릭하여 기본 런타임에 연결합니다.
  2. 설정 코드 블록에서 셀 실행을 클릭합니다.

데이터 탐색

  1. penguins 데이터를 BigQuery DataFrame에 로드하고 결과를 표시하려면 BigQuery 작업에서 DataFrame으로 로드된 결과 집합 섹션의 코드 블록에서 셀 실행을 클릭합니다.
  2. 데이터의 설명 측정항목을 가져오려면 describe()를 사용하여 설명 통계 표시 섹션의 코드 블록에서 셀 실행을 클릭합니다.
  3. 선택사항: 다른 Python 함수나 패키지를 사용하여 데이터를 탐색하고 분석합니다.

다음 코드 샘플은 bigframes.pandas를 사용하여 데이터를 분석하고 bigframes.ml을 사용하여 BigQuery DataFrame의 penguins 데이터에서 선형 회귀 모델을 만드는 방법을 보여줍니다.

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)

# Inspect one of the columns (or series) of the DataFrame:
bq_df["body_mass_g"]

# Compute the mean of this series:
average_body_mass = bq_df["body_mass_g"].mean()
print(f"average_body_mass: {average_body_mass}")

# Find the heaviest species using the groupby operation to calculate the
# mean body_mass_g:
(
    bq_df["body_mass_g"]
    .groupby(by=bq_df["species"])
    .mean()
    .sort_values(ascending=False)
    .head(10)
)

# Create the Linear Regression model
from bigframes.ml.linear_model import LinearRegression

# Filter down to the data we want to analyze
adelie_data = bq_df[bq_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)