기대값 만들기

Great Expectations로 배우는 데이터 품질 입문

Davina Moossazadeh

Data Scientist

Expectations

Expectation - 데이터에 대한 검증 가능한 단언

  • 열(컬럼) Expectation
  • 형태 및 스키마 Expectation
    • schema - 데이터세트 구조의 설계도
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
Great Expectations로 배우는 데이터 품질 입문

Expectations

Expectation - 데이터에 대한 검증 가능한 단언

  • 열(컬럼) Expectation
  • 형태 및 스키마 Expectations
    • schema - 데이터세트 구조의 설계도
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
Great Expectations로 배우는 데이터 품질 입문

재생에너지 발전 데이터세트

재생에너지 발전 데이터가 담긴 pandas DataFrame. 열: "Time", "Energy delta[Wh]", "GHI", "temp", "pressure", "humidity", "wind_speed", "rain_1h", "snow_1h", "clouds_all". 총 118,066행.

1 https://www.kaggle.com/datasets/pythonafroz/renewable-power-generation-and-weather-conditions
Great Expectations로 배우는 데이터 품질 입문

Expectation 생성하기

gx.expectations.Expect...(...)

GX 클래스(Expectations): PascalCase

GX 함수/메서드: snake_case

1 https://docs.greatexpectations.io/docs/core/define_expectations/create_an_expectation/
Great Expectations로 배우는 데이터 품질 입문

Expectation 생성하기

row_count_expectation = gx.expectations.ExpectTableRowCountToEqual(

value=118000 )
validation_results = batch.validate(
expect=row_count_expectation )
1 https://docs.greatexpectations.io/docs/core/define_expectations/create_an_expectation/ https://docs.greatexpectations.io/docs/core/define_expectations/test_an_expectation/
Great Expectations로 배우는 데이터 품질 입문

Expectation 평가하기

print(validation_results)
{ 
    "success": false,
    "expectation_config": {
        "type": "expect_table_row_count_to_equal",
        "kwargs": {"batch_id": "my_pandas_datasource-my_dataframe_asset", "value": 118000},
        "meta": {},
        "rendered_content": [{"name": "atomic.prescriptive.summary", "value": {"schema": {"type": "com.superconductive.rendered.string"}, "template": "Must have exactly $value rows.", "params": {"value": {"schema": {"type": "number"}, "value": 118000}}}, "value_type": "StringValueType"}]
    },
    "result": {"observed_value": 118066},
    "meta": {},
    "exception_info": {"raised_exception": false, "exception_traceback": null, "exception_message": null},
    "rendered_content": [{"name": "atomic.diagnostic.observed_value", "value": {"schema": {"type": "com.superconductive.rendered.string"}, "template": "118066", "params": {}}, "value_type": "StringValueType"}]
}
1 https://docs.greatexpectations.io/docs/core/run_validations/run_a_validation_definition/
Great Expectations로 배우는 데이터 품질 입문

Expectation 평가하기

print(validation_results.describe())
{
    "expectation_type": "expect_table_row_count_to_equal",
    "success": false,
    "kwargs": {
        "batch_id": "my_pandas_datasource-my_dataframe_asset",
        "value": 118000
    },
    "result": {
        "observed_value": 118066
    }
}
Great Expectations로 배우는 데이터 품질 입문

Expectation 평가하기

print(validation_results.success)
False
print(validation_results["success"])
False
Great Expectations로 배우는 데이터 품질 입문

Expectation 평가하기

print(validation_results.result)
{'observed_value': 118066}
print(validation_results["result"])
{'observed_value': 118066}
Great Expectations로 배우는 데이터 품질 입문

기타 자주 쓰는 Expectations

형태(Shape) Expectations:

ExpectTableRowCountToEqual(value: int)
ExpectTableRowCountToBeBetween(
    min_value: int, max_value: int
)
ExpectTableColumnCountToEqual(
    value: int
)
ExpectTableColumnCountToBeBetween(
    min_value: int, max_value: int
)

열 이름 Expectations:

ExpectTableColumnsToMatchSet(
    column_set: set
)
ExpectColumnToExist(column: str)
Great Expectations로 배우는 데이터 품질 입문

치트시트

Expectation 생성:

gx.expectations.Expect...(...)

Expectation 검증:

validation_results = batch.validate(
    expect=expectation
)

행 수 Expectation 생성:

expectation = gx.expectations. \
ExpectTableRowCountToEqual(
    value: int
)

검증 결과 확인:

validation_results.describe()
validation_results.success
validation_results.result
Great Expectations로 배우는 데이터 품질 입문

Let's practice!

Great Expectations로 배우는 데이터 품질 입문

Preparing Video For Download...