Fork me on GitHub

基于随机森林方法的缺失值填充

本文中主要是利用sklearn中自带的波士顿房价数据,通过不同的缺失值填充方式,包含均值填充、0值填充、随机森林的填充,来比较各种填充方法的效果

缺失值

现实中收集到的数据大部分时候都不是完整,会存在缺失值。

  • 有些时候会直接将含有缺失值的样本删除drop
  • 但是有的时候,利用0值、中值、其他常用值或者随机森林填充缺失值效果更好
  • sklearn中使用sklearn.impute.SimpleImputer类填充缺失值

填充缺失值

先让原始数据中产生缺失值,然后采用3种不同的方式来填充缺失值

  • 均值填充
  • 0值填充
  • 随机森林方式填充

波士顿房价数据

各种包和库

1
2
3
4
5
6
7
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston # 波士顿数据
from sklearn.impute import SimpleImputer # 填充缺失值的类
from sklearn.ensemble import RandomForestRegressor # 随机森林回归
from sklearn.model_selection import cross_val_score # 交叉验证

查看数据

1
2
3
dataset = load_boston()
dataset.data.shape # 标签是连续型的值,用于回归分析
dataset.target[:5] # 标签是连续的数值,连续型变量,用于回归问题

完整数据

1
2
3
X_full, y_full = dataset.data, dataset.target
n_samples = X_full.shape[0] # 506
n_features = X_full.shape[1] # 13

向完整数据中填充缺失值

设置缺失的样本总数

1
2
3
4
5
rng = np.random.RandomState(0)  # 确定随机种子
missing_rate = 0.5 # 缺失率是50%
# 计算缺失的样本总数;floor是向下取整
n_missing_samples = int(np.floor(n_samples * n_features * missing_rate))
n_missing_samples

随机数填充

  • 数据集要随机遍布在各行各列中,而一个缺失的数据需要行列两个指标
  • 创造一个数组,行索引在0-506,列索引在0-13之间,利用索引来进行填充3289个位置的数据
  • 利用0、均值、随机森林分别进行填充
1
2
3
4
5
6
7
# randint(下限,上限,n):在上限和下限之间随机取出n个整数
missing_features = rng.randint(0, n_features, n_missing_samples) # (0,13,3289)
missing_samples = rng.randint(0, n_samples, n_missing_samples) # (0,506,3289)

# 采样了3289个数据,远远超过了样本量506,使用随机抽取的函数randint;
# 如果需要的数据量是小于样本量506,则需要使用randint.choice来抽样,保证抽取不重复的随机数
# missing_samples = rng.choice(n_samples, n_missing_samples, replace=False)

均值填充

1
2
imp_mean = SimpleImputer(missing_values=np.nan, strategy="mean")  # 指定缺失值是什么和用什么填充
X_missing_mean = imp_mean.fit_transform(X_missing) # fit + predict---->特殊接口transform

检查得到的数据是否存在缺失值

1
pd.DataFrame(X_missing_mean).isnull().sum() # X_missing_mean是一个ndaraay

0值填充

1
2
imp_0 = SimpleImputer(missing_values=np.nan, strategy="constant", fill_value=0)   # 用0进行填充
X_missing_0 = imp_0.fit_transform(X_missing)

随机森林填充

如何填充

假设一个具有n个特征的数据,特征T存在缺失值**(大量缺失更适合)**,把T当做是标签,其他的n-1个特征和原来的数据看作是新的特征矩阵,具体数据解释为:

数据 说明
Xtrain 特征T不缺失的值对应的n-1个特征+原始标签
ytrain 特征T不缺失的值
Xtest 特征T缺失的值对应的n-1个特征+原始标签
ytest 特征T缺失值(未知)

如果其他特征也存在缺失值,遍历所有的特征,从缺失值最少的开始。

  1. 缺失值越少,所需要的准确信息也越少
  2. 填补一个特征,先将其他特征值的缺失值用0代替,这样每次循环一次,有缺失值的特征便会减少一个

图形解释

