基本概念
热卡填充:在完整数据中找到一个与它最相似的对象,用最相似的值填充当前值
冷卡填充:通过其他途径找到能填充缺失部分的值
热卡填充其实就是使用KNN去预测的一种特殊形式,KNN是参考K个,而热卡填充是参考最近的1个,所以热卡填充可以用KNN做,关键代码是:
from sklearn.impute import KNNImputer
hot_deck_imputer = KNNImputer(n_neighbors=2, weights="uniform") # 虽然看着是用KNN,但是参数固定:n_neighbors=2
dataframe = hot_deck_imputer.fit_transform(dataframe)
示例代码
import pandas as pd
import numpy as np
def hot_deck_imputation(dataframe: pd.DataFrame):
from sklearn.impute import KNNImputer
hot_deck_imputer = KNNImputer(n_neighbors=2, weights="uniform") # 虽然看着是用KNN,但是参数固定:n_neighbors=2
new_df = hot_deck_imputer.fit_transform(dataframe)
return new_df
def get_dataset():
"""得到数据
:return data_x:有缺失值的数据
:return true_value:缺失数据的原始真实值
:return data_y:原问题中待预测的label
"""
import copy
from sklearn.datasets import make_classification
data_x, data_y = make_classification(n_samples=100, n_classes=4, n_features=6, n_informative=4,
random_state=0) # 6个特征
data_x = pd.DataFrame(data_x)
data_x.columns = ['x1', 'x2', 'x3', 'x4', 'x5', 'miss_line']
true_data = copy.deepcopy(data_x)
# 在miss_line这一列删除20%的数据
drop_index = data_x.sample(frac=0.2).index
data_x.loc[drop_index, "miss_line"] = np.nan
true_value = true_data.loc[drop_index, 'miss_line'] # 空值的真实值
return data_x, true_value, data_y
if __name__ == '__main__':
value_x, true_value_x, value_y = get_dataset()
fill_df = hot_deck_imputation(value_x)