【问题标题】:Grid-Search finding Parameters for AUCAUC 的网格搜索查找参数
【发布时间】:2016-10-07 23:31:52
【问题描述】:

我正在尝试为我的 SVM 找到参数,这给了我最好的 AUC。但我在 sklearn 中找不到 AUC 的任何评分函数。有人有想法吗?这是我的代码:

    parameters = {"C":[0.1, 1, 10, 100, 1000], "gamma":[0.1, 0.01, 0.001, 0.0001, 0.00001]}
    clf = SVC(kernel = "rbf")
    clf = GridSearchCV(clf, parameters, scoring = ???)
    svr.fit(features_train , labels_train)
    print svr.best_params_

那我可以用来做什么???获得高 AUC 分数的最佳参数?

【问题讨论】:

    标签: python scikit-learn svm grid-search


    【解决方案1】:

    我没有尝试过,但我相信你想使用sklearn.metrics.roc_auc_score

    问题在于它不是模型记分器,因此您需要构建一个。 比如:

    from sklearn.metrics import roc_auc_score
    
    def score_auc(estimator, X, y):
        y_score = estimator.predict_proba(X)  # You could also use the binary predict, but probabilities should give you a more realistic score.
        return roc_auc_score(y, y_score)
    

    并将此函数用作 GridSearch 中的评分参数。

    【讨论】:

    • 谢谢,我喜欢你的想法,但如果我这样做:svr = GridSearchCV(svr, parameters, scoring = score_auc(svr, features_train, labels_train))it 会导致:AttributeError:predict_proba 在probability=False 时不可用。如果我将其设置为 true,则会出现另一个错误。
    • 只是做一个svr = GridSearchCV(svr, parameters, scoring=score_auc),你不应该调用这个函数,把它传递给搜索。如果predict_proba 给您带来问题,请使用常规predict 评分。
    • 感觉这会将“score_auc”传递给训练数据——如果我们想在交叉验证数据上对其进行评分怎么办?
    • 对于某些 SVM 模型,您需要在初始化它们时显式设置超参数“probability=True”,以获得概率预测。
    【解决方案2】:

    你可以简单地使用:

    clf = GridSearchCV(clf, parameters, scoring='roc_auc')
    

    【讨论】:

    • 所以如果我打印出 svr.best_score_ 它的 auc?因为我试图这样计算它:#ROC false_positive_rate, true_positive_rate, thresholds = roc_curve(labels_test, labels_predicted) roc_auc = auc(false_positive_rate, true_positive_rate) print roc_auc 但它显示我的 auc 低于最好的分数
    • 最佳分数对应于训练过程中每个折叠的最佳平均值roc_auc。人们会期望在测试集上看到较低的分数。
    【解决方案3】:

    您可以自己制作任何得分手:

    from sklearn.metrics import make_scorer
    from sklearn.metrics import roc_curve, auc
    
    # define scoring function 
     def custom_auc(ground_truth, predictions):
         # I need only one column of predictions["0" and "1"]. You can get an error here
         # while trying to return both columns at once
         fpr, tpr, _ = roc_curve(ground_truth, predictions[:, 1], pos_label=1)    
         return auc(fpr, tpr)
    
    # to be standart sklearn's scorer        
     my_auc = make_scorer(custom_auc, greater_is_better=True, needs_proba=True)
    
     pipeline = Pipeline(
                    [("transformer", TruncatedSVD(n_components=70)),
                    ("classifier", xgb.XGBClassifier(scale_pos_weight=1.0, learning_rate=0.1, 
                                    max_depth=5, n_estimators=50, min_child_weight=5))])
    
     parameters_grid = {'transformer__n_components': [60, 40, 20] }
    
     grid_cv = GridSearchCV(pipeline, parameters_grid, scoring = my_auc, n_jobs=-1,
                                                            cv = StratifiedShuffleSplit(n_splits=5,test_size=0.3,random_state = 0))
     grid_cv.fit(X, y)
    

    欲了解更多信息,请在此处查看:sklearn make_scorer

    【讨论】:

      【解决方案4】:

      使用下面的代码,它将为您提供所有参数列表

      import sklearn
      
      sklearn.metrics.SCORERS.keys()
      

      选择您要使用的适当参数

      在您的情况下,下面的代码将起作用

      clf = GridSearchCV(clf, parameters, scoring = 'roc_auc')
      

      【讨论】:

        猜你喜欢
        • 2016-04-12
        • 2020-10-06
        • 2020-07-28
        • 2023-03-09
        • 2016-07-12
        • 2020-09-01
        • 2019-12-23
        • 2019-07-18
        • 2019-10-15
        相关资源
        最近更新 更多