-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_classifier.py
More file actions
174 lines (128 loc) · 5.35 KB
/
text_classifier.py
File metadata and controls
174 lines (128 loc) · 5.35 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
# author: Wanshan
import os
import sys
import pickle as pkl
from sklearn.datasets import fetch_20newsgroups
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from SpamMessageClassifier import data_preprocress
BASE_DIR = os.path.dirname(__file__)
def lr_init(**kwargs):
from sklearn.linear_model import LogisticRegression
return LogisticRegression(**kwargs)
def sgd_init(**kwargs):
from sklearn.linear_model import SGDClassifier
return SGDClassifier(**kwargs)
def decision_tree_init(**kwargs):
from sklearn.tree import DecisionTreeClassifier
return DecisionTreeClassifier(**kwargs)
def naive_bayes_init(**kwargs):
from sklearn.naive_bayes import MultinomialNB
return MultinomialNB(**kwargs)
def svm_init(**kwargs):
from sklearn.svm import LinearSVC
return LinearSVC(**kwargs)
def knn_init(**kwargs):
from sklearn.neighbors import KNeighborsClassifier
return KNeighborsClassifier(**kwargs)
def random_forest_init(**kwargs):
from sklearn.ensemble import RandomForestClassifier
return RandomForestClassifier(**kwargs)
def gradient_boosting_init(**kwargs):
from sklearn.ensemble import GradientBoostingClassifier
return GradientBoostingClassifier(**kwargs)
def xgboost_init(**kwargs):
from xgboost import XGBClassifier
return XGBClassifier(**kwargs)
class model(object):
def __init__(self, model_path, model_name=None, **kwargs):
self.model_path = os.path.join(BASE_DIR, model_path)
self.model_name = model_name
self.kwargs = kwargs
print(self.model_path)
if os.path.exists(self.model_path):
self.model = self.load_model()
else:
self.model = getattr(sys.modules[__name__], '%s_init' % self.model_name.lower())(**self.kwargs)
print(self.model)
def train(self, train_data, label_data):
x_train, x_test, y_train, y_test = train_test_split(train_data,
label_data,
test_size=0.2,
random_state=24)
x_train, x_test = self.count_stop_vector(x_train, x_test)
# print(x_train, x_test, y_train, y_test)
# ss = StandardScaler()
# x_train = ss.fit_transform(x_train)
# x_test = ss.transform(x_test)
self.model.fit(x_train, y_train)
y_predict = self.model.predict(x_test)
print('Accuracy of %s Classifier: ' % self.model_name, self.model.score(x_test, y_test))
print(classification_report(y_test, y_predict))
def inference(self, input_data):
try:
print(self.model)
y_predict = self.model.predict(input_data)
print('The predict result of %s Classifier are:\n' % self.model_name)
print(y_predict)
except:
print("Check the input_data is fit to model.")
exit()
def save_model(self):
print(self.model_path)
with open(self.model_path, 'wb') as file:
pkl.dump(self.model, file)
def load_model(self):
try:
file = open(self.model_path, 'rb')
model = pkl.load(file)
return model
except FileNotFoundError:
print("Make sure trained_model in model path!")
raise FileNotFoundError
@staticmethod
def count_vector(x_train, x_test):
count_vec = CountVectorizer()
x_train = count_vec.fit_transform(x_train)
x_test = count_vec.transform(x_test)
return x_train, x_test
@staticmethod
def count_stop_vector(x_train, x_test):
count_stop_vec = CountVectorizer(stop_words='english')
print(count_stop_vec.stop_words)
x_train = count_stop_vec.fit_transform(x_train)
x_test = count_stop_vec.transform(x_test)
return x_train, x_test
@staticmethod
def tf_idf_vector(x_train, x_test):
tf_idf_vec = TfidfVectorizer()
x_train = tf_idf_vec.fit_transform(x_train)
x_test = tf_idf_vec.transform(x_test)
return x_train, x_test
@staticmethod
def tf_idf_stop_vector(x_train, x_test):
tf_idf_stop_vec = TfidfVectorizer(stop_words='english')
x_train = tf_idf_stop_vec.fit_transform(x_train)
x_test = tf_idf_stop_vec.transform(x_test)
return x_train, x_test
if __name__ == '__main__':
# # useage of fetch_20newsgroups
# news_data = fetch_20newsgroups(subset='all')
# model = model(model_path='model/svm_model.pkl', model_name='svm', penalty='l2')
# print(type(news_data.data))
# model.train(news_data.data, news_data.target)
# model.save_model()
# # inference
# model = model(model_path='model/test_model.pkl', model_name='svm')
# prediction = model.inference(input_data)
# print(prediction)
# useage of Chinese Spam Mail Binary-classification
# load data from txt
data, label = data_preprocress.read_data('./data/data_labeled.txt')
# segment every mail
data = data_preprocress.content_word_segment(data)
model = model(model_path='model/test_model.pkl', model_name='lr', penalty='l2', class_weight={1:5, 0:1})
model.train(data, label)