target_leakage_data_check#
数据检查,用于检查特征是否通过互信息或皮尔逊相关性与目标高度相关。
模块内容#
类摘要#
检查特征是否通过互信息、皮尔逊相关性以及其他相关性指标与目标高度相关。 |
内容#
- class evalml.data_checks.target_leakage_data_check.TargetLeakageDataCheck(pct_corr_threshold=0.95, method='all')[source]#
检查特征是否通过互信息、皮尔逊相关性以及其他相关性指标与目标高度相关。
如果 method='mutual_info',此数据检查将使用互信息并支持所有目标和特征类型。其他相关性指标仅支持二分类目标与数值和布尔类型特征。如果选择其他相关性指标,此方法将返回 [-1, 1] 范围内的值;如果选择互信息,则返回 [0, 1] 范围内的值。可用的相关性指标可以在 Woodwork 的 dependence_dict 方法中找到。
- 参数
pct_corr_threshold (float) – 视为数据泄漏的相关性阈值。默认为 0.95。
method (string) – 确定相关性的方法。使用 'all' 或 'max' 表示最大相关性,或者对于特定的相关性指标,使用其名称(例如,'mutual_info' 表示互信息,'pearson' 表示皮尔逊相关性等)。可能的方法可以在 Woodwork 的 config 中找到,在 correlation_metrics 下。默认为 'all'。
方法
- name(cls)#
返回描述此数据检查的名称。
- validate(self, X, y)[source]#
检查特征是否通过互信息、皮尔逊相关性以及/或斯皮尔曼相关性与目标高度相关。
如果 method='mutual_info' 或 'method='max',此方法支持所有目标和特征类型。其他相关性指标仅支持二分类目标与数值和布尔类型特征。如果选择其他相关性指标,此方法将返回 [-1, 1] 范围内的值;如果选择互信息,则返回 [0, 1] 范围内的值。
- 参数
X (pd.DataFrame, np.ndarray) – 要检查的输入特征。
y (pd.Series, np.ndarray) – 目标数据。
- 返回值
如果检测到目标泄漏,则返回包含 DataCheckWarning 的字典。
- 返回类型
dict (DataCheckWarning)
示例
>>> import pandas as pd
任何与目标强相关的列都将发出警告。这可能预示着数据泄漏。
>>> X = pd.DataFrame({ ... "leak": [10, 42, 31, 51, 61] * 15, ... "x": [42, 54, 12, 64, 12] * 15, ... "y": [13, 5, 13, 74, 24] * 15, ... }) >>> y = pd.Series([10, 42, 31, 51, 40] * 15) ... >>> target_leakage_check = TargetLeakageDataCheck(pct_corr_threshold=0.95) >>> assert target_leakage_check.validate(X, y) == [ ... { ... "message": "Column 'leak' is 95.0% or more correlated with the target", ... "data_check_name": "TargetLeakageDataCheck", ... "level": "warning", ... "code": "TARGET_LEAKAGE", ... "details": {"columns": ["leak"], "rows": None}, ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "TargetLeakageDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["leak"], "rows": None} ... } ... ] ... } ... ]
默认方法可以从 mutual_info 更改为 pearson。
>>> X["x"] = y / 2 >>> target_leakage_check = TargetLeakageDataCheck(pct_corr_threshold=0.8, method="pearson") >>> assert target_leakage_check.validate(X, y) == [ ... { ... "message": "Columns 'leak', 'x' are 80.0% or more correlated with the target", ... "data_check_name": "TargetLeakageDataCheck", ... "level": "warning", ... "details": {"columns": ["leak", "x"], "rows": None}, ... "code": "TARGET_LEAKAGE", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "TargetLeakageDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["leak", "x"], "rows": None} ... } ... ] ... } ... ]