
数据分析实例--R语言如何对垃圾邮件进行分类
Structure of a Data Analysis
1 数据分析的步骤
l Define the question
l Define the ideal data set
l Determine what data you can access
l Obtain the data
l Clean the data
l Exploratory data analysis
l Statistical prediction/model
l Interpret results
l Challenge results
l Synthesize/write up results
l Create reproducible code
2 A sample
1) 问题.
Can I automatically detect emails that are SPAM or not?
2) 具体化问题
Can I use quantitative characteristics of the emails to classify them as SPAM/HAM?
3) 获取数据
http://search.r-project.org/library/kernlab/html/spam.html
4) 取样
#if it isn't installed,please install the package first.
library(kernlab)
data(spam)
#perform the subsampling
set.seed(3435)
trainIndicator =rbinom(4601,size = 1,prob = 0.5)
table(trainIndicator)
trainSpam = spam[trainIndicator == 1, ]
testSpam = spam[trainIndicator == 0, ]
5) 初步分析
a) Names:查看的列名
names(trainSpam)
b) Head:查看前六行
head(trainSpam)
c) Summaries:汇总
table(trainSpam$type)
d) Plots:画图,查看垃圾邮件及非垃圾邮件的分布
plot(trainSpam$capitalAve ~ trainSpam$type)
上图分布不明显,我们取对数后,再看看
plot(log10(trainSpam$capitalAve + 1) ~ trainSpam$type)
e) 寻找预测的内在关系
plot(log10(trainSpam[, 1:4] + 1))
hCluster = hclust(dist(t(trainSpam[, 1:57])))
plot(hCluster)
太乱了.不能发现些什么。老方法不是取log看看
hClusterUpdated = hclust(dist(t(log10(trainSpam[, 1:55] + 1))))
plot(hClusterUpdated)
6) 统计预测及建模
trainSpam$numType = as.numeric(trainSpam$type) - 1
costFunction = function(x, y) sum(x != (y > 0.5))
cvError = rep(NA, 55)
library(boot)
for (i in 1:55) {
lmFormula = reformulate(names(trainSpam)[i], response = "numType")
glmFit = glm(lmFormula, family = "binomial", data = trainSpam)
cvError[i] = cv.glm(trainSpam, glmFit, costFunction, 2)$delta[2]
}
## Which predictor has minimum cross-validated error?
names(trainSpam)[which.min(cvError)]
7) 检测
## Use the best model from the group
predictionModel = glm(numType ~ charDollar, family = "binomial", data = trainSpam)
## Get predictions on the test set
predictionTest = predict(predictionModel, testSpam)
predictedSpam = rep("nonspam", dim(testSpam)[1])
## Classify as `spam' for those with prob > 0.5
predictedSpam[predictionModel$fitted > 0.5] = "spam"
## Classification table 查看分类结果
table(predictedSpam, testSpam$type)
分类错误率:0.2243 =(61 + 458)/(1346 + 458 + 61 + 449)
8) Interpret results(结果解释)
The fraction of charcters that are dollar signs can be used to predict if an email is Spam
Anything with more than 6.6% dollar signs is classified as Spam
More dollar signs always means more Spam under our prediction
Our test set error rate was 22.4%
9) Challenge results
10) Synthesize/write up results
11) Create reproducible code
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
剖析 CDA 数据分析师考试题型:解锁高效备考与答题策略 CDA(Certified Data Analyst)数据分析师考试作为衡量数据专业能力的 ...
2025-07-04SQL Server 字符串截取转日期:解锁数据处理的关键技能 在数据处理与分析工作中,数据格式的规范性是保证后续分析准确性的基础 ...
2025-07-04CDA 数据分析师视角:从数据迷雾中探寻商业真相 在数字化浪潮席卷全球的今天,数据已成为企业决策的核心驱动力,CDA(Certifie ...
2025-07-04CDA 数据分析师:开启数据职业发展新征程 在数据成为核心生产要素的今天,数据分析师的职业价值愈发凸显。CDA(Certified D ...
2025-07-03从招聘要求看数据分析师的能力素养与职业发展 在数字化浪潮席卷全球的当下,数据已成为企业的核心资产,数据分析师岗位也随 ...
2025-07-03Power BI 中如何控制过滤器选择项目数并在超限时报错 引言 在使用 Power BI 进行数据可视化和分析的过程中,对过滤器的有 ...
2025-07-03把握 CDA 考试时间,开启数据分析职业之路 在数字化转型的时代浪潮下,数据已成为企业决策的核心驱动力。CDA(Certified Da ...
2025-07-02CDA 证书:银行招聘中的 “黄金通行证” 在金融科技飞速发展的当下,银行正加速向数字化、智能化转型,海量数据成为银行精准 ...
2025-07-02探索最优回归方程:数据背后的精准预测密码 在数据分析和统计学的广阔领域中,回归分析是揭示变量之间关系的重要工具,而回 ...
2025-07-02CDA 数据分析师报考条件全解析:开启数据洞察之旅 在当今数字化浪潮席卷全球的时代,数据已成为企业乃至整个社会发展的核心驱 ...
2025-07-01深入解析 SQL 中 CASE 语句条件的执行顺序 在 SQL 编程领域,CASE语句是实现条件逻辑判断、数据转换与分类的重要工 ...
2025-07-01SPSS 中计算三个变量交集的详细指南 在数据分析领域,挖掘变量之间的潜在关系是获取有价值信息的关键步骤。当我们需要探究 ...
2025-07-01CDA 数据分析师:就业前景广阔的新兴职业 在当今数字化时代,数据已成为企业和组织决策的重要依据。数据分析师作为负责收集 ...
2025-06-30探秘卷积层:为何一个卷积层需要两个卷积核 在深度学习的世界里,卷积神经网络(CNN)凭借其强大的特征提取能力 ...
2025-06-30探索 CDA 数据分析师在线课程:开启数据洞察之旅 在数字化浪潮席卷全球的当下,数据已成为企业决策、创新与发展的核心驱 ...
2025-06-303D VLA新范式!CVPR冠军方案BridgeVLA,真机性能提升32% 编辑:LRST 【新智元导读】中科院自动化所提出BridgeVLA模型,通过将 ...
2025-06-30LSTM 为何会产生误差?深入剖析其背后的原因 在深度学习领域,LSTM(Long Short-Term Memory)网络凭借其独特的记忆单元设 ...
2025-06-27LLM进入拖拽时代!只靠Prompt几秒定制大模型,效率飙升12000倍 【新智元导读】最近,来自NUS、UT Austin等机构的研究人员创新 ...
2025-06-27探秘 z-score:数据分析中的标准化利器 在数据的海洋中,面对形态各异、尺度不同的数据,如何找到一个通用的标准来衡量数据 ...
2025-06-26Excel 中为不同柱形设置独立背景(按数据分区)的方法详解 在数据分析与可视化呈现过程中,Excel 柱形图是展示数据的常用工 ...
2025-06-26