【数据科学项目02】:NLP应用之垃圾短信 邮件检测(端到端的项目)_短消息分析 nlp(2)

| 2 | spam | Free entry in 2 a wkly comp to win FA Cup fina… | NaN | NaN | NaN |
| 3 | ham | U dun say so early hor… U c already then say… | NaN | NaN | NaN |
| 4 | ham | Nah I don’t think he goes to usf, he lives aro… | NaN | NaN | NaN |

该数据包含一组带有标记的短信数据,其中:

  • v1表示短信标签,ham表示正常信息,spam表示垃圾信息
  • v2是短信的内容
#去除不需要的列
df=df.iloc[:,:2]

#重命名列
df=df.rename(columns={"v1":"label","v2":"message"})
df.head()

labelmessage
0hamGo until jurong point, crazy… Available only …
1hamOk lar… Joking wif u oni…
2spamFree entry in 2 a wkly comp to win FA Cup fina…
3hamU dun say so early hor… U c already then say…
4hamNah I don’t think he goes to usf, he lives aro…
# 将lable进行one-hot编码,其中0:ham,1:spam
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()

df['label']=encoder.fit_transform(df['label'])
df['label'].value_counts()

0    4825
1     747
Name: label, dtype: int64

可以看出一共有747个垃圾短信

# 查看缺失值
df.isnull().sum()
# 数据没有缺失值

label      0
message    0
dtype: int64

2.探索性数据分析(EDA)

通过可视化分析来更好的理解数据

import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.figure(figsize=(9,4))
plt.subplot(1,2,1)
plt.pie(df['label'].value_counts(),labels=['not spam','spam'],autopct="%0.2f")
plt.subplot(1,2,2)
sns.barplot(x=df['label'].value_counts().index,y=df['label'].value_counts(),data=df)
plt.show()

output_17_0

在特征工程部分,我简单创建了一些单独的特征来提取信息

  • 字符数
  • 单词数
  • 句子数
#1.字符数
df['char']=df['message'].apply(len)

nltk.download('punkt')

[nltk_data] Downloading package punkt to /root/nltk_data...
[nltk_data]   Unzipping tokenizers/punkt.zip.

True

#2.单词数,这里我们首先要对其进行分词处理,使用nltk
#分词处理
df['words']=df['message'].apply(lambda x: len(nltk.word_tokenize(x)))

# 3.句子数
df['sen']=df['message'].apply(lambda x: len(nltk.sent_tokenize(x)))

df.head()

labelmessagecharwordssen
00Go until jurong point, crazy… Available only …111242
10Ok lar… Joking wif u oni…2982
21Free entry in 2 a wkly comp to win FA Cup fina…155372
30U dun say so early hor… U c already then say…49131
40Nah I don’t think he goes to usf, he lives aro…61151

描述性统计

# 描述性统计
df.describe()

indexlabelcharwordssen
count5572.05572.05572.05572.0
mean0.1340631730078966480.1188083273510518.695620961952621.9707465900933239
std0.3407507548977697459.690840776503313.7425868017449751.4177777134026657
min0.02.01.01.0
25%0.036.09.01.0
50%0.061.015.01.0
75%0.0121.027.02.0
max1.0910.0220.028.0

下面我们通过可视化比较一下不同短信在这些数字特征上的分布情况

# 字符数比较
plt.figure(figsize=(12,6))
sns.histplot(df[df['label']==0]['char'],color='red')#正常短信
sns.histplot(df[df['label']==1]['char'],color = 'blue')#垃圾短信

<matplotlib.axes._subplots.AxesSubplot at 0x7fce63763dd0>

png

# 比较
plt.figure(figsize=(12,6))
sns.histplot(df[df['label']==0]['words'],color='red')#正常短信
sns.histplot(df[df['label']==1]['words'],color = 'blue')#垃圾短信

<matplotlib.axes._subplots.AxesSubplot at 0x7fce63f4bed0>


png

sns.pairplot(df,hue='label')