假设数据有n个特征,m行数据

由于是从最少的缺失值特征开始填充,那么需要找出存在缺失值的索引的顺序:argsort函数的使用

1
2
3
X_missing_reg = X_missing.copy()
# 找出缺失值从小到大对应的索引值
sortindex = np.argsort(X_missing_reg.isnull().sum(axis=0)).values

填充过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
for i in sortindex:

# 构建新的特征矩阵和新标签
df = X_missing_reg # 所有的操作都在df上进行,只是最后得到的填充值作用在X_missing_reg上面
fillc = df.iloc[:, i] # 某个需要填充的列,索引为i

# 没有被选中填充(!=)的特征与原始标签的连接起来;df就是新特征矩阵
df = pd.concat([df.iloc[:, df.columns != i], pd.DataFrame(y_full)], axis=1)


# 新的特征矩阵df中,对含有缺失值的列,进行0的填补
# 检查是否有0 pd.DataFrame(df_0).isnull().sum()
df_0 = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0).fit_transform(df)

# 找出训练集和测试集
ytrain = fillc[fillc.notnull()] # 被选中填充的特征矩阵T中的非空值
ytest = fillc[fillc.isnull()] # 被选中填充的特征矩阵T中的空值
Xtrain = df_0[ytrain.index, :] # 新特征矩阵上,被选出来要填充的特征的非空值对应的记录
Xtest = df_0[ytest.index, :] # 空值对应的记录

# 随机森林填充缺失值
rfc = RandomForestRegressor(n_estimators=100)
rfc = rfc.fit(Xtrain, ytrain)
y_predict = rfc.predict(Xtest) # predict接口预测得到的结果就是用来填充空值的那些值

# 将填补好的特征返回到我们的原始特征矩阵中
X_missing_reg.loc[X_missing_reg.iloc[:, i].isnull(), i] = y_predict

4种数据建模

  • MSE:均方误差$MSE=\frac{1}{N}\sumN_{i=1}(f_i-y_i)2$;回归树的衡量指标,越小越好。sklearn中使用的是负均方误差neg_mean_squared_error。均方误差本身是种误差loss,通过负数表示
  • $R^2$:回归树score返回的真实值是R的平方,不是MSE

$R^2=1-\frac{u}{v}$

$u=\sumN_{i=1}(f_i-y_i)2$

$v=\sum^N_{i=1}(y_i-\hat y)^2$

1
2
3
4
5
6
7
X = [X_full, X_missing_mean, X_missing_0, X_missing_reg]
mse = []

for x in X:
estimator = RandomForestRegressor(random_state=0, n_estimators=100)
scores = cross_val_score(estimator, x, y_full, scoring="neg_mean_squared_error", cv=5).mean()
mse.append(scores * -1)

绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
x_labels = ["Full Data",
"Mean Imputation",
"Zero Imputation",
"Regressor Imputation"]

colors = ['r', 'g', 'b', 'orange']

plt.figure(figsize=(12,6)) # 生成画布
ax = plt.subplot(111) # 添加子图

# 在上面的过程中已经求出了4种不同数据的MSE
for i in np.arange(len(mse)): # arange(4)
ax.barh(i, mse[i], color=colors[i],alpha=0.6,align='center')

ax.set_title("Imputation Techniques with Boston Data") # 标题
ax.set_xlim(left=np.min(mse)*0.9,
right=np.max(mse)*1.1) # 横轴是mse的取值

ax.set_yticks(np.arange(len(mse)))
ax.set_label("MSE")
ax.set_yticklabels(x_labels) # 用x_labels中的名字作为y轴的命名
plt.show()

本文标题:基于随机森林方法的缺失值填充

发布时间:2020年01月11日 - 17:01

原始链接:http://www.renpeter.cn/2020/01/11/%E5%9F%BA%E4%BA%8E%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97%E6%96%B9%E6%B3%95%E7%9A%84%E7%BC%BA%E5%A4%B1%E5%80%BC%E5%A1%AB%E5%85%85.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea