【问题标题】:how to create my own scoring in GridsearchCV?如何在 GridsearchCV 中创建自己的评分?
【发布时间】:2019-01-13 03:16:32
【问题描述】:

我想在 GridsearchCV 中创建自己的评分,下面是我的代码: 当我运行这些代码时,最后一个短语发生错误:grid_x.fit(train_x_pca, x_ref)。当我使用像“r2”这样的内置评分时 grid_x=GridSearchCV(nnw_model, para_grid, scoring='r2'),它有效。 我自己的得分定义应该有问题。

nnw_model=MLPRegressor(hidden_layer_sizes=(15,), activation='tanh', \
            solver='lbfgs', learning_rate='adaptive', max_iter=1000,\
            learning_rate_init=0.01, alpha=0.01)  

para_grid=[{'activation': ['tanh', 'logistic', 'relu'], 'hidden_layer_sizes':\
                 [(15,), (17,), (19,), (21,)], 'learning_rate_init':\
                 [0.01,0.001,0.0001]}]


x_ref=ocd_ref['tilt_x']
def Rsq_x_cal(train_x_pca, x_ref):
        nnw_model_x.fit(train_x_pca, x_ref)
        train_x_out=nnw_model_x.predict(train_x_pca)
        metric_x=linregress(train_x_out, x_ref)
        rsq_x=metric_x[2]**2
        return rsq_x
rsq_x_value=make_scorer(Rsq_x_cal, greater_is_better=True)
grid_x=GridSearchCV(nnw_model, para_grid, scoring=rsq_x_value)
grid_x.fit(train_x_pca, x_ref)

【问题讨论】:

    标签: python-3.x grid-search scoring


    【解决方案1】:

    scoring 以实际输出和预测输出作为参数的可调用对象并返回单个数字。

    类似这样的:

    def my_scoring(y_actual, y_predicted):
        score = do_something_on_input()
        return score
    

    在您的自定义方法中,您将特征 (train_x_pca) 和标签 (x_ref) 作为输入参数传递。这就是错误的来源。 GridSearchCV 不会将训练数据传递给您的方法。它将已经预测的数据传递给它。所以你的这两行是不必要的:

        nnw_model_x.fit(train_x_pca, x_ref)
        train_x_out=nnw_model_x.predict(train_x_pca)
        ...
    

    这样做:

    # Notice that I changed the order of x_ref here from your 
    # And deleted those two unwanted lines
    def Rsq_x_cal(x_ref, train_x_out):
        metric_x=linregress(train_x_out, x_ref)
        rsq_x=metric_x[2]**2
        return rsq_x
    

    现在假设linregress 返回实际数据和预测数据之间的分数,上面的代码应该可以工作

    【讨论】:

      猜你喜欢
      • 2018-04-19
      • 2019-03-03
      • 2019-05-03
      • 2021-07-11
      • 2018-05-14
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多