from sklearn.ensemble import RandomForestRegressor
x3= x.copy()
sortindex = np.argsort(x3.isnull().sum(axis=0)).values
for i in sortindex:
#构建我们的新特征矩阵和新标签
df = x3
fillc = df.iloc[:,i]
df = pd.concat([df.iloc[:,df.columns != i],pd.DataFrame(y)],axis=1)
#在新特征矩阵中,对含有缺失值的列,进行0的填补
df_0 =SimpleImputer(missing_values=np.nan,
strategy='constant',
fill_value=0).fit_transform(df)
#找出我们的训练集和测试集
y_train = fillc[fillc.notnull()]
y_test = fillc[fillc.isnull()]
x_train = df_0[y_train.index,:]
x_test = df_0[y_test.index,:]
clf = RandomForestRegressor(n_estimators=100)
clf = clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)
#将填补好的特征返回到我们的原始的特征矩阵中
x3.loc[x3.iloc[:,i].isnull(),i] = y_pred
x3.isnull().sum()