python - 使用imputer后如何将数据放入数据框中?

标签 python scikit-learn

我有一些代码可以帮助我预测一些缺失值。这就是代码

from datawig import SimpleImputer
from datawig.utils import random_split
from sklearn.metrics import f1_score, classification_report
df_train, df_test = random_split(df, split_ratios=[0.8, 0.2])
# Initialize a SimpleImputer model
imputer = SimpleImputer(
input_columns=['SITUACION_DNI_A'],  # columns containing information about 
 the column we want to impute
output_column='EXTRANJERO_A',  # the column we'd like to impute values for
output_path='imputer_model'  # stores model data and metrics
)

# Fit an imputer model on the train data
imputer.fit(train_df=df_train, num_epochs=10)

# Impute missing values and return original dataframe with predictions
predictions = imputer.predict(df_test)

之后,我得到一个比原始数据帧少行的新数据帧,如何将预测中获得的值插入到原始数据帧中,或者有一种方法可以使用我的所有数据帧而不是测试

最佳答案

如果两个数据框都有一个唯一的列或可以充当 ID 的东西,那么此方法将起作用

df_test = df_test.set_index('unique_col')
df_test.fillna(predictions.set_index('unique_col'))

如果上述方法不起作用,则删除具有缺失值的行并将输入器预测附加到数据帧。查看以下链接寻求帮助

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html

Delete rows if there are null values in a specific column in Pandas dataframe

关于python - 使用imputer后如何将数据放入数据框中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56133859/

相关文章:

Python 和显示 HTML

python - 用 bool 值替换 Pandas 系列中的数字

machine-learning - 什么是 f1-score 以及它的值表示什么?

scikit-learn - 如何比较 PCA 和 NMF 的预测能力

python - ERROR :gcm_channel_status_request. cc(145)] GCM channel 请求失败消息在 python 项目的终端中显示

python - 如何将内联(也许是 heredoc?)python 脚本插入 bash stdin/stdout 流管道

python - Dask dataframe str.contains(regex=True) 不比 pandas 快

python - 使用、准备用于回归的词袋数据

python - sklearn 中的分类树给出不一致的答案

machine-learning - 可以在不平衡数据上建立模型吗?