This paper illustrates the Python implementation method of naive Bayesian algorithm. Share to everyone for your reference. The implementation methods are as follows:
Advantages and disadvantages of naive Bayesian algorithm
Advantages: It is still valid in the case of less data, can deal with many kinds of problems
Disadvantage: Sensitive to the way the input d
to the overall probability of 0, there is a probability of multiplication after the value will be relatively small, take log convenient comparison.Test the Code Function section:def classifynb (Vec2classify, P0vec, P1vec, PClass1): p1 = SUM (vec2classify * P1vec) + log (pClass1) #element-wise mult p0 = SUM (vec2classify * P0vec) + log (1.0-PCLASS1) if p1 > P0: return 1 else: return 0def tes TINGNB (): listoposts,listclasses = Loaddataset () myvocablist = crea
Again, here's why the title is "using" instead of "Implementing":
First, professionals provide algorithms that are higher than our own algorithms, whether efficient or accurate.
Secondly, for those who are not good at maths, it is very painful to study a bunch of formulas in order to realize the algorithm.
Again, there is no need to "reinvent the wheel" unless the algorithms provided by others meet their own needs.
Below the point, do not understand the Bayesian algorithm can go to check the re
" | b= "No"), P (a1= "3" | b= "No");
P (a2= "1" | b= "No"), P (a2= "2" | b= "No"),...
2. The probability of classifying test data according to the Bayes theorem:
calculation: P (b= "hard" |test_a), p (b= "soft" |test_a), p (b= "no" |test_a), the
probability of the largest category, is the classification result of naive Bayesian classifier.
Code Implementation
Def train (dataset,labels): Uniquelabels =
In this paper, the Python implementation method of naive Bayesian algorithm is described. Share to everyone for your reference. The implementation method is as follows:
Advantages and disadvantages of naive Bayesian algorithm
Pros: Still effective with less data, can handle multiple categories of problems
Cons: Sensitive to the way the input data is prepared
App
)]=1 else:print "The word:%s is not in my vocabulary!" %word return returnvecdef TRAINNBC (trainsamples,traincategory): Numtrainsamp=len (Trainsamples) NumWords=len (train Samples[0]) pabusive=sum (traincategory)/float (numtrainsamp) #y =1 or 0 feature Count P0num=np.ones (numwords) P1NUM=NP.O NES (numwords) #y =1 or 0 category count P0numtotal=numwords p1numtotal=numwords for I in Range (Numtrainsamp): if Traincategory[i]==1:p0num+=trainsamples[i] P0numtotal+=sum (Trainsamples[i]) E
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.