-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_API.py
210 lines (180 loc) · 7.95 KB
/
package_API.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
import pandas as pd
from math import isnan
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split,cross_val_score
from sklearn.metrics import classification_report,confusion_matrix,roc_curve,auc
from datetime import datetime
import pickle
import matplotlib.pyplot as plt
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# from sklearn.feature_extraction.text import TfidfVectorizer
# v = TfidfVectorizer(stop_words="english")
# import string
# no_biaodian = string.punctuation(input)
def listElement2str(list1):
list2 = []
for i in range(len(list1)):
if type(list1[i]) == float:
if isnan(list1[i]):
list2.append("")
else:
list2.append(list1[i])
return list2
def draw_ROC_curve(y_test,y_predict,savepath):
false_positive_rate,true_positive_rate,threshold = roc_curve(y_test,y_predict)
roc_auc = auc(false_positive_rate,true_positive_rate)
plt.title("ROC")
plt.plot(false_positive_rate,true_positive_rate,'b',label = 'AUC = %0.2f'%roc_auc)
plt.legend(loc= 'low right')
plt.plot([0,1],[1,0],'r--')
plt.ylabel("TPR")
plt.xlabel("FPR")
plt.savefig(savepath)
def see_result(train_X,train_y,test_X,test_y,clf,savepath):
# (2)使用交叉验证调参
scores = cross_val_score(clf, train_X, train_y, cv=10)
print(scores.mean())
precision_report = classification_report(test_y, clf.predict(test_X))
print(precision_report)
Confusion_matrix = confusion_matrix(test_y, clf.predict(test_X))
print(Confusion_matrix)
draw_ROC_curve(test_y,clf.predict(test_X),savepath)
def load_model(model_path):
# 载入模型
model = pickle.load(open(model_path,'rb'))
return model
def save_model(clf,model_path):
# 保存模型
model = pickle.dump(clf,open(model_path,'wb'))
def drop_stopwords(contents,stopwords):
contents_clean = []
all_words = []
for line in contents:
#遍历contents里面的每个元素,每一个元素都是一个list,也就是一条信息
line_clean = []
for word in line:
#遍历每个list里的单词,因为之前已经经过切分了
if word in stopwords:
continue
line_clean.append(word)
all_words.append(str(word))
contents_clean.append(line_clean)
return contents_clean,all_words
class Spam_Detector():
def nltk_fc(self):
"""用nltk进行英文分词"""
content_S = []
for line in self.content:
current_segment = nltk.word_tokenize(line)
content_S.append(current_segment)
self.content_S = content_S
return self.content_S
def drop_stopwords(self):
"""去掉停用词"""
self.stopwords = np.array(pd.read_csv("resource_data/stopwords.txt", index_col=False, sep="\t", quoting=3, names=['stopword'],encoding='utf-8')).tolist()
self.clean_words = drop_stopwords(self.content_S, self.stopwords)[0]
self.all_words = drop_stopwords(self.content_S, self.stopwords)[1]
return self.clean_words,self.stopwords
def tfidf_draw_feature(self):
"""用tf-idf进行提取特征"""
logger.info("TF-IDF is drawing the feature!")
# (1)首先需要对每句话抽出的词进行拼接成一句子,虽然是断断续续的
corpus = []
for line in self.clean_words:
line_single = " ".join(line)
corpus.append(line_single)
self.corpus_df = pd.DataFrame({"corpus:":corpus})
self.corpus = corpus
# (2)然后用tfidf模型训练(必须输入是一个list形式)
self.vectorizer = TfidfVectorizer(analyzer='word')
self.vectorizer.fit(self.corpus)
self.X = self.vectorizer.transform(self.corpus).toarray() # tfidf 矩阵
self.diction = self.vectorizer.vocabulary_ #tfidf字典(比如 array :111)
return self.X,self.diction
def train_model(self):
"""6.建立二分类模型"""
# (1)将数据集分为训练和测试集
self.train_X, self.test_X, self.train_y, self.test_y = train_test_split(self.X, self.label_new, test_size=0.2, random_state=42)
# (2)尝试不同的分类器效果朴素贝叶斯和随机森林
self.RF_model = RandomForestClassifier(n_estimators=500, max_features=10, n_jobs=-1)
self.RF_model.fit(self.train_X, self.train_y)
see_result(self.train_X, self.train_y, self.test_X, self.test_y, self.RF_model, "ROC/RF_roc.jpg")
save_model(self.RF_model, "model/RF_model.model")
def input_one_record(self,input):
"""输入单个语句,得到输出结果"""
self.current_singe = nltk.word_tokenize(input)
self.end_words = []
for i in self.current_singe:
if i not in self.stopwords:
self.end_words.append(i)
self.words_join = " ".join(self.end_words)
"""3.tfidf+model"""
tfidf_matrix = self.vectorizer.transform([self.words_join]).toarray()
self.result = self.RF_model.predict(tfidf_matrix)
return self.result
def __init__(self):
logger.info("Starting up the Spam Detector: ")
self.path = 'resource_data/spam.csv'
"""1.加载数据"""
logger.info("Loading the data!")
spam_data = pd.read_csv(self.path, encoding='utf-8')
spam_data.columns = ['label', 'content1', 'content2', 'content3', 'content4']
col_names = spam_data.columns.values
"""2.数据整理"""
content1 = listElement2str(np.array(spam_data["content1"]).tolist())
content2 = listElement2str(np.array(spam_data["content2"]).tolist())
content3 = listElement2str(np.array(spam_data["content3"]).tolist())
content4 = listElement2str(np.array(spam_data["content4"]).tolist())
# 整理标签
label_orig = np.array(spam_data["label"]).tolist()
self.label_new = []
for i in range(len(label_orig)):
if label_orig[i] == 'ham':
self.label_new.append(0)
else:
self.label_new.append(1)
self.content = []
for i in range(len(content1)):
content_single = "".join((content1[i], content2[i], content3[i], content4[i]))
self.content.append(content_single)
new_spam = pd.DataFrame({"label": self.label_new, "content": self.content})
self.nltk_fc()
self.drop_stopwords()
self.tfidf_draw_feature()
self.train_model()
class One_record_test():
def __init__(self):
# 关于单一一条记录进行测试
"""1.读取model,读取语料库"""
self.model = load_model("model/RF_model.model")
self.vectorizer = load_model("model/vectorizer.model")
self.corpus = pd.read_csv("corpus.csv")
self.spam_data = pd.read_csv('spam_new.csv', encoding='utf-8')
def confirm_result(self,content_1_str):
"""2.分词+去停用词+合并"""
self.current_S = nltk.word_tokenize(content_1_str)
self.stopwords = np.array(pd.read_csv("source_data/stopwords.txt", index_col=False, sep="\t", quoting=3, names=['stopword'],
encoding='utf-8')).tolist()
self.end_words = []
for i in self.current_S:
if i not in self.stopwords:
self.end_words.append(i)
self.words_join = " ".join(self.end_words)
"""3.tfidf+model"""
self.tfidf_matrix = self.vectorizer.transform([self.words_join]).toarray()
self.result = self.model.predict(self.tfidf_matrix)
return self.result
if __name__ == '__main__':
# obj = Spam_Detector()
input_str = "Hello,I am YeYan!"
# result = obj.input_one_record(input_str)
# print(result)
obj2 = One_record_test()
result2 = obj2.confirm_result(input_str)
print(result2)