python - 如何使用 KNN 估算缺失值
问题描述
我正在尝试从我的数据框中估算缺失值,为此我使用了 fancyimpute 库。
from fancyimpute import KNN
X_filled_knn = KNN(k=3).complete(df_OppLine[['family']])
我有这个错误:
AttributeError Traceback (most recent call last)
<ipython-input-28-8475f35fc36a> in <module>()
----> 1 X_filled_knn = KNN(k=3).complete(df_OppLine[['family']])
AttributeError: 'KNN' object has no attribute 'complete'
有什么想法可以帮助我解决此错误吗?
解决方案
首先,您必须将字符串转换为数字数据。
尝试 one-hot 编码(为每个类别创建一个列,值仅为相应类别的 1,其余为 0)。您也可以尝试序数编码。它为每个类别分配一个值
from sklearn.preprocessing import OrdinalEncoder
# Create Ordinal encoder
initialize_encoder=OrdinalEncoder()
# Select non-null values of family column
family=df_OppLine["family"]
family_not_null=family[family.notnull()]
# Reshape family_not_null to shape (-1, 1)
reshaped_vals=family_not_null.values.reshape(-1,1)
# Ordinally encode reshaped_vals
encoded_vals=initialize_encoder.fit_transform(reshaped_vals)
# Assign back encoded values to non-null values
df_OppLine.loc[family.notnull(),"family"]=np.squeeze(encoded_vals)
推荐阅读
- python-3.x - 去掉 '$' 和 ',' 的数字不会从 str 转换为 int
- mysql - 如何为表中存在的每一列调用 MySQL 函数?
- typescript - 如何在抽象类中推断构造函数参数类型
- c++ - QObject::connect: 没有这样的信号错误 C++
- http - iframe 无法访问自己创建的 cookie
- android - Android Spinner 自定义文本视图不可点击
- python - 如何在 Python Web 应用程序中为状态更改通知生成服务器发送的事件?
- javascript - 如何使用javascript获取开始日期和结束日期之间的日期,结果日期应为数组格式
- java - 在 java 中使用 Zeroc Ice 时出现 NoValueFactoryException
- android - 处理网格视图中的项目可见性