<jupyter_start><jupyter_text>Unit 3  Chapter 3  ---  Naive Bayes Classifiers ---Naive Bayes Classifiers are **Bayesian** classifiers that make an assumption that the features are **naive** i.e. the features(here words) are independent and have no relation between each other. For example, consider the sentence,'He is my friend. His name is John.'A naive classifier sees no relation between, **He**, **His**, & **John** (even though there is). ---  Bag of words Before we work on any algorithm, we need to convert our text data to numbers.To convert text into numbers, we need a way to represent each word in our data.In the **bag of words** model, we use frequency as a way of representing text as numbers.There are new & better ways to convert our text data into numbers, like **word2vec**.We will first take a look at the ```bag of words``` model. --- A bag of words model, as the name suggests, stores words into an unordered **set**.We use **set** because duplicate words are not allowed, as we are storing their frequency as well.  Bag of words in Python> [Code Courtesy](https://www.geeksforgeeks.org/bag-of-words-bow-model-in-nlp/) - Let's take a sample paragraph > Beans. I was trying to explain to somebody as we were flying in, that’s corn. That’s beans. And they were very impressed at my agricultural knowledge. Please give it up for Amaury once again for that outstanding introduction. I have a bunch of good friends here today, including somebody who I served with, who is one of the finest senators in the country, and we’re lucky to have him, your Senator, <NAME> is here. I also noticed, by the way, former Governor Edgar here, who I haven’t seen in a long time, and somehow he has not aged and I have. And it’s great to see you, Governor. I want to thank President Killeen and everybody at the U of I System for making it possible for me to be here today. And I am deeply honored at the Paul Douglas Award that is being given to me. He is somebody who set the path for so much outstanding public service here in Illinois. Now, I want to start by addressing the elephant in the room. I know people are still wondering why I didn’t speak at the commencement.<jupyter_code>dataset = 'Beans. I was trying to explain to somebody as we were flying in, that’s corn. That’s beans. And they were very impressed at my agricultural knowledge. Please give it up for Amaury once again for that outstanding introduction. I have a bunch of good friends here today, including somebody who I served with, who is one of the finest senators in the country, and we’re lucky to have him, your Senator, <NAME> is here. I also noticed, by the way, former Governor Edgar here, who I haven’t seen in a long time, and somehow he has not aged and I have. And it’s great to see you, Governor. I want to thank President Killeen and everybody at the U of I System for making it possible for me to be here today. And I am deeply honored at the Paul Douglas Award that is being given to me. He is somebody who set the path for so much outstanding public service here in Illinois. Now, I want to start by addressing the elephant in the room. I know people are still wondering why I didn’t speak at the commencement.'
dataset<jupyter_output><empty_output><jupyter_text>Basic preprocessing > We will first break the sentences, then convert text to lower case, and remove punctuation and non words<jupyter_code>from nltk.tokenize import sent_tokenize
import re 
sentences = sent_tokenize(dataset)
sentences
sentences[4]
# Now the dataset has been broken into sentences.<jupyter_output><empty_output><jupyter_text>> Converting to lower case.But why is it neccessary to convert to lower case?- If we don't convert to lower case then, any two words like ```Going``` and ```going``` will be treated differently, resulting in a large feature space.<jupyter_code>lower_cased_data = [sentence.lower() for sentence in sentences]
lower_cased_data[4]<jupyter_output><empty_output><jupyter_text>> Removing non-word characters<jupyter_code>cleaned_data = [re.sub(r'\W+',' ',sentence) for sentence in lower_cased_data]
cleaned_data[4]<jupyter_output><empty_output><jupyter_text>> Removing punctuations if any<jupyter_code>cleaned_data = [re.sub(r'\s+',' ',sentence) for sentence in cleaned_data]
cleaned_data[4]<jupyter_output><empty_output><jupyter_text>> Creating bag of words<jupyter_code>word2count = {} 
for data in cleaned_data: 
    words = nltk.word_tokenize(data) 
    for word in words: 
        if word not in word2count.keys(): 
            word2count[word] = 1
        else: 
            word2count[word] += 1<jupyter_output><empty_output><jupyter_text>Now we have a bag of words that has stored frequency with it<jupyter_code>word2count<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup