【问题标题】:How does sklearn's MLP predict_proba function work internally?sklearn 的 MLP predict_proba 函数在内部是如何工作的?
【发布时间】:2020-08-06 19:48:45
【问题描述】:

我试图了解sklearn's MLP Classifier 如何为其predict_proba 函数检索结果。

网站简单列出:

概率估计

而许多其他人,例如logistic regression,有更详细的答案: 概率估计。

所有类的返回估计值按类标签排序。

对于 multi_class 问题,如果 multi_class 设置为“多项式” softmax函数用于找到每个的预测概率 班级。否则使用一对一的方法,即计算概率 使用逻辑函数假设它是正的每个类。 并在所有类中标准化这些值。

其他模型类型也有更多细节。以support vector machine classifier

为例

而且还有this very nice Stack Overflow post 对此进行了深入的解释。

计算 X 中样本可能结果的概率。

模型需要在训练时计算概率信息 时间:适合属性概率设置为真。

其他示例

Random Forest:

预测 X 的类别概率。

输入样本的预测类别概率计算为 森林中树木的平均预测类别概率。这 一棵树的类概率是 同一类在一片叶子中。

Gaussian Process Classifier:

我希望了解与上述帖子相同的内容,但对于 MLPClassifierMLPClassifier 在内部是如何工作的?

【问题讨论】:

    标签: python machine-learning scikit-learn probability mlp


    【解决方案1】:

    查看source code,我发现:

    def _initialize(self, y, layer_units):
    
        # set all attributes, allocate weights etc for first call
        # Initialize parameters
        self.n_iter_ = 0
        self.t_ = 0
        self.n_outputs_ = y.shape[1]
    
        # Compute the number of layers
        self.n_layers_ = len(layer_units)
    
        # Output for regression
        if not is_classifier(self):
            self.out_activation_ = 'identity'
        # Output for multi class
        elif self._label_binarizer.y_type_ == 'multiclass':
            self.out_activation_ = 'softmax'
        # Output for binary class and multi-label
        else:
            self.out_activation_ = 'logistic'
    

    似乎 MLP 分类器使用逻辑函数进行二元分类,使用 softmax 函数进行多标签分类,以构建输出层。这表明网络的输出是一个概率向量,网络在此基础上推导出预测。

    如果我查看predict_proba 方法:

    def predict_proba(self, X):
        """Probability estimates.
        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            The input data.
        Returns
        -------
        y_prob : ndarray of shape (n_samples, n_classes)
            The predicted probability of the sample for each class in the
            model, where classes are ordered as they are in `self.classes_`.
        """
        check_is_fitted(self)
        y_pred = self._predict(X)
    
        if self.n_outputs_ == 1:
            y_pred = y_pred.ravel()
    
        if y_pred.ndim == 1:
            return np.vstack([1 - y_pred, y_pred]).T
        else:
            return y_pred
    

    这确认了 softmax 或逻辑作为输出层的激活函数的作用,以获得概率向量。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 2022-12-29
      • 2013-02-13
      • 2011-05-07
      • 2018-03-07
      • 2015-09-18
      • 2019-04-21
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多