京公网安备 11010802034615号
经营许可证编号:京B2-20210330
数据分析实例--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
用户调研是企业洞察客户需求、优化产品服务、制定运营策略的核心前提,而调研数据的可靠性,直接决定了决策的科学性与有效性。在 ...
2026-05-11在市场竞争日趋激烈、流量成本持续攀升的今天,企业的核心竞争力已从“获取流量”转向“挖掘客户价值”。客户作为企业最宝贵的资 ...
2026-05-11 很多数据分析师精通Excel单元格操作,熟练应用多种公式,但当被问到“表结构数据的基本处理单位是什么”“字段和记录的本质 ...
2026-05-11在互联网运营、产品优化、用户增长等领域,次日留存率是衡量产品价值、用户粘性与运营效果的核心指标,更是判断新用户是否认可产 ...
2026-05-09相关性分析是数据分析领域中用于探究两个或多个变量之间关联强度与方向的核心方法,广泛应用于科研探索、商业决策、医疗研究、社 ...
2026-05-09 数据分析师八成以上的时间在和数据表格打交道,但许多人拿到Excel后习惯性地先算、先分析,结果回头发现漏了一列关键数据, ...
2026-05-09在数据驱动运营的时代,指标是连接业务目标与实际行动的核心桥梁,是企业解读业务现状、发现问题、预判趋势的“量化标尺”。一套 ...
2026-05-08在存量竞争日趋激烈的商业时代,“以客户为中心”早已从口号落地为企业运营的核心逻辑。而客户画像作为打通“了解客户”与“服务 ...
2026-05-08 很多数据分析师每天与Excel打交道,但当被问到“什么是表格结构数据”“它和表结构数据有什么区别”“表格结构数据有哪些核 ...
2026-05-08在数据分析、计量研究等场景中,回归分析是探究变量间量化关系的核心方法,无论是简单的一元线性回归,还是复杂的多元线性回归、 ...
2026-05-07在数据分析、计量研究等场景中,回归分析是探究变量间量化关系的核心方法,无论是简单的一元线性回归,还是复杂的多元线性回归、 ...
2026-05-07 很多数据分析师画过趋势图、做过业绩预测,但当被问到“这个月销售额增长20%,到底是长期趋势自然增长,还是促销活动的短期 ...
2026-05-07在数字化时代,商业竞争的核心已从“经验驱动”转向“数据驱动”,越来越多的企业意识到,商业分析不是简单的数据统计与报表呈现 ...
2026-05-06在Excel数据透视表的实操中,“引用”是连接透视表与公式、辅助数据的核心操作,而相对引用作为最基础、最常用的引用方式,其设 ...
2026-05-06 很多数据分析师做过按月份的销售额趋势图,画过按天的流量折线图,但当被问到“时间序列和普通数据有什么本质区别”“季节性 ...
2026-05-06在Excel数据分析中,数据透视表是汇总、整理海量数据的高效工具,而公式则是实现数据二次计算、逻辑判断的核心功能。实际操作中 ...
2026-04-30Excel透视图是数据分析中不可或缺的工具,它能将透视表中的数据快速可视化,帮助我们直观捕捉数据规律、呈现分析结果。但在实际 ...
2026-04-30 很多数据分析师能熟练地计算指标、搭建标签体系,但当被问到“画像到底在解决什么问题”“画像和标签是什么关系”“画像如何 ...
2026-04-30在中介效应分析中,人口统计学变量(如年龄、性别、学历、收入、职业等)是常见的控制变量或调节变量,其处理方式直接影响分析结 ...
2026-04-29在SQL数据库实操中,日期数据的存储与显示是高频需求,而“数字日期”(如20240520、20241231、45321)是很多开发者、数据分析师 ...
2026-04-29