null_data_check#
数据检查,用于检查输入中是否存在高度为空值的列和行。
模块内容#
类摘要#
检查输入中是否存在高度为空值的数值型、布尔型、类别型、自然语言型和未知类型的列和行。 |
目录#
- class evalml.data_checks.null_data_check.NullDataCheck(pct_null_col_threshold=0.95, pct_moderately_null_col_threshold=0.2, pct_null_row_threshold=0.95)[source]#
检查输入中是否存在高度为空值的数值型、布尔型、类别型、自然语言型和未知类型的列和行。
- 参数
pct_null_col_threshold (float) – 如果输入特征中 NaN 值的百分比超过此数值,则该列将被视为高度为空值。默认为 0.95。
pct_moderately_null_col_threshold (float) – 如果输入特征中 NaN 值的百分比超过此数值但小于 pct_null_col_threshold 中指定的百分比,则该列将被视为中度为空值。默认为 0.20。
pct_null_row_threshold (float) – 如果输入行中 NaN 值的百分比超过此数值,则该行将被视为高度为空值。默认为 0.95。
方法
查找被视为高度为空值(空值百分比大于阈值)的列,并返回列名到空值百分比的字典以及列名到该列中空值索引的字典。
查找被视为高度为空值(空值百分比大于阈值)的行。
返回描述数据检查的名称。
检查输入中是否存在高度为空值的列或行。
- static get_null_column_information(X, pct_null_col_threshold=0.0)[source]#
查找被视为高度为空值(空值百分比大于阈值)的列,并返回列名到空值百分比的字典以及列名到该列中空值索引的字典。
- 参数
X (pd.DataFrame) – 用于检查高度为空值列的 DataFrame。
pct_null_col_threshold (float) – 列被视为空值的百分比阈值。默认为 0.0。
- 返回值
包含以下内容的元组:将列名映射到其空值百分比的字典,以及将列名映射到该列中空值索引的字典。
- 返回类型
tuple
- static get_null_row_information(X, pct_null_row_threshold=0.0)[source]#
查找被视为高度为空值(空值百分比大于阈值)的行。
- 参数
X (pd.DataFrame) – 用于检查高度为空值行的 DataFrame。
pct_null_row_threshold (float) – 行被视为空值的百分比阈值。默认为 0.0。
- 返回值
包含每行空值百分比的 Series。
- 返回类型
pd.Series
- name(cls)#
返回描述数据检查的名称。
- validate(self, X, y=None)[source]#
检查输入中是否存在高度为空值的列或行。
- 参数
X (pd.DataFrame, np.ndarray) – 特征。
y (pd.Series, np.ndarray) – 忽略。默认为 None。
- 返回值
如果存在高度为空值的列或行,则返回包含 DataCheckWarning 的 dict。
- 返回类型
dict
示例
>>> import pandas as pd ... >>> class SeriesWrap(): ... def __init__(self, series): ... self.series = series ... ... def __eq__(self, series_2): ... return all(self.series.eq(series_2.series))
将 pct_null_col_threshold 设置为 0.50 时,任何包含 50% 或更多空值观测值的列都将包含在警告中,以及识别出的空值百分比(“all_null”: 1.0, “lots_of_null”: 0.8)。
>>> df = pd.DataFrame({ ... "all_null": [None, pd.NA, None, None, None], ... "lots_of_null": [None, None, None, None, 5], ... "few_null": [1, 2, None, 2, 3], ... "no_null": [1, 2, 3, 4, 5] ... }) ... >>> highly_null_dc = NullDataCheck(pct_null_col_threshold=0.50) >>> assert highly_null_dc.validate(df) == [ ... { ... "message": "Column(s) 'all_null', 'lots_of_null' are 50.0% or more null", ... "data_check_name": "NullDataCheck", ... "level": "warning", ... "details": { ... "columns": ["all_null", "lots_of_null"], ... "rows": None, ... "pct_null_rows": {"all_null": 1.0, "lots_of_null": 0.8} ... }, ... "code": "HIGHLY_NULL_COLS", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "NullDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["all_null", "lots_of_null"], "rows": None} ... } ... ] ... }, ... { ... "message": "Column(s) 'few_null' have between 20.0% and 50.0% null values", ... "data_check_name": "NullDataCheck", ... "level": "warning", ... "details": {"columns": ["few_null"], "rows": None}, ... "code": "COLS_WITH_NULL", ... "action_options": [ ... { ... "code": "IMPUTE_COL", ... "data_check_name": "NullDataCheck", ... "metadata": {"columns": ["few_null"], "rows": None, "is_target": False}, ... "parameters": { ... "impute_strategies": { ... "parameter_type": "column", ... "columns": { ... "few_null": { ... "impute_strategy": {"categories": ["mean", "most_frequent"], "type": "category", "default_value": "mean"} ... } ... } ... } ... } ... } ... ] ... } ... ]
将 pct_null_row_threshold 设置为 0.50 时,任何包含 50% 或更多相应列值为空值的行都将包含在警告中,以及存在问题的行(“rows”: [0, 1, 2, 3])。由于 pct_null_col_threshold 的默认值为 0.95,“all_null” 也包含在警告中,因为该行中的空值百分比超过 95%。由于 pct_moderately_null_col_threshold 的默认值为 0.20,“few_null” 被视为“中度为空值”的列,因为它具有 20% 的空值百分比。
>>> highly_null_dc = NullDataCheck(pct_null_row_threshold=0.50) >>> validation_messages = highly_null_dc.validate(df) >>> validation_messages[0]["details"]["pct_null_cols"] = SeriesWrap(validation_messages[0]["details"]["pct_null_cols"]) >>> highly_null_rows = SeriesWrap(pd.Series([0.5, 0.5, 0.75, 0.5])) >>> assert validation_messages == [ ... { ... "message": "4 out of 5 rows are 50.0% or more null", ... "data_check_name": "NullDataCheck", ... "level": "warning", ... "details": { ... "columns": None, ... "rows": [0, 1, 2, 3], ... "pct_null_cols": highly_null_rows ... }, ... "code": "HIGHLY_NULL_ROWS", ... "action_options": [ ... { ... "code": "DROP_ROWS", ... "data_check_name": "NullDataCheck", ... "parameters": {}, ... "metadata": {"columns": None, "rows": [0, 1, 2, 3]} ... } ... ] ... }, ... { ... "message": "Column(s) 'all_null' are 95.0% or more null", ... "data_check_name": "NullDataCheck", ... "level": "warning", ... "details": { ... "columns": ["all_null"], ... "rows": None, ... "pct_null_rows": {"all_null": 1.0} ... }, ... "code": "HIGHLY_NULL_COLS", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "NullDataCheck", ... "metadata": {"columns": ["all_null"], "rows": None}, ... "parameters": {} ... } ... ] ... }, ... { ... "message": "Column(s) 'lots_of_null', 'few_null' have between 20.0% and 95.0% null values", ... "data_check_name": "NullDataCheck", ... "level": "warning", ... "details": {"columns": ["lots_of_null", "few_null"], "rows": None}, ... "code": "COLS_WITH_NULL", ... "action_options": [ ... { ... "code": "IMPUTE_COL", ... "data_check_name": "NullDataCheck", ... "metadata": {"columns": ["lots_of_null", "few_null"], "rows": None, "is_target": False}, ... "parameters": { ... "impute_strategies": { ... "parameter_type": "column", ... "columns": { ... "lots_of_null": {"impute_strategy": {"categories": ["mean", "most_frequent"], "type": "category", "default_value": "mean"}}, ... "few_null": {"impute_strategy": {"categories": ["mean", "most_frequent"], "type": "category", "default_value": "mean"}} ... } ... } ... } ... } ... ] ... } ... ]