MLP分类模型
垃圾邮件
垃圾邮件的数据集可以在kaggle上下载。
数据集一共包含三个文件,data文件是数据文件,其中的每一行都代表一个邮件,一共有4061个邮件,其中有1813个非垃圾邮件,2788个垃圾邮件
目标主要是训练一个全连接神经网络来实现对垃圾邮件的预测
垃圾邮件的数据分类
| SPAM E-MAIL DATABASE ATTRIBUTES (in .names format)
| 48 continuous real [0,100] attributes of type word_freq_WORD
| 6 continuous real [0,100] attributes of type char_freq_CHAR
| 1 continuous real [1,…] attribute of type capital_run_length_average | = average length of uninterrupted sequences of capital letters
| 1 continuous integer [1,…] attribute of type capital_run_length_longest | = length of longest uninterrupted sequence of capital letters
| 1 continuous integer [1,…] attribute of type capital_run_length_total | = sum of length of uninterrupted sequences of capital letters | = total number of capital letters in the e-mail
| 1 nominal {0,1} class attribute of type spam | = denotes whether the e-mail was considered spam (1) or not (0),
准备工作
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
## 导入所需要的模块
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler,MinMaxScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
from sklearn.manifold import TSNE
import torch
import torch.nn as nn
from torch.optim import SGD,Adam
import torch.utils.data as Data
import matplotlib.pyplot as plt
import seaborn as sns
import hiddenlayer as hl
from torchviz import make_dot
读取数据
## 读取数据显示数据的前几行
spam = pd.read_csv("D:\jupyter/spambase.csv")
spam.head()
## 将数据随机切分为训练集和测试集
X = spam.iloc[:,0:57].values
y = spam['58'].values
X_train, X_test, y_train, y_test = train_test_split(
X,y,test_size=0.25, random_state=123
)
## 对数据的前57列特征进行数据标准化处理
scales = MinMaxScaler(feature_range=(0, 1))
X_train_s = scales.fit_transform(X_train)
X_test_s = scales.transform(X_test)
## 使用训练数据集对数据特征进行可视化
## 使用密度曲线对比不同类别在每个特征上的数据分布情况
colname = spam.columns.values[:-1]
plt.figure(figsize=(20,14))
for ii in range(len(colname)):
plt.subplot(7,9,ii+1)
sns.kdeplot(X_train_s[y_train == 0,ii], bw_method=0.05)
sns.kdeplot(X_train_s[y_train == 1,ii], bw_method=0.05)
plt.title(colname[ii])
plt.subplots_adjust(hspace=0.4)
plt.show()
## 将数据转化为张量
X_train_t = torch.from_numpy(X_train_s.astype(np.float32))
y_train_t = torch.from_numpy(y_train.astype(np.int64))
X_test_t = torch.from_numpy(X_test_s.astype(np.float32))
y_test_t = torch.from_numpy(y_test.astype(np.int64))
## 将训练集转化为张量后,使用TensorDataset将X和Y整理到一起
train_data = Data.TensorDataset(X_train_t,y_train_t)
## 定义一个数据加载器,将训练数据集进行批量处理
train_loader = Data.DataLoader(
dataset = train_data, ## 使用的数据集
batch_size=64, # 批处理样本大小
shuffle = True, # 每次迭代前打乱数据
num_workers = 1, # 使用两个进程
)
搭建全连接神经网络
## 全连接网络
class MLPclassifica(nn.Module):
def __init__(self):
super(MLPclassifica,self).__init__()
## 定义第一个隐藏层
self.hidden1 = nn.Sequential(
nn.Linear(
in_features = 57, ## 第一个隐藏层的输入,数据的特征数
out_features = 30,## 第一个隐藏层的输出,神经元的数量
bias=True, ## 默认会有偏置
),
nn.ReLU()
)
## 定义第二个隐藏层
self.hidden2 = nn.Sequential(
nn.Linear(30,10),
nn.ReLU()
)
## 分类层
self.classifica = nn.Sequential(
nn.Linear(10,2),
nn.Sigmoid()
)
## 定义网络的向前传播路径
def forward(self, x):
fc1 = self.hidden1(x)
fc2 = self.hidden2(fc1)
output = self.classifica(fc2)
## 输出为两个隐藏层和输出层的输出
return fc1,fc2,output
## 输出我们的网络结构
mlpc = MLPclassifica()
print(mlpc)
可视化网络结果
## 使用make_dot可视化网络
x = torch.randn(1,57).requires_grad_(True)
y = mlpc(x)
Mymlpcvis = make_dot(y, params=dict(list(mlpc.named_parameters()) + [('x', x)]))
Mymlpcvis
## 将mlpvis保存为图片
Mymlpcvis.format = "png" ## 形式转化为png,默认pdf
# ## 指定文件保存位置
Mymlpcvis.directory = "D:\jupyter/Mymlpc_vis"
Mymlpcvis.view() ## 会自动在当前文件夹生成文件