Machine learning code combat-Naive Bayes (implementing spam classification)

1. Experimental Purpose

(1) Analyze mail data and preprocess the data
(2) Run Naive Bayes model to classify mail

Data link
Password: bwfa

2. Import the necessary modules and read the data

import pandas as pd

df = pd.read_csv('spam.csv')
df.head()

Insert picture description here

df['spam'] = df['Category'].apply(lambda x: 1 if x=='spam' else 0)    #将标签数字化

from sklearn.model_selection import train_test_split  

X_train, X_test, y_train, y_test = train_test_split(df.Message, df.spam)  #拆分训练集与测试集

from sklearn.feature_extraction.text import CountVectorizer

v = CountVectorizer()    #将文本中的词语转换为词频矩阵
X_train_count = v.fit_transform(X_train.values)   #通过fit_transform函数计算各个词语出现的次数
X_train_count.toarray()[0:5]

Insert picture description here

3. Training + prediction

from sklearn.naive_bayes import MultinomialNB    #导入多标签朴素贝叶斯模块

model = MultinomialNB()   #实例化 
model.fit(X_train_count, y_train)    #训练

X_test_count = v.transform(X_test.values)      #转化测试集为数字
model.score(X_test_count, y_test)   #计算准确率

Insert picture description here

#测试
emails = [
    'Hey mohan, can we get together to watch footbal game tomorrow?',
    'Upto 20% discount on parking, exclusive offer just for you. Dont miss this reward!'
]
emails_count = v.transform(emails)  #转化为数字
model.predict(emails_count)   #预测

Insert picture description here

4. How to use Sklearn Pipeline

from sklearn.pipeline import Pipeline

clf = Pipeline([                       #将转换和模型封装在一起
    ('vectorizer',CountVectorizer()),
    ('nb',MultinomialNB())
])

clf.fit(X_train,y_train)    #训练

Insert picture description here

clf.score(X_test,y_test)    #测试
clf.predict(emails)

Insert picture description here

Published 227 original articles · praised 633 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_37763870/article/details/105460383