【问题标题】:RandomizedSearchCV reduces accuracy score drastically [closed]RandomizedSearchCV 大大降低了准确度得分 [关闭]
【发布时间】:2021-08-18 07:43:29
【问题描述】:

当我使用 RandomForestRegressor 进行检查时

from sklearn.ensemble import RandomForestRegressor
r=RandomForestRegressor()
r.fit(X_train,y_train)
r.score(X_test,y_test)

我得到 0.9746156332220394

但是当我使用 RandomizedSearchCV

from sklearn.ensemble import RandomForestRegressor
n_estimators = [int(x) for x in np.linspace(start = 100, stop =1200, num = 12)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(5, 30, num = 6)]
min_samples_split = [2, 5, 10, 15, 100]
min_samples_leaf = [1, 2, 5, 10]

from sklearn.model_selection import RandomizedSearchCV
random_grid = {'n_estimators': n_estimators,
           'max_features': max_features,
           'max_depth': max_depth,
           'min_samples_split': min_samples_split,
           'min_samples_leaf': min_samples_leaf}

rf=RandomForestRegressor()

rf_random = RandomizedSearchCV(estimator = rf,
                           param_distributions = random_grid,
                           scoring='neg_mean_squared_error',
                           n_iter = 10,
                           cv = 5,
                           verbose=2,
                           random_state=42,
                           n_jobs = 1)
rf_random.fit(X_train,y_train)
rf_random.score(X_test,y_test)

我收到 -14881793274.345808

那么为什么准确度分数表现如此糟糕

【问题讨论】:

    标签: machine-learning regression random-forest cross-validation


    【解决方案1】:

    您的方法和假设存在一些问题:

    问题 1:您根本没有测量准确性

    您手头有一个回归任务。因此,准确度作为衡量正确分类的指标不能在这里应用。事实上,您在这两种情况下都没有测量准确性。

    问题 2:您正在比较不同的指标

    RandomForestRegressor 的 score() 函数执行以下操作:

    返回预测的决定系数R2。

    而RandomizedSearchCV 的score() 函数这样做:

    这将使用scoring 定义的分数,如果提供,则使用best_estimator_.score 方法。

    所以在第一种情况下,将为拟合的RandomForestRegressor 测量 R2。在第二种情况下,将返回最佳估计量的负均方误差 (MSE),因为您将 neg_mean_squared_error 指定为评分指标。

    结论:您的比较无效

    如果您想比较任何内容,请在您的 RandomizedSeachCV 中指定 scoring=r2 以比较 R2 的性能。或者使用mean_squared_error 计算拟合RandomForestRegressor 的MSE(但请记住RandomizedSearchCV 将返回取反的MSE)。

    重要提示

    您还应该注意,不能保证 RandomizedSearchCV 找到的最佳估计器确实会在您的测试集上表现更好,因为超参数仅在训练集上进行交叉验证,并且无法在测试集上取得性能考虑到。

    【讨论】:

    • 是的,完全明白了,谢谢你这么详细的解释。
    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 2020-08-10
    • 2016-08-22
    • 2021-10-21
    • 2018-04-16
    • 2012-09-09
    • 2015-09-29
    相关资源
    最近更新 更多