Live data from Hacker News

A practical explanation of a Naive Bayes classifier

monkeylearn.com

11–20 of 42 posts

Re: A practical explanation of a Naive Bayes classifier

#11
I would add that another practical aspect about Naive Bayes classifiers is that you can make use of the conditional probabilities for each feature that contributes to the predictions. That gives you some introspection on how the model is working and it's useful when "debugging" classifiers by finding features that should/shouldn't be used.

https://monkeylearn.com/blog/how-to-create-text-classifiers-...

Re: A practical explanation of a Naive Bayes classifier

#12
post #9

A practical issue for Naive Bayes that also infects linear models is bias w.r.t. document length. Typically when you are detecting a rare, relatively compact class such as sports articles (or spam) you will tend to have a strongly negative prior, many positive features, and few negative ones. As a consequence, as the length of your text increases, not only does the variance of your prediction increase, but the mean t…

Is there some way to normalize the document length?

Re: A practical explanation of a Naive Bayes classifier

#13
post #2

Considering the relative ease of implementation, classification accuracy with smaller datasets, and computational efficiency of Naive Bayes classifiers, I am surprised that they are not mentioned as often as other machine learning competitors, such as random forest. Are there major drawbacks to Naive Bayes classifiers? Is it just that they aren't as accurate on large datasets?

many competition solutions use stacking, where you use the probabilities of several classifiers are used instead of their predictions.

Since naive bayes makes an independence assumption, and most competitions have features that are highly correlated, its probability / confidence of its prediction is probably a lot more useless than other classifiers.

besides that, nb doesn't do feature selection, regularization, and interactions like many tree based algorithms (such as XGBoost) do out of the box.

interactions is a big one for me. With NB, you have to add an interaction feature by multiplying two together, but in creating the feature you must steal some probability away from the other stuff, and finding a good interaction among many features is hard. The tree structure of random forests naturally captures that

edit: to your last point, nb does worse on larger data sets. when you dont have a lot of data, you have to make larger assumptions. NB makes a big independence assumption, thus it does well on very small data sets but falls short on large ones

Re: A practical explanation of a Naive Bayes classifier

#14
post #12
post #9

A practical issue for Naive Bayes that also infects linear models is bias w.r.t. document length. Typically when you are detecting a rare, relatively compact class such as sports articles (or spam) you will tend to have a strongly negative prior, many positive features, and few negative ones. As a consequence, as the length of your text increases, not only does the variance of your prediction increase, but the mean t…

Is there some way to normalize the document length?

Lots of reasonable hacks.

1. Use only the beginning of the document, as that's probably the most important part anyways, and it's fast.

2. Divide the sum of your feature scores by sqrt(n) to give it constant variance, and hopefully keep it comparable with your prior.

3. Split the doc into reasonably sized chunks, and average their scores rather than adding them.

Re: A practical explanation of a Naive Bayes classifier

#15

The examples other people are using are fairly narrow. I would like to substantiate that text categorization via naive bayes classifier is surprisingly accurate and simple. This paper[1] uses ngrams and a simple out of place measure to compare articles against different verticals and often sees greater than 99% accuracy for relatively small blocks of text. The out of place measure also adds a penalty to features not…

From experience, I suspect NB does well on text primarily for two reasons:

(1) High dimensionality - this is also the reason why SVMs with linear kernels do so well on text (NBs find a linear boundary too - but a different linear boundary than linear-kernel SVMs)

(2) this reason applies to certain cases only: text where "token" based estimates are sufficiently discriminatory. For ex take a dataset that has two kinds of documents - one talking about product A and the other talking about product B. And you want to label the documents based on which product they're talking about. Here, just noticing if the term "A" or "B" appears in the document is good enough for classification. You don't have to have powerful models that infer "connotations" of words based on context. NB will do well here, esp if bundled with a feature selection technique that weeds out noisy features (like sthg based on Normalized Mutual Information)

Re: A practical explanation of a Naive Bayes classifier

#16

I would add that another practical aspect about Naive Bayes classifiers is that you can make use of the conditional probabilities for each feature that contributes to the predictions. That gives you some introspection on how the model is working and it's useful when "debugging" classifiers by finding features that should/shouldn't be used. https://monkeylearn.com/blog/how-to-create-text-classifiers-...

A side note here: the classification probabilities NB produces are not very accurate and usually need some correction using a process called "calibration".

Re: A practical explanation of a Naive Bayes classifier

#17
I created a small program that finds the best sub-reddit given any title text[1] using this algorithm.

I'm a total ML noob but it was a interesting project and the results were pretty accurate.

I basically used reddit's Bigquery data for the dataset (it's huge!). If you need a practical example of this algo, the algorithm and code is here[2].

[1] https://storage.googleapis.com/superasn/script.html

[2] https://www.reddit.com/r/learnmachinelearning/comments/6hqd6...

Re: A practical explanation of a Naive Bayes classifier

#18
post #2

Considering the relative ease of implementation, classification accuracy with smaller datasets, and computational efficiency of Naive Bayes classifiers, I am surprised that they are not mentioned as often as other machine learning competitors, such as random forest. Are there major drawbacks to Naive Bayes classifiers? Is it just that they aren't as accurate on large datasets?

Accuracy is a problem. NBs are linear classifiers, and more so, the linear boundary is found by assuming features to be independent.

So, in cases:

(1) data cannot be separated linearly - NBs don't do well for the same reason as any other linear classifier wouldn't work well

(2) for linearly separable data - if the feature independence assumption is incompatible with the data, linear classifiers that don't make this assumption will beat it. For ex you can prove that logistic regression beats (or is at least as good as) Naive Bayes asymptotically. [1]

[1] See proposition 1 here: "On Discriminative vs Generative classifiers ... Andrew ng, Michael Jordan. https://ai.stanford.edu/~ang/papers/nips01-discriminativegen... [PDF]

Re: A practical explanation of a Naive Bayes classifier

#19
post #9

A practical issue for Naive Bayes that also infects linear models is bias w.r.t. document length. Typically when you are detecting a rare, relatively compact class such as sports articles (or spam) you will tend to have a strongly negative prior, many positive features, and few negative ones. As a consequence, as the length of your text increases, not only does the variance of your prediction increase, but the mean t…

I agree, there are issues with NB such as the ones you brought up. I don't think document length is the real offender here though. This really boils down to noise and how well you filter and devalue it. Stacking more filters like stemming, stopwords, and high frequency features definitely helps in this case to the point where longer documents can actually improve accuracy. Additionally, tuning your ngram lengths or using variable lengths, choosing between word or character ngrams, and limiting your distribution size all will help depending on what you're trying to categorize.

Re: A practical explanation of a Naive Bayes classifier

#20
post #12
post #9

A practical issue for Naive Bayes that also infects linear models is bias w.r.t. document length. Typically when you are detecting a rare, relatively compact class such as sports articles (or spam) you will tend to have a strongly negative prior, many positive features, and few negative ones. As a consequence, as the length of your text increases, not only does the variance of your prediction increase, but the mean t…

Is there some way to normalize the document length?

You can use term frequency instead of binary features. This is invariant to the size of the document. This is called multinomial naive Bayes: https://en.m.wikipedia.org/wiki/Naive_Bayes_classifier#Multi...
Post reply on HN