# Implementing Complementary Naive Bayes in python?

### Problem

I have tried using Naive bayes on a labeled data set of crime data but got really poor results (7% accuracy). Naive Bayes runs much faster than other alogorithms I've been using so I wanted to try finding out why the score was so low.

### Research

After reading I found that Naive bayes should be used with balanced datasets because it has a bias for classes with higher frequency. Since my data is unbalanced I wanted to try using the Complementary Naive Bayes since it is specifically made for dealing with data skews. In the paper that describes the process, the application is for text classification but I don't see why the technique wouldn't work in other situations. You can find the paper I'm referring to here. In short the idea is to use weights based on the occurences where a class doesn't show up.

After doing some research I was able to find an implementation in Java but unfortunately I don't know any Java and I just don't understand the algorithm well enough to implement myself.

### Question

where I can find an implementation in python? If that doesn't exist how should I go about implementing it myself?

Naive Bayes should be able to handle imbalanced datasets. Recall that the Bayes formula is

$$P(y \mid x) = \cfrac{P(x \mid y) \, P(y)}{P(x)} \propto P(x \mid y) \, P(y)$$

So $P(x \mid y) \, P(y)$ takes the prior $P(y)$ into account.

In your case maybe you overfit and need some smoothing? You can start with +1 smoothing and see if it gives any improvements. In python, when using numpy, I'd implement the smoothing this way:

table = # counts for each feature
PT = (table + 1) / (table + 1).sum(axis=1, keepdims=1)


Note that this is gives you Multinomial Naive Bayes - which applies only to categorical data.

I can also suggest the following link: http://www.itshared.org/2015/03/naive-bayes-on-apache-flink.html. It's about implementing Naive Bayes on Apache Flink. While it's Java, maybe it'll give you some theory you need to understand the algorithm better.

• You may be correct it looks like naive Bayes only has the imbalance problem with text classification. Can you explain what smoothing is? – grasshopper Jun 21 '15 at 15:34
• Have you had a look at the link? It's explained there. In short, when estimating $P(x \mid y)$ sometimes $x$ is never seen during training and smoothing ensures that it doesn't ruin the classifier performance by adding some extra count (in case of laplace or +1 smoothing) to all the features. – Alexey Grigorev Jun 21 '15 at 17:35

My implementation of Complement Naive Bayes in scikit-learn can be found here.
{"ft_lang_label":"__label__en","ft_lang_prob":0.977382,"math_prob":0.77668613,"size":1123,"snap":"2020-24-2020-29","text_gpt3_token_len":240,"char_repetition_ratio":0.10545129,"word_repetition_ratio":0.0,"special_character_ratio":0.1967943,"punctuation_ratio":0.052380953,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98734486,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-06-05T19:30:30Z\",\"WARC-Record-ID\":\"<urn:uuid:7e243222-3a00-4944-9858-ee14e5f77418>\",\"Content-Length\":\"154089\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:be27be5d-80b9-4f58-8fdf-c45139e099c8>\",\"WARC-Concurrent-To\":\"<urn:uuid:9f51f653-1116-4bd0-ad0f-d6f6418f869b>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://datascience.stackexchange.com/questions/6165/implementing-complementary-naive-bayes-in-python/6166\",\"WARC-Payload-Digest\":\"sha1:7ZCWETEDEAG5S5KLJJGGTJKM75T4GHUZ\",\"WARC-Block-Digest\":\"sha1:EDLENAU77Y2HZSSHTAOY2VAHOQQNZEWZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590348502204.93_warc_CC-MAIN-20200605174158-20200605204158-00398.warc.gz\"}"}