CNN英文垃圾邮件分类(数据预处理)

本文介绍利用CNN进行英文垃圾邮件分类的方法,包括数据预处理、构建词表、初始化词向量矩阵、预处理邮件、构造标签、数字化数据集和打乱数据等步骤。通过选取邮件中的高频词,将单词转化为索引序列,使用随机初始化的词向量矩阵表示邮件内容,最终形成适合CNN处理的输入数据。
摘要由CSDN通过智能技术生成

整理自唐宇迪老师的视频课程,感谢他!
本文最后会贴出所有的源代码文件,下文只是针对每个小点贴出代码进行注释说明,可以略过。

1.思路
关于利用CNN做文本分类,其主要思想通过下面这幅图就能够一目了然。

这里写图片描述

本文主要记录了利用CNN来分类英文垃圾邮件的全过程。数据集主要包含两个文件:里面分别是垃圾邮件和正常邮件,用记事本就能打开。先来看看数据集长什么样:

simplistic , silly and tedious .
unfortunately the story and the actors are served with a hack script .
all the more disquieting for its relatively gore-free allusions to the serial murders , but it falls down in its attempts to humanize its subject .
a sentimental mess that never rings true .
while the performances are often engaging , this loose collection of largely improvised numbers would probably have worked better as a one-hour tv documentary .
interesting , but not compelling .

这里展示的是其中的6封邮件,可以看到每封邮件之间是通过回车换行来分隔的,我们等下也通过这个特点来分割每一个样本。

我们知道,在CNN文本分类中,是通过将每个单词对应的词向量堆叠起来,形成一个二维矩阵,以此来进行卷积和池化的。但在此处我们没有词向量怎么办呢?既然没有,那索性就不要,把它也当做一个参数,让它在训练中产生。具体做法就是:

①根据所有邮件中的单词,选取出现频率靠前的k个或者全部(本例采用全部)产生一个长度为vocabulary_size的字典(词表);
②随机初始化一个大小为[vocabulary_size,embedding_size]的词向量矩阵,embedding_size表示你用多少维的向量来表示一个词;
③对于每一封邮件,找出其每个单词在字典中对应的索引,然后按照索引从词向量矩阵中取出对应位置的词向量,堆叠形成表示该邮件的二维矩阵;
④为所有的邮件设定一个最大长度,即最多由多少个单词构成,多的截取,少的用0填充。

例如:

一封邮件内容为tomorrow is sunny,假设这三个单词在字典中对应的索引为6,2,3,且邮件的最大长度为7;那么我们首先得到这封邮件对应单词的索引序列就为:56,28,97,0,0,0,0。同时初始化的词向量矩阵为:

[[0.398 0.418 0.29  0.344 0.898 0.555 0.033 0.056 0.923]    0
 [0.668 0.957 0.428 0.942 0.692 0.084 0.413 0.619 0.02 ]    1    
 [0.329 0.618 0.189 0.544 0.76  0.702 0.009 0.811 0.882]    2
 [0.912 0.042 0.777 0.765 0.708 0.887 0.944 0.272 0.5  ]    3
 [0.397 0.828 0.244 0.439 0.598 0.298 0.505 0.63  0.883]    4
 [0.402 0.084 0.419 0.66  0.69  0.031 0.354 0.117 0.494]    5
 [0.966 0.016 0.218 0.732 0.523 0.263 0.749 0.813 0.547]    6
 [0.065 0.739 0.394 0.077 0.461 0.203 0.246 0.456 0.809]]   7

tomorrow is sunny这封邮件对应的矩阵就为:

[[0.966 0.016 0.218 0.732 0.523 0.263 0.749 0.813 0.547]    6
 [0.329 0.618 0.189 0.544 0.76  0.702 0.009 0.811 0.882]    2
 [0.912 0.042 0.777 0.765 0.708 0.887 0.944 0.272 0.5  ]    3
 [0.398 0.418 0.29  0.344 0.898 0.555 0.033 0.056 0.923]    0
 [0.398 0.418 0.29  0.344 0.898 0.555 0.033 0.056 0.923]    0
 [0.398 0.4
这里提供一个简单的CNN垃圾邮件分类代码示例,仅供参考: ```python import numpy as np import pandas as pd import tensorflow as tf from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense from tensorflow.keras.models import Sequential # 读入数据 data = pd.read_csv('spam.csv', encoding='latin-1') texts = data['v2'].values labels = data['v1'].values # 对文本进行分词和编码 tokenizer = Tokenizer(num_words=10000) tokenizer.fit_on_texts(texts) sequences = tokenizer.texts_to_sequences(texts) # 对文本序列进行填充 maxlen = 100 x = pad_sequences(sequences, maxlen=maxlen) # 对标签进行编码 y = np.zeros(shape=(len(labels), 1)) y[labels == 'spam'] = 1 # 划分训练集和测试集 indices = np.arange(len(texts)) np.random.shuffle(indices) x = x[indices] y = y[indices] train_size = int(len(x) * 0.8) x_train, x_test = x[:train_size], x[train_size:] y_train, y_test = y[:train_size], y[train_size:] # 构建CNN模型 model = Sequential() model.add(Embedding(input_dim=10000, output_dim=32, input_length=maxlen)) model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu')) model.add(GlobalMaxPooling1D()) model.add(Dense(units=1, activation='sigmoid')) model.summary() # 编译模型并进行训练 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.2) # 在测试集上进行评估 results = model.evaluate(x_test, y_test) print('Test loss:', results[0]) print('Test accuracy:', results[1]) ``` 需要注意的是,这只是一个简单的示例,实际应用中还需要对数据进行更详细的预处理、调参等操作。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值