阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。
1. Missing Value
缺失值独立示例:
| 样本 | 年龄 | 分数 |
|---|---|---|
| A | 36 | 65 |
| B | NA | NA |
| C | 18 | 62 |
| D | 36 | 70 |
缺失不是0,NA表示没有观测。记录缺失机制与数据来源,后续删除或填补会改变可用样本与分布。
1.1 Why do we need to handle missing values?
- Data information loss
- Lead to wrong prediction/classification
- 并非所有 scikit-learn 模型都接受缺失值;部分模型原生支持 NaN,其余应在训练集内拟合填补步骤。
缺失可能改变结论: 若某组低分样本更易缺失,只比较已观测均值就可能高估该组。不能从缺失前后一个小表推断群体因果关系;要检查缺失机制、比例与敏感性。
1.2 How to detect missing values?
In pandas, NaN is the default missing value marker. Using pandas api isnull(), notnull(), any(). all() to detect the missing values.
1.3 How to handle missing values?
Delete rows vs delete cols
- Elimination
- Delete rows (observations)
Remove all data for an observation that has one or more missing values.
Particularly if the missing data is limited to a small number of observations, you may just opt to eliminate those ccases from the analysis.
- Delete rows (observations)
六种处理关系(独立整理):
- 删行:仅保留所需字段完整的观测,样本量会减少。
- 删列:在业务允许且缺失严重时删除变量,不把60%当普适阈值。
- 分组填补:可在训练数据内按分组拟合中位数/均值,注意小组不稳定性。
- KNN填补:按其他可观测特征找邻居,连续值平均、类别可众数,距离计算需处理缺失与尺度。
- 模型填补:用已观测该字段的训练样本拟合预测器,再预测其缺失项;不是把正式测试集拿来训练。
- 简单填补:数值用中位数/均值、类别用众数或专门缺失类别;保留缺失指示变量有时有用。
所有填补参数只在训练折拟合,并对验证/测试复用;填补值不是新增的真实观测。
* Delete columns (variables)
Drop varaibles if the data is missing for more than a threshold (like 60%)
Drop these observations but only if that variable is insignificant. 2. Median/Mean, More imputation
-
Predictive Model
-
KNN imputation