在电商平台中有大量的垃圾或者欺诈消息msg,如何识别召回这些垃圾消息是风控经常遇到的问题,今天我们来尝试用传统的朴素贝叶斯分类算法来挖掘分类模型来识别垃圾消息
下面的样本中,标签1是垃圾消息,标签0是正常消息
im_msg_train.csv
1 我要买 加我213④772834
1 我要买 加我213④772834
1 宝子你这个我要了加我QQ3149970399
1 宝子这个我要了加我QQ2743900479
1 宝子我要啦 加我 QQ3662847015
1 姐妹我要了加我QQ3640548334好联系
1 宝子我收啦+我q3645355250
0 aj312的5a版本要黄黑配色42.5码
0 Y507-59
0 帮我改到甘肃省 洒泉市 肃北蒙古族自治县
0 已故:高蕴峰,沙振忠,高映臣,宋振霞,高小明,程光远,程桐基,
0 本周六后再发货,包结实
0 复古提花
0 您好,鞋子要38码的,谢谢您
0 苏州相城区合景峰汇三期 Celine 13402553805
0 请于下周二发货,周三后到京即可,谢谢!
import jieba
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import BernoulliNB
from sklearn.model_selection import cross_val_score
data_list = []
label_list = []
for line in open("im_msg_train.csv"):
ll = line.strip().split("\t")
label, data = ll[0], ll[1]
data_list.append(data)
label_list.append(int(label))
# 分词
data_cut = []
for sentence in data_list:
sentence_cut = jieba.cut(sentence)
sentence_cut_join = ' '.join(sentence_cut)
data_cut.append(sentence_cut_join)
print(data_cut[:10])
# 计算TF-IDF向量
vectorizer_word = TfidfVectorizer()
vectorizer_word.fit(data_cut)
train = vectorizer_word.transform(data_cut)
print(train[:10])
test_msg = "姐妹我要了加我QQ3640548334好联系"
test_msg = jieba.cut(test_msg)
test_msg = ' '.join(test_msg)
test = vectorizer_word.transform([test_msg])
# 模型训练
clf = BernoulliNB() # BernoulliNB 适用于伯努利分布的数据,通常用于二元数据的分类
clf.fit(train.toarray(), label_list)
scores = cross_val_score(clf,train,label_list,cv=10,scoring='accuracy')
print("Accuracy:{:.4f}".format(scores.mean()))
res1 = clf.predict(test.toarray())
print(res1)
res = clf.predict_proba(test.toarray())
print(res)