繁体   English   中英

scikit-learn中二进制分类的权重和偏差量

[英]Dimension of weights and bias for binary classification in scikit-learn

我们在sklearn.neural_network中使用MLPClassifier,并对分类器生成的偏差和权重进行一些分析。

当我们拥有二进制数据时存在一个问题,即仅允许两个值。 然后,似乎最后一层的尺寸为1,而不是2。在其他情况下,偏差和权重的形状似乎总是与输出值的数量匹配。

binary_classifier= MLPClassifier().fit(np.matrix([[0.], [1.]]), np.array([0,1]))
other_classifier = MLPClassifier().fit(np.matrix([[0.], [1.], [2]]), np.array([0,1,2]))

# Note that the dimension below is 1
print(binary_classifier.intercepts_[-1].shape, binary_classifier.coefs_[-1].shape)
# Note that the dimension below is 3
print(other_classifier.intercepts_[-1].shape, other_classifier.coefs_[-1].shape)

输出:

(1,) (100, 1)
(3,) (100, 3)

从数学上讲,您可以执行此操作,并且我认为这是一种优化,但是我们失去了概括性。 有没有简单的方法可以防止scikit这样做? 否则我们如何转换权重和偏差,以使它们的尺寸与值的数量匹配?

神经网络的类标签需要一种热编码,这是在MLPClassifier 如果您显式传递一个热编码目标,那么您将获得所需的输出:

#Now one hot encoded
binary_classifier= MLPClassifier().fit(np.matrix([[0.], [1.]]), np.array([[1, 0], [0, 1]]))
# NOT encoded
other_classifier = MLPClassifier().fit(np.matrix([[0.], [1.], [2]]), np.array([0,1,2]))

# Note that the dimension below is 2
print(binary_classifier.intercepts_[-1].shape, binary_classifier.coefs_[-1].shape)
# Note that the dimension below is 3
print(other_classifier.intercepts_[-1].shape, other_classifier.coefs_[-1].shape)

输出继电器:

((2,), (100, 2))
((3,), (100, 3))

有关如何执行此预处理步骤的更多信息,请查看scikit中的OneHotEncoder 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM