在插入符 (R) 中如何将两个 class 概率得分列合并为一个 "predict(model,test_set,type="prob 之后的概率 Class 列")"
In Caret (R) How to consolidate two class probability score columns to one Probability Class column after "predict(model,test_set,type="prob")"
我想将两个概率得分列合并为一个概率得分列(见下图)
mtcars
library(caret)
#print(colnames(dat))
set.seed(7117)
ind <- createDataPartition(mtcars$am, p = .7, list = FALSE)
train_set <- mtcars[ind, ]
test_set <- mtcars[-ind, ]
train_set
test_set
###################################################################################################
train_ctrl = trainControl(
method = "cv", #Specifying Cross validation
number = 3 # Specifying 3-fold #
)
set.seed(40198)
model <- train(as.factor(am) ~ .,
data = train_set,
method = "rf",
trControl = train_ctrl,
# linout = T,
preProc = c("center", "scale"))
model
# need help after `predict(model,test_set,type="prob")`
Prob_score <- predict(model,test_set,type="prob") # returns probability of both '0' and '1' classes, I want a consolidated Probability score of corresponding class
Pred_Class<-predict(model,test_set)
df <-cbind(Pred_Class,Prob_score)
期望的输出
#而不是第 0 列和第 1 列,我只想 return ONE
与下图中突出显示的 Pred_Class
相对应的概率列。
我不确定为什么我不能用相同的种子得到你的输出,但这是你最后一行代码的输出:
cbind(Pred_Class,Prob_score)
Pred_Class 0 1
Hornet 4 Drive 0 0.824 0.176
Duster 360 0 0.916 0.084
Merc 450SL 0 0.986 0.014
Merc 450SLC 0 0.978 0.022
Cadillac Fleetwood 0 0.994 0.006
Toyota Corona 1 0.322 0.678
Camaro Z28 0 0.758 0.242
Ford Pantera L 0 0.602 0.398
Ferrari Dino 1 0.308 0.692
您可以使用apply(..,1,..)
取每一行的最大值:
data.frame(Pred_Class,prob = apply(Prob_score,1,max))
Pred_Class prob
Hornet 4 Drive 0 0.824
Duster 360 0 0.916
Merc 450SL 0 0.986
Merc 450SLC 0 0.978
Cadillac Fleetwood 0 0.994
Toyota Corona 1 0.678
Camaro Z28 0 0.758
Ford Pantera L 0 0.602
Ferrari Dino 1 0.692
我想将两个概率得分列合并为一个概率得分列(见下图)
mtcars
library(caret)
#print(colnames(dat))
set.seed(7117)
ind <- createDataPartition(mtcars$am, p = .7, list = FALSE)
train_set <- mtcars[ind, ]
test_set <- mtcars[-ind, ]
train_set
test_set
###################################################################################################
train_ctrl = trainControl(
method = "cv", #Specifying Cross validation
number = 3 # Specifying 3-fold #
)
set.seed(40198)
model <- train(as.factor(am) ~ .,
data = train_set,
method = "rf",
trControl = train_ctrl,
# linout = T,
preProc = c("center", "scale"))
model
# need help after `predict(model,test_set,type="prob")`
Prob_score <- predict(model,test_set,type="prob") # returns probability of both '0' and '1' classes, I want a consolidated Probability score of corresponding class
Pred_Class<-predict(model,test_set)
df <-cbind(Pred_Class,Prob_score)
期望的输出
#而不是第 0 列和第 1 列,我只想 return ONE
与下图中突出显示的 Pred_Class
相对应的概率列。
我不确定为什么我不能用相同的种子得到你的输出,但这是你最后一行代码的输出:
cbind(Pred_Class,Prob_score)
Pred_Class 0 1
Hornet 4 Drive 0 0.824 0.176
Duster 360 0 0.916 0.084
Merc 450SL 0 0.986 0.014
Merc 450SLC 0 0.978 0.022
Cadillac Fleetwood 0 0.994 0.006
Toyota Corona 1 0.322 0.678
Camaro Z28 0 0.758 0.242
Ford Pantera L 0 0.602 0.398
Ferrari Dino 1 0.308 0.692
您可以使用apply(..,1,..)
取每一行的最大值:
data.frame(Pred_Class,prob = apply(Prob_score,1,max))
Pred_Class prob
Hornet 4 Drive 0 0.824
Duster 360 0 0.916
Merc 450SL 0 0.986
Merc 450SLC 0 0.978
Cadillac Fleetwood 0 0.994
Toyota Corona 1 0.678
Camaro Z28 0 0.758
Ford Pantera L 0 0.602
Ferrari Dino 1 0.692