垃圾邮件分类器_如何在10个步骤中构建垃圾邮件分类器

本文将指导你如何在10个步骤中构建一个垃圾邮件分类器,通过学习和理解数据,预处理文本,构建模型,直到最终部署,帮助你入门数据科学实践。
摘要由CSDN通过智能技术生成

垃圾邮件分类器

If you’re just starting out in Machine Learning, chances are you’ll be undertaking a classification project. As a beginner, I built an SMS spam classifier but did a ton of research to know where to start. In this article, I’ll walk you through my project in 10 steps to make it easier for you to build your first spam classifier using Tf-IDF Vectorizer, and the Naïve Bayes model!

如果您刚刚开始学习机器学习,那么您很可能会进行分类项目。 作为一个初学者,我建立了一个SMS垃圾邮件分类器,但进行了大量研究以了解从何开始。 在本文中,我将分10个步骤逐步介绍我的项目,以使您更轻松地使用Tf-IDF Vectorizer和NaïveBayes模型构建第一个垃圾邮件分类器!

1.加载并简化数据集 (1. Load and simplify the dataset)

Our SMS text messages dataset has 5 columns if you read it in pandas: v1 (containing the class labels ham/spam for each text message), v2 (containing the text messages themselves), and three Unnamed columns which have no use. We’ll rename the v1 and v2 columns to class_label and message respectively while getting rid of the rest of the columns.

如果您以熊猫阅读,我们的SMS短信数据集有5列:v1(每个短信包含类别标签ham / spam),v2(包含短信本身)和三个无用的未命名列。 我们将第1版和第2版列分别重命名为class_label和message,而除去其余的列。

import pandas as pd
df = pd.read_csv(r'spam.csv',encoding='ISO-8859-1')
df.rename(columns = {'v1':'class_label', 'v2':'message'}, inplace = True)
df.drop(['Unnamed: 2', 'Unnamed: 3', 'Unnamed: 4'], axis = 1, inplace = True)df

Check out the fact that ‘5572 rows x 2 columns’ means that our dataset has 5572 text messages!

看看“ 5572行x 2列”这一事实意味着我们的数据集包含5572条文本消息!

2.浏览数据集:条形图 (2. Explore the dataset: Bar Chart)

It’s a good idea to carry out some Exploratory Data Analysis (EDA) in a classification problem to visualize, get some information out of, or find any issues with your data before you start working with it. We’ll look at how many spam/ham messages we have and create a bar chart for it.

在开始处理分类问题之前,最好对分类问题进行一些探索性数据分析(EDA)以可视化,从中获取一些信息或发现任何问题。 我们将查看有多少垃圾邮件/火腿邮件,并为其创建条形图。

#exploring the datasetdf['class_label'].value_counts()
Jupyter notebook cell output showing the number of spam and ham messages

Our dataset has 4825 ham messages and 747 spam messages. This is an imbalanced dataset; the number of ham messages is much higher than those of spam! This can potentially cause our model to be biased. To fix this, we could resample our data to get an equal number of spam/ham messages.

我们的数据集包含4825个火腿邮件和747垃圾邮件。 这是一个不平衡的数据集; 火腿邮件的数量远高于垃圾邮件! 这可能会导致我们的模型出现偏差。 为了解决这个问题,我们可以对数据重新采样以获取相同数量的垃圾邮件/火腿邮件。

To generate our bar chart, we use NumPy and pyplot from Matplotlib.

为了生成条形图,我们使用Matplotlib中的NumPy和pyplot。

Bar chart showing the number of spam and ham messages in our dataset

3.探索数据集:词云 (3. Explore the dataset: Word Clouds)

For my project, I generated word clouds of the most frequently occurring words in my spam messages.

对于我的项目,我生成了垃圾邮件中最常出现的单词的单词云。

First, we’ll f

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值