
数据分析实例--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
解析 loss.backward ():深度学习中梯度汇总与同步的自动触发核心 在深度学习模型训练流程中,loss.backward()是连接 “前向计算 ...
2025-09-02要解答 “画 K-S 图时横轴是等距还是等频” 的问题,需先明确 K-S 图的核心用途(检验样本分布与理论分布的一致性),再结合横轴 ...
2025-09-02CDA 数据分析师:助力企业破解数据需求与数据分析需求难题 在数字化浪潮席卷全球的当下,数据已成为企业核心战略资产。无论是市 ...
2025-09-02Power BI 度量值实战:基于每月收入与税金占比计算累计税金分摊金额 在企业财务分析中,税金分摊是成本核算与利润统计的核心环节 ...
2025-09-01巧用 ALTER TABLE rent ADD INDEX:租房系统数据库性能优化实践 在租房管理系统中,rent表是核心业务表之一,通常存储租赁订单信 ...
2025-09-01CDA 数据分析师:企业数字化转型的核心引擎 —— 从能力落地到价值跃迁 当数字化转型从 “选择题” 变为企业生存的 “必答题”, ...
2025-09-01数据清洗工具全景指南:从入门到进阶的实操路径 在数据驱动决策的链条中,“数据清洗” 是决定后续分析与建模有效性的 “第一道 ...
2025-08-29机器学习中的参数优化:以预测结果为核心的闭环调优路径 在机器学习模型落地中,“参数” 是连接 “数据” 与 “预测结果” 的关 ...
2025-08-29CDA 数据分析与量化策略分析流程:协同落地数据驱动价值 在数据驱动决策的实践中,“流程” 是确保价值落地的核心骨架 ——CDA ...
2025-08-29CDA含金量分析 在数字经济与人工智能深度融合的时代,数据驱动决策已成为企业核心竞争力的关键要素。CDA(Certified Data Analys ...
2025-08-28CDA认证:数据时代的职业通行证 当海通证券的交易大厅里闪烁的屏幕实时跳动着市场数据,当苏州银行的数字金融部连夜部署新的风控 ...
2025-08-28PCU:游戏运营的 “实时晴雨表”—— 从数据监控到运营决策的落地指南 在游戏行业,DAU(日活跃用户)、MAU(月活跃用户)是衡量 ...
2025-08-28Excel 聚类分析:零代码实现数据分群,赋能中小团队业务决策 在数字化转型中,“数据分群” 是企业理解用户、优化运营的核心手段 ...
2025-08-28CDA 数据分析师:数字化时代数据思维的践行者与价值推动者 当数字经济成为全球经济增长的核心引擎,数据已从 “辅助性信息” 跃 ...
2025-08-28ALTER TABLE ADD 多个 INDEX:数据库批量索引优化的高效实践 在数据库运维与性能优化中,索引是提升查询效率的核心手段。当业务 ...
2025-08-27Power BI 去重函数:数据清洗与精准分析的核心工具 在企业数据分析流程中,数据质量直接决定分析结果的可靠性。Power BI 作为主 ...
2025-08-27CDA 数据分析师:数据探索与统计分析的实践与价值 在数字化浪潮席卷各行业的当下,数据已成为企业核心资产,而 CDA(Certif ...
2025-08-27t 检验与 Wilcoxon 检验:数据差异比较的两大统计利器 在数据分析中,“比较差异” 是核心需求之一 —— 如新药疗效是否优于旧药 ...
2025-08-26季节性分解外推法:解锁时间序列预测的规律密码 在商业决策、资源调度、政策制定等领域,准确的预测是规避风险、提升效率的关键 ...
2025-08-26CDA 数据分析师:数据治理驱动下的企业数据价值守护者 在数字经济时代,数据已成为企业核心战略资产,其价值的释放离不开高 ...
2025-08-26