【发布时间】:2020-05-15 03:47:17
【问题描述】:
我到处寻找答案,但找不到。
我的目标:我正在尝试填充 DataFrame 中的一些缺失值,使用监督学习来决定如何填充它。
我的代码如下所示:注意 - 这第一部分并不重要,它只是提供上下文
train_df = df[df['my_column'].notna()] #I need to train the model without using the missing data
train_x = train_df[['lat','long']] #Lat e Long are the inputs
train_y = train_df[['my_column']] #My_column is the output
clf = neighbors.KNeighborsClassifier(2)
clf.fit(train_x,train_y) #clf is the classifies, here we train it
df_x = df[['lat','long']] #I need this part to do the prediction
prediction = clf.predict(df_x) #clf.predict() returns an array
series_pred = pd.Series(prediction) #now the array is a series
print(series_pred.shape) #RETURNS (2381,)
print(series_pred.isna().sum()) #RETURN 0
到目前为止,一切都很好。我有我的 2381 个预测(我只需要其中的几个)里面没有 NaN 值(为什么预测中会有 NaN 值?我只是想确定一下,因为我不明白我的错误)
在这里我尝试将预测分配给我的数据框:
#test_1
df.loc[df['my_colum'].isna(), 'my_colum'] = series_pred #I assign the predictions using .loc()
#test_2
df['my_colum'] = df['my_colum'].fillna(series_pred) #Double check: I assign the predictions using .fillna()
print(df['my_colum'].shape) #RETURNS (2381,)
print(df['my_colum'].isna().sum()) #RETURN 6
如您所见,i没有用:缺失值仍然是 6。我随机尝试了一种稍微不同的方法:
#test_3
df[['my_colum']] = df[['my_colum']].fillna(series_pred) #Will it work?
print(df[['my_colum']].shape) #RETURNS (2381, 1)
print(df[['my_colum']].isna().sum()) #RETURNS 6
没有用。我决定尝试最后一件事:甚至在 将结果分配 到原始 df 之前检查 fillna 结果:
In[42]:
print(df['my_colum'].fillna(series_pred).isna().sum()) #extreme test
Out[42]:
6
那么...我的非常非常愚蠢的错误在哪里?非常感谢
编辑 1
为了显示一点数据,
In[1]:
df.head()
Out[1]:
my_column lat long
id
9df Wil 51 5
4f3 Fabio 47 9
x32 Fabio 47 8
z6f Fabio 47 9
a6f Giovanni 47 7
另外,我在问题的开头添加了信息
【问题讨论】:
-
嗨 Federico,您能否发布一份您正在使用的数据样本?也许表格的输出也是如此。
-
series_pred的索引(行索引)是否匹配df? -
也不应该是
df.loc[df['my_colum'].isna(), 'my_colum'] = series_pred[df['my_colum'].isna()]吗?还有df和df_x有什么区别? -
我会重置索引,以便它们匹配...
series_pred.index = df.index。我猜想fillna之类的匹配索引而不是位置。 -
@Dan 是对的,当在
fillna中使用系列时,它是索引对齐的。如果您确定数据的大小,那么df.loc[df['my_colum'].isna(), 'my_colum'] = prediction应该这样做,无需创建系列
标签: python pandas numpy supervised-learning fillna