在这里插入图片描述

#删除数据集中存在的一些异常值
i=df[df['char']>500].index
df.drop(i,axis=0,inplace=True)

df=df.reset_index()
df.drop("index",inplace=True,axis=1)

#相关系数矩阵
sns.heatmap(df.corr(),annot=True)

<matplotlib.axes._subplots.AxesSubplot at 0x7fce606d0250>

output_34_1

我们这里看到存在多重共线性,因此,我们不使用所有的列,在这里选择与label相关性最强的char

3.数据预处理

对于英文文本数据,我们常用的数据预处理方式如下

  • 去除标点符号
  • 去除停用词
  • 去除专有名词
  • 变换成小写
  • 分词处理
  • 词根、词缀处理

下面我们来看看如何实现这些步骤

nltk.download('stopwords')

[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Unzipping corpora/stopwords.zip.

True

# 首先导入需要使用到的包
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from wordcloud import WordCloud
import string,time

# 标点符号
string.punctuation

'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

# 停用词
stopwords.words('english')

3.1清洗文本数据

  • 去除web链接
  • 去除邮件
  • 取掉数字

下面使用正则表达式来处理这些数据。

def remove\_website\_links(text):
    no_website_links = text.replace(r"http\S+", "")#去除网络连接
    return no_website_links

def remove\_numbers(text):
    removed_numbers = text.replace(r'\d+','')#去除数字
    return removed_numbers

def remove\_emails(text):
    no_emails = text.replace(r"\S\*@\S\*\s?",'')#去除邮件
    return no_emails

df['message'] = df['message'].apply(remove_website_links)
df['message'] = df['message'].apply(remove_numbers)
df['message'] = df['message'].apply(remove_emails)

df.head()

labelmessagecharwordssen
00Go until jurong point, crazy… Available only …111242
10Ok lar… Joking wif u oni…2982
21Free entry in 2 a wkly comp to win FA Cup fina…155372
30U dun say so early hor… U c already then say…49131
40Nah I don’t think he goes to usf, he lives aro…61151

3.2 文本特征转换

def message\_transform(text):
  
  text = text.lower()#转换为小写
  
  text = nltk.word_tokenize(text)#分词处理
  
  # 去除停用词和标点
  y = []#创建一个空列表
  for word in text:
    stopwords_punc = stopwords.words('english')+list(string.punctuation)#存放停用词和标点
    if word.isalnum()==True and word not in stopwords_punc:
      y.append(word)
  
  # 词根变换
  message=y[:]
  y.clear()
  for i in message:
    ps=PorterStemmer()
    y.append(ps.stem(i))
  return " ".join(y)#返回字符串形式

df['message'] = df['message'].apply(message_transform)
df['num\_words\_transform']=df['message'].apply(lambda x: len(str(x).split()))

df.head()

labelmessagecharwordssen
00Go until jurong point, crazy… Available only …111242
10Ok lar… Joking wif u oni…2982
21Free entry in 2 a wkly comp to win FA Cup fina…155372
30U dun say so early hor… U c already then say…49131
40Nah I don’t think he goes to usf, he lives aro…61151

4.词频统计

4.1绘制词云

#绘制信息中出现最多的词的词云
from wordcloud import WordCloud
#首先,创建一个object
wc=WordCloud(width=500,height=500,min_font_size=10,background_color='white')

# 垃圾信息的词云
spam_wc=wc.generate(df[df['label']==1]['message'].str.cat(sep=""))

plt.figure(figsize=(18,12))
plt.imshow(spam_wc)

<matplotlib.image.AxesImage at 0x7fce5d938710>

output_53_1

可以看出,这些垃圾邮件出现频次最多的单词是:free、call等这种具有诱导性的信息

# 正常信息的词云
ham_wc = wc.generate(df[df['label']==0]['message'].str.cat(sep=''))
plt.figure(figsize=(18,12))
plt.imshow(ham_wc)

<matplotlib.image.AxesImage at 0x7fce607af190>

output_55_1

可以看出正常信息出现频次较多的单词为u、go、got、want等一些传达信息的单词

为了简化词云图的信息,我们现在分别统计垃圾短信和正常短信频次top30的单词

4.2找出词数top30的单词

垃圾短信:

# 统计词频
spam_corpus=[]
for i in df[df['label']==1]['message'].tolist():
  for word in i.split():
        spam_corpus.append(word)


from collections import Counter
Counter(spam_corpus)#记数
Counter(spam_corpus).most_common(30)#取最多的30个单词
plt.figure(figsize=(10,7))
sns.barplot(y=pd.DataFrame(Counter(spam_corpus).most_common(30))[0],x=pd.DataFrame(Counter(spam_corpus).most_common(30))[1])
plt.xticks()
plt.xlabel("Frequnecy")
plt.ylabel("Spam Words")
plt.show()

output_61_0

正常短信

ham_corpus=[]
for i in df[df['label']==0]['message'].tolist():
  for word in i.split():
    ham_corpus.append(word)

from collections import Counter
plt.figure(figsize=(10,7))
sns.barplot(y=pd.DataFrame(Counter(ham_corpus).most_common(30))[0],x=pd.DataFrame(Counter(ham_corpus).most_common(30))[1])
plt.xticks()
plt.xlabel("Frequnecy")
plt.ylabel("Ham Words")
plt.show()

output_64_0

下面进一步分析垃圾短信和非垃圾短信的单词和字符数分布情况

# 字符数
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(15,6))
text_len=df[df['label']==1]['text'].str.len()
ax1.hist(text_len,color='green')
ax1.set_title('Original text')
text_len=df[df['label']==0]['text'].str.len()
ax2.hist(text_len,color='red')
ax2.set_title('Fake text')
fig.suptitle('Characters in texts')
plt.show()

