一.缺失数据处理
1.1填充缺失值
大多数的机器学习算法无法处理非数值(not a number), 在python中非数值用nan表示。所以我们就需要把所有的nan值替换为某个合适的填充值。这个操作称为填充缺失值。
1.2scikit-learn实现
在scikit-learn的sklearn.impute
.SimpleImputer类中提供了三种不同的方法(策略)来填充缺失值。
(1)mean: 将所有的nan值填充为矩阵在指定坐标轴上元素的平均值(默认情况,axis=0)。
(2)median: 将所有的nan值填充为矩阵在指定坐标轴上元素的中值(默认情况,axis=0)。
(3)most_frequent: 将所有的nan值填充为矩阵在指定坐标轴上00(出现频率最高的值(默认情况,axis=0)
(4)constant: 将所有的nan值使用常量填充。
1.3验证
# -*- coding:utf-8 -*-
import numpy as np
from numpy import nan
from sklearn.impute import SimpleImputer
X = np.array(
[[nan, 0, -3],
[2, 9, -8],
[1, nan, 1],
[5, 2, 4],
[7, 6, -3]]
)
# 使用平均值(mean)填充
imp_mean = SimpleImputer(missing_values=nan, strategy='mean')
X_mean =