Does sklearn set_params take exactly 1 argument?

I am trying to use the SkLearn Bayesian 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 am trying to use this code:

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

And we get:

 ValueError: Invalid parameter sigma_ for estimator GaussianNB 

Is this an invalid parameter name or value?

+8
python scikit-learn
source share
5 answers

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

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

+4
source share

I just stumbled upon this, so here is the solution for a few arguments from the 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) 
+13
source share

The documentation says 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 this in form your_param = 'sigma__0.2'

+2
source share

The problem is that GaussianNB has only one parameter and 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 GaussianNB class, and the set_params() and get_params() methods cannot access them.

You can manipulate the attributes of sigma and theta by submitting some priors to GaussianNB or by setting it to a specific training set.

0
source share

sigma_ - attribute of the instance that is computed during training. You are probably not going 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, you can change the value of sigma_ . This may affect forecasting results.

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

All Articles