How to get both MSE and R2 from a sklearn GridSearchCV?

前端 未结 2 1716
春和景丽
春和景丽 2021-01-06 16:54

I can use a GridSearchCV on a pipeline and specify scoring to either be \'MSE\' or \'R2\'. I can then access gridsearchcv._best_score

2条回答
  •  猫巷女王i
    2021-01-06 17:14

    This is unfortunately not straightforward right now with GridSearchCV, or any built in sklearn method/object.

    Although there is talk of having multiple scorer outputs, this feature will probably not come soon.

    So you will have to do it yourself, there are several ways:

    1) You can take a look at the code of cross_val_score and perform the cross validation loop yourself, calling the scorers of interest once each fold is done.

    2) [not recommended] You can also build your own scorer out of the scorers you are interested in and have them output the scores as an array. You will then find yourself with the problem explained here: sklearn - Cross validation with multiple scores

    3) Since you can code your own scorers, you could make a scorer that outputs one of your scores (the one by which you want GridSearchCV to make decisions), and which stores all the other scores you are interested in in a separate place, which may be a static/global variable, or even a file.

    Number 3 seems the least tedious and most promising:

    import numpy as np
    from sklearn.metrics import r2_score, mean_squared_error
    secret_mses = []
    
    def r2_secret_mse(estimator, X_test, y_test):
        predictions = estimator.predict(X_test)
        secret_mses.append(mean_squared_error(y_test, predictions))
        return r2_score(y_test, predictions)
    
    X = np.random.randn(20, 10)
    y = np.random.randn(20)
    
    from sklearn.cross_validation import cross_val_score
    from sklearn.linear_model import Ridge
    
    r2_scores = cross_val_score(Ridge(), X, y, scoring=r2_secret_mse, cv=5)
    

    You will find the R2 scores in r2_scores and the corresponding MSEs in secret_mses.

    Note that this can become messy if you go parallel. In that case you would need to write the scores to a specific place in a memmap for example.

提交回复
热议问题