Leonid
Leonid

Reputation: 1157

Sklearn set_params takes exactly 1 argument?

I'm trying to use SkLearn Bayes classification.

 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?

Upvotes: 36

Views: 51117

Answers (5)

Brent Bradburn
Brent Bradburn

Reputation: 54859

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

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.

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

Upvotes: 3

Pedro Baracho
Pedro Baracho

Reputation: 357

The problem here is that GaussianNB has only one parameter and that is priors.

From the documentation

 class sklearn.naive_bayes.GaussianNB(priors=None)

The sigma parameter 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().

You can manipulate sigma and theta attributes, by feeding some Priors to GaussianNB or by fitting it to a specific training set.

Upvotes: 2

Kam Sen
Kam Sen

Reputation: 1148

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)

Upvotes: 71

Salvador Dali
Salvador Dali

Reputation: 222461

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'

Upvotes: 4

Mezgrman
Mezgrman

Reputation: 886

set_params() takes only keyword arguments, as can be seen in the documentation. It is declared as 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')

Upvotes: 24

Related Questions