scikitlearn中二元分类的权维数和偏差

2024-06-07 04:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我们在使用MLP分类器sklearn.neural_网络并对分类器产生的偏差和权重进行了分析。在

当我们有二进制数据时会有一个问题,即只允许两个值。最后一层的尺寸似乎是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)

输出:

^{pr2}$

从数学上讲,你能做到这一点是有道理的,我假设这是一个优化,但我们失去了泛化能力。 有没有一种简单的方法可以阻止scikit这样做?我们如何转换权重和偏差,使它们的尺寸与值的数量相匹配?在


Tags: 数量分类器尺寸nparraymatrixfitnote
1条回答
网友
1楼 · 发布于 2024-06-07 04:23:35

神经网络的类标签需要一个热编码,而这是在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)

输出:

^{pr2}$

关于如何执行这个预处理步骤的更多信息,我将查看scikit中的OneHotEncoderdocumentation。在

相关问题 更多 >