สร้าง Expectations

การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Davina Moossazadeh

Data Scientist

Expectations

Expectation - ข้อความยืนยันเกี่ยวกับข้อมูลที่ตรวจสอบได้

  • Column Expectations
  • Shape และ schema Expectations
    • schema - โครงสร้างของชุดข้อมูล
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Expectations

Expectation - ข้อความยืนยันเกี่ยวกับข้อมูลที่ตรวจสอบได้

  • Column Expectations
  • Shape และ schema Expectations
    • schema - โครงสร้างของชุดข้อมูล
1 https://docs.greatexpectations.io/docs/reference/learn/terms/expectation
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

ชุดข้อมูลการผลิตพลังงานหมุนเวียน

DataFrame ของ pandas ที่มีข้อมูลการผลิตพลังงานหมุนเวียน ประกอบด้วยคอลัมน์: "Time", "Energy delta[Wh]", "GHI", "temp", "pressure", "humidity", "wind_speed", "rain_1h", "snow_1h" และ "clouds_all" โดย DataFrame มีทั้งหมด 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
)

Column name 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:

validation_results.describe()
validation_results.success
validation_results.result
การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

มาฝึกกันเถอะ!

การตรวจสอบคุณภาพข้อมูลด้วย Great Expectations เบื้องต้น

Preparing Video For Download...