简体   繁体   English

在scikit-learn中使用Imputer

[英]using Imputer in scikit-learn

I need to fill the missing temperature value with the mean value of that month using Imputer() in scikit-learn. 我需要使用scikit-learn中的Imputer()用该月的平均值填充缺失的温度值。

First I split the dataframe into groups based on the month. 首先,我根据月份将数据框分为几组。 Then I called the imputer function to calculate the mean for that group and fill in the missing values. 然后,我调用了imputer函数以计算该组的均值并填写缺失值。

Here is the code I wrote but it didn't work: 这是我写的代码,但是没有用:

def impute_missing (data_1_group):
    imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
    imp.fit(data_1_group)
    data_1_group=imp.transform(data_1_group['datetime'])
    return(data_1_group)

for data_1_group in data_1.groupby(pd.TimeGrouper("M")):
    impute_missing(data_1_group)

Any suggestion? 有什么建议吗?

try this small change 试试这个零钱

imp=imp.fit(data_1_group['datetime']) data_1_group=imp.transform(data_1_group['datetime'])

Though I m new to scikit myself, I am recommending the solution that worked for me. 尽管我自己不是scikit的新手,但我还是建议适用于我的解决方案。 This is because 这是因为

1) imp object needs to override to fit, as in the first line 1)imp对象需要覆盖以适合,如第一行所示

2) it needs to fit and impute the same dataset, which in this case seems to be data_1_group['datetime'] 2)它需要拟合和估算相同的数据集,在这种情况下,它似乎是data_1_group ['datetime']

I hope this helps 我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM