首页 文章

通过GridSearchCV获取精确模型以进行召回优化

提问于
浏览
0

给定一个称为“m”的机器学习模型RBF SVC,我对gamma值执行了gridSearchCV,以优化召回 . 我想回答这个问题:“网格搜索应该找到最能优化回忆的模型 . 这个模型的召回比精度更好?”

所以我做了gridSearchCV:

grid_values = {'gamma': [0.001, 0.01, 0.05, 0.1, 1, 10, 100]}
grid_m_re = GridSearchCV(m, param_grid = grid_values, scoring = 'recall')
grid_m_re.fit(X_train, y_train)
y_decision_fn_scores_re = grid_m_re.decision_function(X_test) 

print('Grid best parameter (max. recall): ', grid_m_re.best_params_)
print('Grid best score (recall): ', grid_m_re.best_score_)

这告诉我最好的模型是γ= 0.001并且召回得分为1 .

我想知道如何获得这个模型的精度来获得这个模型的交易,因为GridSearchCV只有属性来获得它的优化 . ( [Doc sklearn.GridSearchCV][1]

2 回答

  • 1

    不确定是否有更简单/更直接的方法来获得这个,但这种方法还允许您捕获“最佳”模型以便以后使用:

    首先,你是否适合训练数据:

    grid_m_re = GridSearchCV(m, param_grid = grid_values, scoring = 'recall')
    grid_m_re.fit(X_train, y_train)
    

    一旦你__604401_最好'模型(由你在CV期间的 scoring 标准确定),然后使用它你想要:

    m_best = grid_m_re.best_estimator_
    

    在您的具体情况下:

    from sklearn.metrics import precision_score
    
    y_pred = m_best.predict(X_test)
    precision_score(y_test, y_pred)
    
  • 0

    如果不同时优化C和gamma,则可以轻松过度配合 .

    如果您在X轴上绘制带有C的SVC,在y轴上绘制伽马并将其调用为颜色you get some kind of V-Shape, see here

    因此,如果您进行网格搜索,则可以同时更好地优化C和gamma .

    问题是通常你会得到小C值的最佳结果,并且在那个区域V形有它的尖端:不是很大而且难以击中 .

    我最近用过:

    make a random grid of 10 points
          every point contains C, gamma, direction, speed
    cut the dataset with stratifiedShuffleSplit
    fit & estimate score with cross validation 
    
    repeat:
      kill the worst two points
      the best two points spawn a kid
      move every point in its direction with just a little bit of random, 
      fit & estimate score with cross validation 
         (if a point notice it goes downward, turn around and half speed)
      until break criterion is hit
    

    工作就像一个魅力 .

    我使用特征空间中的最大距离除以4作为初始速度,方向具有pi / 4的最大随机值

    嗯,交叉验证有点贵 .

    Cleptocreatively inspired by this paper.

    ...和另一个编辑:

    我在循环中使用10到20个循环来获得完美的点 . 如果您的数据集太大而无法进行多次拟合,请为前几次培训制作代表性子集...

相关问题