我试图理解sklearn
的MLP分类器是如何获取其predict_proba
函数的结果的。
网站上只是简单地列出了:
概率估计
而其他许多模型,如逻辑回归,则有更详细的解释:概率估计。
返回的所有类的估计值按类标签的顺序排列。
对于多类问题,如果multi_class设置为“multinomial”,则使用softmax函数来查找每个类的预测概率。否则使用一对多的方法,即使用逻辑函数计算假设每个类为正类的概率,并在所有类中对这些值进行归一化处理。
其他类型的模型也有更详细的描述。例如,支持向量机分类器
还有这个非常好的Stack Overflow帖子,它详细解释了这一点。
计算X中样本可能结果的概率。
模型需要在训练时计算概率信息:使用属性probability设置为True进行拟合。
其他示例
随机森林:
预测X的类概率。
输入样本的预测类概率被计算为森林中树的预测类概率的平均值。单棵树的类概率是一个叶子中相同类的样本的分数。
我想了解与上述帖子相同的内容,但针对MLPClassifier
。MLPClassifier
的内部工作原理是什么?
回答:
在查看源代码后,我发现了以下内容:
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或逻辑函数作为输出层的激活函数,以获得概率向量。
希望这对你有帮助。