【发布时间】:2017-12-27 14:23:33
【问题描述】:
我有一个大熊猫数据名气df。它有很多缺失。删除行/或 col-wise 不是一种选择。估算中位数、均值或最常见的值也不是一种选择(因此,不幸的是,用pandas 和/或scikit 进行估算并不能奏效)。
我遇到了一个名为fancyimpute 的看起来很简洁的包(你可以找到它here)。但我有一些问题。
这是我的工作:
#the neccesary imports
import pandas as pd
import numpy as np
from fancyimpute import KNN
# df is my data frame with the missings. I keep only floats
df_numeric = = df.select_dtypes(include=[np.float])
# I now run fancyimpute KNN,
# it returns a np.array which I store as a pandas dataframe
df_filled = pd.DataFrame(KNN(3).complete(df_numeric))
然而,df_filled 不知何故是一个单一的向量,而不是填充的数据框。如何通过插补获取数据框?
更新
我意识到,fancyimpute 需要 numpay array。因此,我使用as_matrix() 将df_numeric 转换为一个数组。
# df is my data frame with the missings. I keep only floats
df_numeric = df.select_dtypes(include=[np.float]).as_matrix()
# I now run fancyimpute KNN,
# it returns a np.array which I store as a pandas dataframe
df_filled = pd.DataFrame(KNN(3).complete(df_numeric))
输出是一个缺少列标签的数据框。有什么方法可以检索标签?
【问题讨论】:
-
df_filled.columns = df_numeric.columns应该这样做。顺便说一句,这看起来确实是一个有趣的包 -
我也这么认为!我对
pandas fillna()和sklearn.preprocessing.Imputer有点失望。我没有遇到可以充分利用它们的情况。我认为,他们将极大地受益于一些更复杂的方法来估算/插入缺失数据。
标签: python python-3.x pandas imputation fancyimpute