forked from OptimaNiko/ML-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes.py
35 lines (28 loc) · 952 Bytes
/
naive_bayes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# cleaning texts
import pandas as pd
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer
dataset = [["I liked the movie", "positive"],
["It’s a good movie. Nice story", "positive"],
["Hero’s acting is bad but heroine looks good.\
Overall nice movie", "positive"],
["Nice songs. But sadly boring ending.", "negative"],
["sad movie, boring movie", "negative"]]
dataset = pd.DataFrame(dataset)
dataset.columns = ["Text", "Reviews"]
nltk.download('stopwords')
corpus = []
for i in range(0, 5):
text = re.sub('[^a-zA-Z]', '', dataset['Text'][i])
text = text.lower()
text = text.split()
ps = PorterStemmer()
text = ''.join(text)
corpus.append(text)
# creating bag of words model
cv = CountVectorizer(max_features = 1500)
X = cv.fit_transform(corpus).toarray()
y = dataset.iloc[:, 1].values