How to specify the prior probability for scikit-learn s Naive Bayes

0 votes

I'm using the scikit-learn machine learning library (Python) for a machine learning project. One of the algorithms I'm using is the Gaussian Naive Bayes implementation. One of the attributes of the GaussianNB() function is the following:

class_prior_ : array, shape (n_classes,)

I want to alter the class prior manually since the data I use is very skewed and the recall of one of the classes is very important. By assigning a high prior probability to that class the recall should increase.

However, I can't figure out how to set the attribute correctly.

This is my code:

gnb = GaussianNB()
gnb.class_prior_ = [0.1, 0.9], yTrain)
yPredicted = gnb.predict(data.XTest)

I figured this was the correct syntax and I could find out which class belongs to which place in the array by playing with the values but the results remain unchanged. Also no errors were given.

What is the correct way of setting the attributes of the GaussianNB algorithm from scikit-learn library?

Apr 5, 2022 in Machine Learning by Dev
• 6,000 points

1 answer to this question.

0 votes

In GaussianNB, there is a mechanism to set prior probabilities. It's called 'priors,' and it's a parameter that you can use. See the following documentation: "Parameters: priors: array-like, (n classes,) shape The classes' prior probability. The priors are not adjusted according to the data unless otherwise specified." As an example, consider the following:

from sklearn.naive_bayes import GaussianNB
# minimal dataset
X = [[1, 0], [1, 0], [0, 1]]
y = [0, 0, 1]
# use empirical prior, learned from y
gauss = GaussianNB()
print (,y).predict([1,1]))
print (gauss.class_prior_)

>>>[ 0.66666667  0.33333333]

However, if you adjust the prior probabilities, you'll get a different result, which I believe is what you're looking for.

# use custom prior to make 1 more likely
gauss = GaussianNB(priors=[0.1, 0.9]),y).predict([1,1])

You can't set class prior with the GaussianNB() function in scikit-learn. If you look at the documentation online, you'll notice that. Instead of arguments, class prior_ is an attribute. You can access the class prior_ property after fitting the GaussianNB().

Elevate Your Expertise with Our Machine Learning Certification Program!

answered Apr 7, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
1 answer

How to calculate ctc probability for given input and expected output?

The loss for a batch is defined ...READ MORE

answered Mar 17, 2022 in Machine Learning by Dev
• 6,000 points
0 votes
1 answer

How to simulate first passage time probability in python for a random walk?

To begin with, you're now computing fp ...READ MORE

answered Apr 5, 2022 in Machine Learning by Dev
• 6,000 points
+1 vote
1 answer

how to analysis the heatmap to find the correlation

Hi @Vikas, there are 5 simple steps ...READ MORE

answered Sep 30, 2019 in Machine Learning by Vishal
0 votes
1 answer

How to save classifier to disk in scikit-learn?

Hi@akhtar, Classifiers are just objects that can be ...READ MORE

answered Jul 14, 2020 in Machine Learning by MD
• 95,440 points
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,130 views
0 votes
1 answer

Leela Chess Zero: how large is the probability vector in the output layer?

The next move's probability vector (called the ...READ MORE

answered Mar 9, 2022 in Machine Learning by Nandini
• 5,480 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP