-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_models.py
87 lines (61 loc) · 2.31 KB
/
create_models.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Importing the libraries
import numpy as np
import re
import pickle
import nltk
from nltk.corpus import stopwords
from sklearn.datasets import load_files
nltk.download('stopwords')
# Importing the dataset
reviews = load_files('txt_sentoken/')
X,y = reviews.data,reviews.target
# Pickling the dataset
with open('X.pickle','wb') as f:
pickle.dump(X,f)
with open('y.pickle','wb') as f:
pickle.dump(y,f)
# Unpickling dataset
X_in = open('X.pickle','rb')
y_in = open('y.pickle','rb')
X = pickle.load(X_in)
y = pickle.load(y_in)
# Creating the corpus
corpus = []
for i in range(0, 2000):
review = re.sub(r'\W', ' ', str(X[i]))
review = review.lower()
review = re.sub(r'^br$', ' ', review)
review = re.sub(r'\s+br\s+',' ',review)
review = re.sub(r'\s+[a-z]\s+', ' ',review)
review = re.sub(r'^b\s+', '', review)
review = re.sub(r'\s+', ' ', review)
corpus.append(review)
# Creating the BOW model
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(max_features = 2000, min_df = 3, max_df = 0.6, stop_words = stopwords.words('english'))
X = vectorizer.fit_transform(corpus).toarray()
# Creating the Tf-Idf Model
from sklearn.feature_extraction.text import TfidfTransformer
transformer = TfidfTransformer()
X = transformer.fit_transform(X).toarray()
# Creating the Tf-Idf model directly
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features = 2000, min_df = 3, max_df = 0.6, stop_words = stopwords.words('english'))
X = vectorizer.fit_transform(corpus).toarray()
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
text_train, text_test, sent_train, sent_test = train_test_split(X, y, test_size = 0.20, random_state = 0)
# Training the classifier
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(solver='liblinear')
classifier.fit(text_train,sent_train)
# Testing model performance
sent_pred = classifier.predict(text_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(sent_test, sent_pred)
# Saving our classifier
with open('classifier.pickle','wb') as f:
pickle.dump(classifier,f)
# Saving the Tf-Idf model
with open('tfidfmodel.pickle','wb') as f:
pickle.dump(vectorizer,f)