实战数据科学|1数据科学过程的代码
专题介绍:R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来。得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器的R Studio(用户的界面体验更好)。也正是由于全世界越来越多的数据科学社区和用户对R包的慷慨贡献,让R语言在全球范围内越来越流行。其中一些R包,例如MASS,SparkR, ggplot2,使数据操作,可视化和计算功能越来越强大。R是用于统计分析、绘图的语言和操作环境。R是属于GNU系统的一个自由、免费、源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具。R作为一种统计分析软件,是集统计分析与图形显示于一体的。它可以运行于UNIX、Windows和Macintosh的操作系统上,而且嵌入了一个非常方便实用的帮助系统,相比于其他统计分析软件,R的学术性开发比较早,适合生物学和医学等学术学科的科研人员使用。
点击上方蓝字关注我,一起学习R语言
-
数据处理
-
数据建模
-
模型性能评价
##########################
#实战数据科学第一章R语言代码
#数据科学过程
#1)数据科学是什么?
#2)数据科学项目角色和过程
#3)数据科学预期
#########################
# 第一步:数据处理阶段
d <- read.table('german.data',
sep = " ",
stringsAsFactors = FALSE, header = FALSE)
colnames(d) <- c('Status_of_existing_checking_account', 'Duration_in_month',
'Credit_history', 'Purpose', 'Credit_amount', 'Savings_account_bonds',
'Present_employment_since',
'Installment_rate_in_percentage_of_disposable_income',
'Personal_status_and_sex', 'Other_debtors_guarantors',
'Present_residence_since', 'Property', 'Age_in_years',
'Other_installment_plans', 'Housing',
'Number_of_existing_credits_at_this_bank', 'Job',
'Number_of_people_being_liable_to_provide_maintenance_for',
'Telephone', 'foreign_worker', 'Good_Loan')
str(d)
View(head(d))
d$Good_Loan <- as.factor(ifelse(d$Good_Loan == 1, 'GoodLoan', 'BadLoan'))
View(head(d))
mapping <- c('A11' = '... < 0 DM',
'A12' = '0 < = ... < 200 DM',
'A13' = '... > = 200 DM / salary assignments for at least 1 year',
'A14' = 'no checking account',
'A30' = 'no credits taken/all credits paid back duly',
'A31' = 'all credits at this bank paid back duly',
'A32' = 'existing credits paid back duly till now',
'A33' = 'delay in paying off in the past',
'A34' = 'critical account/other credits existing (not at this bank)',
'A40' = 'car (new)',
'A41' = 'car (used)',
'A42' = 'furniture/equipment',
'A43' = 'radio/television',
'A44' = 'domestic appliances',
'A45' = 'repairs',
'A46' = 'education',
'A47' = '(vacation - does not exist?)',
'A48' = 'retraining',
'A49' = 'business',
'A410' = 'others',
'A61' = '... < 100 DM',
'A62' = '100 < = ... < 500 DM',
'A63' = '500 < = ... < 1000 DM',
'A64' = '.. > = 1000 DM',
'A65' = 'unknown/ no savings account',
'A71' = 'unemployed',
'A72' = '... < 1 year',
'A73' = '1 < = ... < 4 years',
'A74' = '4 < = ... < 7 years',
'A75' = '.. > = 7 years',
'A91' = 'male : divorced/separated',
'A92' = 'female : divorced/separated/married',
'A93' = 'male : single',
'A94' = 'male : married/widowed',
'A95' = 'female : single',
'A101' = 'none',
'A102' = 'co-applicant',
'A103' = 'guarantor',
'A121' = 'real estate',
'A122' = 'if not A121 : building society savings agreement/life insurance',
'A123' = 'if not A121/A122 : car or other, not in attribute 6',
'A124' = 'unknown / no property',
'A141' = 'bank',
'A142' = 'stores',
'A143' = 'none',
'A151' = 'rent',
'A152' = 'own',
'A153' = 'for free',
'A171' = 'unemployed/ unskilled - non-resident',
'A172' = 'unskilled - resident',
'A173' = 'skilled employee / official',
'A174' = 'management/ self-employed/highly qualified employee/ officer',
'A191' = 'none',
'A192' = 'yes, registered under the customers name',
'A201' = 'yes',
'A202' = 'no')
for(ci in colnames(d)) {
if(is.character(d[[ci]])) {
d[[ci]] <- as.factor(mapping[d[[ci]]])
}
}
vars <- setdiff(colnames(d), 'Good_Loan')
vars
creditdata <- d
saveRDS(creditdata, "creditdata.RDS")
# 第二步:数据建模阶段
library("rpart")
load("GCDData.RData")
model <- rpart(Good_Loan ~
Duration_in_month +
Installment_rate_in_percentage_of_disposable_income +
Credit_amount +
Other_installment_plans,
data = d,
control = rpart.control(maxdept = 4),
method = "class")
library("rpart.plot")
print(model)
rpart.plot(model)
# 第三步:模型性能评价阶段
d$Loan.status <- d$Good_Loan
# 1) 生成混淆矩阵
conf_mat <- table(actual = d$Loan.status, pred = predict(model, type = 'class'))
conf_mat
# 2)模型性能评价的指标(准确率,查准率,查全率,假阳率)
(accuracy <- sum(diag(conf_mat)) / sum(conf_mat))
(precision <- conf_mat["BadLoan", "BadLoan"] / sum(conf_mat[, "BadLoan"]))
(recall <- conf_mat["BadLoan", "BadLoan"] / sum(conf_mat["BadLoan", ]))
(fpr <- conf_mat["GoodLoan","BadLoan"] / sum(conf_mat["GoodLoan", ]))
推荐阅读
1 R语言机器学习3本经典书籍集合本,提高你的R语言和机器学习能力!(可供下载)
2 R数据分析和可视化培训课程书籍,5大模块,助你学习数据分析和挖掘技术(可供下载)
3 R语言实战英文书籍,配套源代码,帮助你学习R语言!(可下载)
推荐公众号:数据科学与人工智能
数据科学与人工智能公众号推广Python语言,数据科学与人工智能的知识和信息。扫码下方二维码关注我,一起学习Python语言和数据科学与人工智能。
依托【R语言】公众号,我创建了R语言群,群友们每天都会就R语言的主题进行交流和分享。需要加入R语言群的朋友,可以扫码加我的个人微信,请备注【姓名-入群】。我诚邀你加入群,大家相互学习和共同进步。
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!