在RandomForestRegressor模型中使用平均绝对误差时,如何解决“值误差”?

时间:2019-06-23 21:32:56

标签: python machine-learning scikit-learn

我正在使用scikit学习库运行RandomTreeForest模型,并且在使用mean absolute评估其准确性时,出现ValueError:

  

“找到样本数量不一致的输入变量”。

我使用的pandas数据框来自一个房地产csv文件,在我添加了一些缺失值后对其进行了修改。请参见下面的代码。

X_train, X_valid, y_train, y_valid = train_test_split(X, y, train_size=0.8, test_size=0.2, random_state=0)

X_train_plus = X_train.copy()
X_valid_plus = X_train.copy()

for col in cols_with_missing:
    X_train_plus[col + "_was_missing"] = X_train_plus[col].isnull()
    X_valid_plus[col + "_was_missing"] = X_valid_plus[col].isnull()

my_imputer = SimpleImputer()
imp_X_train_plus = pd.DataFrame(my_imputer.fit_transform(X_train_plus))
imp_X_valid_plus = pd.DataFrame(my_imputer.fit_transform(X_valid_plus))

imp_X_train_plus.columns = X_train_plus.columns
imp_X_valid_plus.columns = X_valid_plus.columns

model_1 = RandomForestRegressor(n_estimators=50, random_state=0)
model_2 = RandomForestRegressor(n_estimators=100, random_state=0)
model_3 = RandomForestRegressor(n_estimators=100, criterion="mae", random_state=0)
model_4 = RandomForestRegressor(n_estimators=100, min_samples_split=20, random_state=0)
model_5 = RandomForestRegressor(n_estimators=100, max_depth=7, random_state=0)

models = [model_1, model_2, model_3, model_4, model_5]

def score_model (model, imp_X_train_plus, y_train, imp_X_valid_plus, y_valid):
    model.fit(imp_X_train_plus, y_train)
    pred = model.predict(imp_X_valid_plus)
    return mean_absolute_error(y_valid, pred)

for i in range(0, len(models)):
    mae = score_model (models[i], imp_X_train_plus, y_train, imp_X_valid_plus, y_valid)
    print("Model %d with extended imputed has a MAE: %d" %(i+1, mae)) 

我希望输出类似于以下内容:

  

“具有扩展插补的Model 1具有MAE:345237”

但是相反,当返回在score_model函数中调用mean_absolute_error时,我得到的是以下值错误:

  

“ ValueError:找到样本数量不一致的输入变量:[2716,10864]”

我认为错误可能出在imp_X_train_plus和imp_X_valid_plus变量中,但是我运行了一个非常类似的模型,该模型实现了数据帧并且运行良好。

1 个答案:

答案 0 :(得分:0)

可能是由于您的3d线:X_valid_plus = X_train.copy()

您是要这样做吗:X_valid_plus = X_valid.copy()