# 导入数据
setwd("E:\\FOR_Study\\Daniel_File\\about_Study\\Junior\\Data_Science\\Data")
load("ch2_sample.RData")
# 文件名读入
data <- read.table("v6.csv", header = T, sep = ",")
# 连续数据离散化
brks <-c (-1, -0.01, 2000, 6000, 10000, 20000, 50000, Inf)
labels <- c("Missing", "0-2000", "2000-6000", "6000-10000",
"10000-20000", "20000-50000", "above 50000")
dpus$WAGP.fix <- cut(dpus$WAGP.fix, breaks=brks, labels, include.lowest=T)
x <- table(cut(HExer, c(-Inf, 7, 10, Inf)))
# 按条件筛选记录
data <- subset(data,with(data,(bmi>=10)&(bmi<=133)&(age>=18)&(age<=150)
&(glucose_max<=900)&(data$glucose_min<=900)
&(tempc_min>=30)&(ph_min>=6)))
# 单次划分
set.seed(128767)
custdata_fix$gp <- runif(dim(custdata_fix)[1])
train_set <- subset(custdata_fix,custdata_fix$gp <= 0.9)
test_set <- subset(custdata_fix,custdata_fix$gp > 0.9)
# ROC评价指标
library('ROCR')
calcAUC <-function(predcol,outcol) {
perf <-performance(prediction(predcol,outcol==pos),'auc')
as.numeric([email protected])
}
# 最大似然指标
loglikelihood<-function(y, py) {
pysmooth<-ifelse(py==0, 1e-12, ifelse(py==1, 1-1e-12, py))
sum(y * log(pysmooth) + (1-y)*log(1 -pysmooth))
}
# 常见指标封装
accuracyMeasures<-function(pred, truth, name="model",value) {
dev.norm<--2*loglikelihood(as.numeric(truth), pred)/length(pred)
ctable<-table(truth==1,pred=(pred>value))
accuracy <-sum(diag(ctable))/sum(ctable)
precision <-ctable[2,2]/sum(ctable[,2])
recall <-ctable[2,2]/sum(ctable[2,])
f1 <-2*precision*recall/(precision+recall)
data.frame(model=name, accuracy=accuracy,precision=precision,recall=recall, f1=f1, dev.norm)
}
# roc曲线
library(ggplot2)
plotROC<-function(predcol,outcol) {
perf<-performance(prediction(predcol,outcol==pos),'tpr','fpr')
pf<-data.frame(
[email protected][[1]],
[email protected][[1]])
ggplot() + geom_line(data=pf,aes(x=FalsePositiveRate,y=TruePositiveRate)) +
geom_line(aes(x=c(0,1),y=c(0,1)))
}
which能够帮助定位索引号,如返回当前列表中最大值对应的索引。
country[which(prob_age2030_god == max(prob_age2030_god))]
如找列名对应的索引
which(names(data) == 'v225')
planes <- group_by(data_select, Market)
Market_category <- summarise(planes,
sum_sales = sum(Sales),
sum_quantity = sum(Quantity),
sum_profit = sum(Profit)) #求和
Market_category