Python Sklearn set_params 只需要 1 个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27122757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:27:10  来源:igfitidea点击:

Sklearn set_params takes exactly 1 argument?

pythonscikit-learn

提问by Leonid

I'm trying to use SkLearn Bayes classification.

我正在尝试使用 SkLearn Bayes 分类

 gnb = GaussianNB()
 gnb.set_params('sigma__0.2')
 gnb.fit(np.transpose([xn, yn]), y)

But I get:

但我得到:

set_params() takes exactly 1 argument (2 given)


now I try to use this code:

现在我尝试使用此代码:

gnb = GaussianNB()
arr = np.zeros((len(labs),len(y)))
arr.fill(sigma)
gnb.set_params(sigma_ = arr)

And get:

并得到:

ValueError: Invalid parameter sigma_ for estimator GaussianNB

Is it wrong parameter name or value?

是错误的参数名称或值吗?

采纳答案by Mezgrman

set_params()takes only keyword arguments, as can be seen in the documentation. It is declared as set_params(**params).

set_params()只接受关键字参数,如文档中所示。它被声明为set_params(**params).

So, in order to make it work, you need to call it with keyword arguments only: gnb.set_params(some_param = 'sigma__0.2')

因此,为了使其工作,您只需要使用关键字参数调用它: gnb.set_params(some_param = 'sigma__0.2')

回答by Salvador Dali

It is written in documentation that the syntax is:

它写在文档中,语法是:

set_params(**params)

设置参数(**参数)

These two stars mean that you need to give keyword arguments (read about it here). So you need to do it in the form your_param = 'sigma__0.2'

这两颗星意味着您需要提供关键字参数(在此处阅读)。所以你需要在form your_param = 'sigma__0.2'

回答by Kam Sen

I just stumbled upon this, so here is a solution for multiple arguments from a dictionary:

我只是偶然发现了这一点,所以这里是字典中多个参数的解决方案:

from sklearn import svm
params_svm = {"kernel":"rbf", "C":0.1, "gamma":0.1, "class_weight":"auto"}
clf = svm.SVC()
clf.set_params(**params_svm)

回答by Pedro Baracho

The problem here is that GaussianNBhas only one parameter and that is priors.

这里的问题是它GaussianNB只有一个参数,即priors.

From the documentation

文档

 class sklearn.naive_bayes.GaussianNB(priors=None)

The sigmaparameter you are looking for is, in fact, an attribute of the class GaussianNB, and cannot be accessed by the methods set_params()and get_params().

sigma您要查找的参数实际上是 GaussianNB 类的一个属性,无法通过方法set_params()和访问get_params()

You can manipulate sigmaand thetaattributes, by feeding some Priorsto GaussianNB or by fitting it to a specific training set.

您可以通过向 GaussianNB提供一些数据或将其拟合到特定训练集来操作sigmatheta属性Priors

回答by nobar

sigma_is an instance attribute which is computed during training. You probably aren't intended to modify it directly.

sigma_是在训练期间计算的实例属性。您可能不打算直接修改它。

from sklearn.naive_bayes import GaussianNB
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])

gnb = GaussianNB()
print gnb.sigma_

Output:

输出:

AttributeError: 'GaussianNB' object has no attribute 'sigma_'

More code:

更多代码:

gnb.fit(X,y) ## training
print gnb.sigma_

Output:

输出:

array([[ 0.66666667,  0.22222223],
       [ 0.66666667,  0.22222223]])

After training, it is possible to modify the sigma_value. This might affect the results of prediction.

训练后,可以修改该sigma_值。这可能会影响预测结果。

gnb.sigma_ = np.array([[1,1],[1,1]])