我想做什么?
我正在尝试使用 GaussianNB 分类器训练具有 10 个标签的数据集,但在调整我的 gaussianNB 先验参数时,我收到此错误:-
文件“/home/mg/anaconda2/lib/python2.7/site-packages/sklearn/naive_bayes.py”,第 367 行,位于 _partial_fit raise ValueError('先验之和应为 1。') ValueError:先验之和应为 1。
代码:- clf = GaussianNB(先验 = [0.08, 0.14, 0.03, 0.16, 0.11, 0.16, 0.07, 0.14, 0.11, 0.0])
你可以看到总和显然是1,但它向我显示了这个错误,你能指出错误吗?
最佳答案
这看起来是 sklearn 中一个非常糟糕的设计决策,因为他们正在做通常的不比较 float 的事情( what every computer scientist should know about floating-point arithmetic ),这让我感到惊讶(因为 sklearn 通常是这样的)高质量代码)!
(尽管使用了列表,但我没有看到你有任何错误的用法。文档调用了一个数组,而不是像许多其他情况下那样的数组,但是他们的代码正在做尽管如此,数组转换)
if self.priors is not None:
priors = np.asarray(self.priors)
# Check that the provide prior match the number of classes
if len(priors) != n_classes:
raise ValueError('Number of priors must match number of'
' classes.')
# Check that the sum is 1
if priors.sum() != 1.0:
raise ValueError('The sum of the priors should be 1.')
# Check that the prior are non-negative
if (priors < 0).any():
raise ValueError('Priors must be non-negative.')
self.class_prior_ = priors
else:
# Initialize the priors to zeros for each class
self.class_prior_ = np.zeros(len(self.classes_),
dtype=np.float64)
所以:
- 您给出一个列表,但他们的代码将创建一个 numpy 数组
- 因此
np.sum()
将用于求和 - 在求和中可能存在与 fp-math 相关的数字错误,就像您的情况一样
- 从技术上讲,您的总和!= 1.0;但非常接近!
- fp-comparison
x == 1.0
被认为是不好的!- numpy 带来了
np.isclose()
这是执行此操作的常用方法
- numpy 带来了
演示:
import numpy as np
priors = np.array([0.08, 0.14, 0.03, 0.16, 0.11, 0.16, 0.07, 0.14, 0.11, 0.0])
my_sum = np.sum(priors)
print('my_sum: ', my_sum)
print('naive: ', my_sum == 1.0)
print('safe: ', np.isclose(my_sum, 1.0))
输出:
('my_sum: ', 1.0000000000000002)
('naive: ', False)
('safe: ', True)
编辑:
由于我认为这段代码不好,所以我发布了一个问题 here您可以按照这些内容查看他们是否遵守。
numpy.random.sample()也采用这样的向量,实际上也在采用 fp 安全方法(数值更稳定的求和 + epsilon 检查;但不使用 np.isclose()
),如 here 所示。 .
关于python-2.7 - 高斯NB :- ValueError: The sum of the priors should be 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45896410/