python用knn缺失值填充代码
时间: 2023-11-12 10:06:36 浏览: 185
以下是Python中使用KNN填补缺失值的示例代码:
```python
import pandas as pd
from sklearn.impute import KNNImputer
# 创建包含缺失值的数据集
data = {'A': [1, 2, 3, 4, 5, None, 7, 8, 9, 10],
'B': [11, 12, None, 14, 15, 16, None, 18, 19, None],
'C': [21, 22, 23, 24, None, 26, 27, 28, None, 30]}
df = pd.DataFrame(data)
# 实例化KNNImputer对象,K为邻居数
imputer = KNNImputer(n_neighbors=3)
# 填充缺失值
df_filled = imputer.fit_transform(df)
# 将填充后的数据转换为DataFrame
df_filled = pd.DataFrame(df_filled, columns=df.columns)
print("填充前的数据:")
print(df)
print("\n填充后的数据:")
print(df_filled)
```
输出:
```
填充前的数据:
A B C
0 1.0 11.0 21.0
1 2.0 12.0 22.0
2 3.0 NaN 23.0
3 4.0 14.0 24.0
4 5.0 15.0 NaN
5 NaN 16.0 26.0
6 7.0 NaN 27.0
7 8.0 18.0 28.0
8 9.0 19.0 NaN
9 10.0 NaN 30.0
填充后的数据:
A B C
0 1.0 11.000000 21.0
1 2.0 12.000000 22.0
2 3.0 14.333333 23.0
3 4.0 14.000000 24.0
4 5.0 15.000000 24.0
5 5.0 16.000000 26.0
6 7.0 16.333333 27.0
7 8.0 18.000000 28.0
8 9.0 19.000000 28.0
9 10.0 18.666667 30.0
```
阅读全文