有什么方法可以将 GridSearchCV 与 RandomForestRegressor 一起使用?

时间:2021-01-27 03:45:31

标签: python scikit-learn random-forest gridsearchcv

我改进模型的代码是:

clf = RandomForestRegressor()
parameters = {'n_estimators': [4, 6, 9],
                  'max_features': ['log2', 'sqrt','auto'],
                  'criterion': ['mse', 'mae'],
                  'max_depth': [2, 3, 5, 10],
                  'min_samples_split': [2, 3, 5],
                  'min_samples_leaf': [2,5,8]
                 }
    
    print("Improving accuracy")
    acc_scorer = make_scorer(accuracy_score)
    print("Running Grid Search")
    grid_obj = GridSearchCV(clf, parameters, scoring=acc_scorer)
    print("Fitting using optimal parameters")
    grid_obj = grid_obj.fit(train_X_g_h, train_y_g_h)
    clf = grid_obj.best_estimator_
    print("Fit the best algorithm to the data")
    clf.fit(train_X_g_h, train_y_g_h)
    print("Running predictions")
    predictions = clf.predict(val_X_g_h)
    predictions = pd.DataFrame({'Result' : predictions})
    print("Getting Accuracy score")
    print(accuracy_score(val_y_g_h, predictions))

我得到的错误是: ValueError:分类指标无法处理多类和连续目标的混合

我阅读了文档,但找不到任何支持来估计 RandomForestRegressor 模型的准确性

那么,有什么方法可以让我的模型获得准确度并将其与 GridSearchCV 一起使用?

0 个答案:

没有答案