-
Notifications
You must be signed in to change notification settings - Fork 3
/
NLPmodelTrainer.py
66 lines (54 loc) · 2.25 KB
/
NLPmodelTrainer.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
import csv
import numpy as np
import pandas as pd
import re
import nltk
import pickle
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
features = []
labels = []
with open("training.1600000.processed.noemoticon.csv", mode='r') as csvOut:
outfile = csv.reader(csvOut, delimiter=',')
initialLine = True
for row in outfile:
if initialLine == False:
initialLine = True
else:
features.append(row[5])
labels.append(row[0])
processed_features = []
valRange = 1
numFeatStart = int(len(features)* (.5-valRange/2))
numFeatEnd = int(len(features)* (.5+valRange/2))
scaleLabel = labels[numFeatStart:numFeatEnd]
for sentence in range(numFeatStart, numFeatEnd):
# Remove all the special characters
processed_feature = re.sub(r'\W', ' ', str(features[sentence]))
# remove all single characters
processed_feature= re.sub(r'\s+[a-zA-Z]\s+', ' ', processed_feature)
# Remove single characters from the start
processed_feature = re.sub(r'\^[a-zA-Z]\s+', ' ', processed_feature)
# Substituting multiple spaces with single space
processed_feature = re.sub(r'\s+', ' ', processed_feature, flags=re.I)
# Removing prefixed 'b'
processed_feature = re.sub(r'^b\s+', '', processed_feature)
# Converting to Lowercase
processed_feature = processed_feature.lower()
processed_features.append(processed_feature)
print("Finished Text Processing")
vectorizer = TfidfVectorizer(max_features=2500, min_df=7, max_df=0.8, stop_words=stopwords.words('english'))
processed_features = vectorizer.fit_transform(processed_features).toarray()
with open("vectorizer.pickle", "wb") as pickle_out:
pickle.dump(vectorizer, pickle_out)
print(processed_features.shape)
print("Splitting Tests")
X_train, X_test, y_train, y_test = train_test_split(processed_features, scaleLabel, test_size=0.2, random_state=0)
print("Fitting Logistic Regression")
text_classifier = LogisticRegression(random_state=0)
text_classifier.fit(X_train, y_train)
with open("LogisticRegClass.pickle", "wb") as pickle_out:
pickle.dump(text_classifier, pickle_out)
print("Fitting Completed")