sklearn NBClassifier收到了意外的关键字参数“ var_smoothing”

时间:2019-06-16 14:25:29

标签: python-3.x scikit-learn naivebayes

我在我的代码中回复此示例:

<iframe srcdoc="
    <html>
        <head>
            <title>Example Page!</title>
            <link rel='stylesheet' type='text/css' href='http://example.com/mystyle.css'>
        </head>
        <body>
            <p class='main'>Here goes the text.
            </p>

            <script src='http://example.com/js/superscript.js'>
        </body>
    </html>">
</iframe>

显示在这里:GaussianNB documentation

我明白了

import numpy as np
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([1, 1, 1, 2, 2, 2])
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf.fit(X, Y)
GaussianNB(priors=None, var_smoothing=1e-09)
print(clf.predict([[-0.8, -1]]))

sklearn的版本是

GaussianNB(priors=None, var_smoothing=1e-09)
TypeError: __init__() got an unexpected keyword argument 'var_smoothing'

有人知道发生了什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

当前sci-kit学习版本为 0.21.2

我在sklearn版本0.19.2中对此进行了测试。未为var_smoothing方法定义参数GaussianNB

您可以通过使用文档进行检查

from sklearn.naive_bayes import GaussianNB
help(GaussianNB)

# Result
Help on class GaussianNB in module sklearn.naive_bayes:

class GaussianNB(BaseNB)
 |  Gaussian Naive Bayes (GaussianNB)
...
...
 |  Parameters
 |  ----------
 |  priors : array-like, shape (n_classes,)
 |      Prior probabilities of the classes. If specified the priors are not
 |      adjusted according to the data.
 |  
 |  Attributes
...
...

您可以升级到scikit Learn的最新版本,也可以删除该参数。