YiTaoXianYu
2022-05-09 21:41
采纳率: 0%
浏览 175

请问为什么随机森林算法结果报错:Found array with 0 sample(s) (shape=(0, 11)) while a minimum of 1 is required.

img

img


import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.impute import SimpleImputer

data = pd.read_excel('D:\Study\master\DO\Origin_all.xlsx',sheet_name= 'test',usecols=[2,3,4,5,6,7,8,9,10,11,12],nrows=9999)
lst=[1 for _ in range(9999)]
target = pd.DataFrame({'label':lst})

sortindex = data.isnull().sum().sort_values().index
sortindex = list(sortindex)
for i in sortindex:
df = data
fillc = df.loc[:,i]
df = pd.concat([df.loc[:,df.columns != i],target],axis=1)
df_0 = SimpleImputer(missing_values=np.nan , strategy="constant" , fill_value=0).fit_transform(df)

Ytrain = fillc[fillc.notnull()]
Ytest = fillc[fillc.isnull()]
Xtrain = df_0[Ytrain.index,:]
Xtest = df_0[Ytest.index,:]

rfc = RandomForestRegressor(n_estimators=50,random_state=0,max_depth=5)
rfc = rfc.fit(Xtrain, Ytrain)
Ypredict = rfc.predict(Xtest)

data.loc[data.loc[:,i].isnull(),i] = Ypredict
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题