【问题标题】:Is it possible to set a "threshold" for a scikit-learn ensemble classifier?是否可以为 scikit-learn 集成分类器设置“阈值”?
【发布时间】:2019-01-23 20:46:54
【问题描述】:

我有一个由 200 个单独的 SVM 分类器组成的 VotingClassifier。默认情况下,此分类器使用多数规则投票。我想设置一个自定义阈值 - 仅当 60% 或更多的 SVM 分类器相同时才进行分类。

如果 59% 的 SVM 分类器具有相同的分类,我不希望集成模型进行分类。

我没有看到为 VotingClassifier 对象执行此操作的参数,但我认为它必须在 scikit-learn 中的某个地方是可能的。我应该使用不同的集成类吗?

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    根据您在页面末尾获得的方法,最简单的解决方案是使用转换方法:

    def transform(self, X):
            """Return class labels or probabilities for X for each estimator.
            Parameters
            ----------
            X : {array-like, sparse matrix}, shape = [n_samples, n_features]
                Training vectors, where n_samples is the number of samples and
                n_features is the number of features.
            Returns
            -------
            If `voting='soft'` and `flatten_transform=True`:
              array-like = (n_classifiers, n_samples * n_classes)
              otherwise array-like = (n_classifiers, n_samples, n_classes)
                Class probabilities calculated by each classifier.
            If `voting='hard'`:
              array-like = [n_samples, n_classifiers]
                Class labels predicted by each classifier.
            """
    

    只需执行一个简单的函数,将一行的总和除以 SVM 的数量并应用您的阈值:

    if(ratio>threshold):
         return 1
    elif(ratio<(1-threshold)):
         return 0
    else:
         #we don't make the prediction
         return -1
    

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 2017-11-16
      • 2021-06-20
      • 2013-11-27
      • 2017-01-02
      • 2020-05-13
      • 2015-08-23
      • 2015-05-09
      • 2019-12-05
      相关资源
      最近更新 更多