Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:11:13+00:00 2026-06-02T03:11:13+00:00

Using, amongst other sources, various posts here on Stackoverflow, I’m trying to implement my

  • 0

Using, amongst other sources, various posts here on Stackoverflow, I’m trying to implement my own PHP classier to classify tweets into a positive, neutral and negative class. Before coding, I need to get the process straigt. My train-of-thought and an example are as follows:

                                  p(class) * p(words|class)
 Bayes theorem: p(class|words) =  ------------------------- with
                                           p(words)

 assumption that p(words) is the same for every class leads to calculating
 arg max p(class) * p(words|class) with
 p(words|class) = p(word1|class) * p(word2|topic) * ... and
 p(class) = #words in class / #words in total and

                 p(word, class)                       1
 p(word|class) = -------------- = p(word, class) * -------- =
                    p(class)                       p(class)

 #times word occurs in class    #words in total  #times word occurs in class
 --------------------------- * --------------- = ---------------------------
       #words in total          #words in class        #words in class

 Example: 

 ------+----------------+-----------------+
 class | words          | #words in class |
 ------+----------------+-----------------+
 pos   | happy win nice | 3               |
 neu   | neutral middle | 2               |
 neg   | sad loose bad  | 3               |
 ------+----------------+-----------------+

 p(pos) = 3/8
 p(neu) = 2/8
 p(meg) = 3/8

 Calculate: argmax(sad loose)

 p(sad loose|pos) = p(sad|pos) * p(loose|pos) = (0+1)/3 * (0+1)/3 = 1/9
 p(sad loose|neu) = p(sad|neu) * p(loose|neu) = (0+1)/3 * (0+1)/3 = 1/9
 p(sad loose|neg) = p(sad|neg) * p(loose|neg) =     1/3 *     1/3 = 1/9

 p(pos) * p(sad loose|pos) = 3/8 * 1/9 = 0.0416666667
 p(neu) * p(sad loose|neu) = 2/8 * 1/9 = 0.0277777778
 p(neg) * p(sad loose|neg) = 3/8 * 1/9 = 0.0416666667 <-- should be 100% neg!

As you can see, I have “trained” the classifier with a positive (“happy win nice”), a neutral (“neutral middle”) and a negative (“sad loose bad”) tweet. In order to prevent problems of having probabilities of zero because of one word missing in all classes, I’m using LaPlace (or ädd one”) smoothing, see “(0+1)”.

I basically have two questions:

  1. Is this a correct blueprint for implementation? Is there room for improvement?
  2. When classifying a tweet (“sad loose”), it is expected to be 100% in class “neg” because it only contains negative words. The LaPlace smoothing is however making things more complicated: class pos and neg have an equal probability. Is there a workaround for this?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:11:14+00:00Added an answer on June 2, 2026 at 3:11 am

    There are two main elements to improve in your reasoning.

    First, you should improve your smoothing method:

    • When applying Laplace smoothing, it should be applied to all measurements, not just to those with zero denominator.
    • In addition, Laplace smoothing for such cases is usually given by (c+1)/(N+V), where V is the vocabulary size (e.g., see in Wikipedia).

    Therefore, using probability function you have defined (which might not be the most suitable, see below):

    p(sad loose|pos) = (0+1)/(3+8) * (0+1)/(3+8) = 1/121
    
    p(sad loose|neu) = (0+1)/(3+8) * (0+1)/(3+8) = 1/121
    
    p(sad loose|neg) = (1+1)/(3+8) * (1+1)/(3+8) = 4/121 <-- would become argmax
    

    In addition, a more common way of calculating the probability in the first place, would be by:

    (number of tweets in class containing term c) / (total number of tweets in class)
    

    For instance, in the limited trainset given above, and disregarding smoothing, p(sad|pos) = 0/1 = 0, and p(sad|neg) = 1/1 = 1. When the trainset size increases, the numbers would be more meaningful. e.g. if you had 10 tweets for the negative class, with ‘sad’ appearing in 4 of them, then p(sad|neg) would have been 4/10.

    Regarding the actual number outputted by the Naive Bayes algorithm: you shouldn’t expect the algorithm to assign actual probability to each class; rather, the category order is of more importance. Concretely, using the argmax would give you the algorithm’s best guess for the class, but not the probability for it. Assigning probabilities to NB results is another story; for example, see an article discussing this issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing some integration software for a client using amongst other things, C#,
I have a wrapper php class for mysqli which amongst other things needs to
i'm loosing my mind. Using ASP.NET in a GridView, amongst other controls, I have
I am using the python-ldap module to (amongst other things) search for groups, and
I am cloning a table row using JQuery and, amongst other things, the row
I'm trying to represent a class that, amongst other things, has a set of
I'm using a Windows Server 2008 x64 R2 machine as a development box. Amongst
I have a form that (amongst other things) contains 2 multi-select listboxes. Basically you
I'm building a custom ItemsControl in Silverlight that (amongst other things) allows items to
I'm in the process of creating a universal iOS app that, amongst other functions,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.