在这里插入图片描述

#单词数
fig,(ax1,ax2)=plt.subplots(1,2,figsize=(15,6))
text_len=df[df['label']==1]['num\_words\_transform']
ax1.hist(text_len,color='red')
ax1.set_title('Original text')
text_len=df[df['label']==0]['num\_words\_transform']
ax2.hist(text_len,color='green')
ax2.set_title('Fake text')
fig.suptitle('Words in texts')
plt.show()

在这里插入图片描述

总结
经过上面分析,我们可以得出结论,垃圾短信文本与非垃圾短信文本相比具有更多的单词和字符。

  • 垃圾短信中包含的平均字符数约为 90 个字符
  • 垃圾短信中包含的平均字数约为 15 个字

5.模型构建

根据历史经验,在文本数据上朴素贝叶斯算法效果很好,因此我们将使用它,但在此过程中还将它与不同的算法进行比较。
在统计学中,朴素贝叶斯分类器是一系列简单的“概率分类器”,它们基于应用贝叶斯定理和特征之间的(朴素)条件独立假设。它们是最简单的贝叶斯网络模型之一,但与核密度估计相结合,它们可以达到更高的准确度水平。

image-20220823233043973

首先,我们这里的输入数据是文本数据,不能够直接建立模型。因此,我们必须将这些文本数据进行特征提取。比较常用的几种方法:

  • 词袋模型(Bag of words) 存在稀疏性问题
  • TF-IDF
  • Word2vec

因为是实战训练,在这里不具体展开的几种方法的原理,在这里我选择TF-IDF

image-20220823233013280

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

c5ceea7e5a.png#pic_center)

首先,我们这里的输入数据是文本数据,不能够直接建立模型。因此,我们必须将这些文本数据进行特征提取。比较常用的几种方法:

  • 词袋模型(Bag of words) 存在稀疏性问题
  • TF-IDF
  • Word2vec

因为是实战训练,在这里不具体展开的几种方法的原理,在这里我选择TF-IDF

image-20220823233013280

[外链图片转存中…(img-lpHaiH3v-1714269270097)]
[外链图片转存中…(img-Y2zZCYQK-1714269270098)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值