我正在尝试使用hmeasure指标Hand,2009作为我在插入符号中训练SVM的自定义指标.由于我使用R相对较新,我尝试调整了twoClassSummary函数.我需要的是将真实的类标签和预测的类概率从模型(svm)传递到hmeasure包中的HMeasure函数,而不是使用ROC或插入符号中的其他分类性能度量.
例如,在R-HMeasure(true.class,predictProbs [,2])中调用HMeasure函数会导致计算Hmeasure.使用下面的twoClassSummary代码调整会导致返回错误:'x'必须是数字.
也许该列车功能不能"看到"预测的概率来评估HMeasure函数.我怎样才能解决这个问题?
我已经阅读了文档,并提出了关于SO 处理回归的问题.多数民众赞成让我走了一路.我将不胜感激任何帮助或指向解决方案.
library(caret)
library(doMC)
library(hmeasure)
library(mlbench)
set.seed(825)
data(Sonar)
table(Sonar$Class)
inTraining <- createDataPartition(Sonar$Class, p = 0.75, list = FALSE)
training <- Sonar[inTraining, ]
testing <- Sonar[-inTraining, ]
# using caret
fitControl <- trainControl(method = "repeatedcv",number = 2,repeats=2,summaryFunction=twoClassSummary,classProbs=TRUE)
svmFit1 <- train(Class ~ ., data = training,method = "svmRadial",trControl = fitControl,preProc = c("center", "scale"),tuneLength = 8,metric = "ROC")
predictedProbs <- predict(svmFit1, newdata = testing , type = "prob")
true.class<-testing$Class
hmeas<- HMeasure(true.class,predictedProbs[,2]) # suppose its …Run Code Online (Sandbox Code Playgroud) 我调整了使用包中的函数的mtry参数.我的数据中只有列,但返回为最佳值,而这不是有效值().那是什么解释?randomForesttraincaret48Xtrainmtry=50>48
> dim(X)
[1] 93 48
> fit <- train(level~., data=data.frame(X,level), tuneLength=13)
> fit$finalModel
Call:
randomForest(x = x, y = y, mtry = param$mtry)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 50
OOB estimate of error rate: 2.15%
Confusion matrix:
high low class.error
high 81 1 0.01219512
low 1 10 0.09090909
Run Code Online (Sandbox Code Playgroud)
如果我不设置tuneLength参数,情况会更糟:
> fit <- train(level~., data=data.frame(X,level))
> fit$finalModel …Run Code Online (Sandbox Code Playgroud) 我使用插入库来计算二元分类问题的类概率和预测,使用10倍交叉验证和5次重复.
现在我有一个TRUE(每个数据点的观测值)值,PREDICTED(通过算法)值,Class 0概率和Class 1概率,它们被算法用来预测类标签.
现在我如何roc使用其中一个ROCR或pROC库创建一个对象然后计算auc值?
假设我将所有这些值存储在predictionsdataframe中.例如predictions$pred,predictions$obs分别是预测值和真值,依此类推......
我试图在R中创建一个序数回归树rpart,其预测变量主要是序数数据,存储factor在R中.
当我使用创建树时rpart,我得到这样的东西:

其中值是因子值(例如A170,标签的范围从-5到10).
然而,当我使用caret来train使用数据rpart,当我解压的最终模型,树不再具有有序预测.请参阅下面的示例输出树

如上所示,似乎序数变量A170现在已经转换为多个虚拟分类值,即A17010在第二个树中是虚拟A170值10.
那么,在使用caret包装树时,是否可以保留序数变量而不是将因子变量转换为多个二元指示变量?
我正在使用Caret的PCI预处理.
multinomFit <- train(LoanStatus~., train, method = "multinom", std=TRUE, family=binomial, metric = "ROC", thresh = 0.85, verbose = TRUE, pcaComp=7, preProcess=c("center", "scale", "pca"),
trControl = ctrl)
Run Code Online (Sandbox Code Playgroud)
我指定,PCA组件的数量为7.为什么摘要显示使用68个组件的拟合?
summary(multinomFit)
Call:
multinom(formula = .outcome ~ ., data = dat, decay = param$decay,
std = TRUE, family = ..2, thresh = 0.85, verbose = TRUE,
pcaComp = 7)
Coefficients:
Values Std. Err.
(Intercept) 1.6650694329 0.03760419
PC1 -0.1023790683 0.01474812
PC2 0.0375344688 0.01554707
PC3 -0.1012080589 0.01870754
PC4 -0.1004020357 0.02418817
PC5 0.0707421015 0.02403815
PC6 0.0034671796 0.02535015 …Run Code Online (Sandbox Code Playgroud) 我正在使用由caret软件包生成的SVM-RFE模型处理交叉验证数据(重复10倍,重复5次)。我知道该caret软件包pROC在计算指标时可以与软件包一起使用,但是我需要使用ROCR软件包才能获得平均ROC。但是,我注意到使用每个软件包时的平均AUC值不相同,因此我不确定是否应区分使用两个软件包。
我用来证明的代码是:
predictions_NG3<-list()
labels_NG3<-list()
optSize <- svmRFE_NG3$optsize
resamples<-(split(svmRFE_NG3$pred,svmRFE_NG3$pred$Variables))
resamplesFOLD<-(split(resamples[[optSize]],resamples[[optSize]]$Resample))
auc_pROC <- vector()
auc_ROCR <- vector()
for (i in 1:50){
predictions_NG3[[i]]<-resamplesFOLD[[i]]$LUNG
labels_NG3[[i]]<-resamplesFOLD[[i]]$obs
#WITH pROC
rocCurve <- roc(response = labels_NG3[[i]],
predictor = predictions_NG3[[i]],
levels = c("BREAST","LUNG")) #LUNG POSITIVE
auc_pROC <- c(auc_pROC,auc(rocCurve))
#WITH ROCR
pred_ROCR <- prediction(predictions_NG3[[i]], labels_NG3[[i]],
label.ordering = c("BREAST","LUNG")) #LUNG POSITIVE
auc_ROCR <- c(auc_ROCR,performance(pred_ROCR,"auc")@y.values[[1]])
}
auc_mean_pROC <- mean(auc_pROC)
auc_sd_pROC <- sd(auc_pROC)
auc_mean_ROCR <- mean(auc_ROCR)
auc_sd_ROCR <- sd(auc_ROCR)
Run Code Online (Sandbox Code Playgroud)
结果略有不同:
auc_mean_pROC auc_sd_pROC auc_mean_ROCR auc_sd_ROCR
1 …Run Code Online (Sandbox Code Playgroud) 使用caret包,在创建数据分区75%培训和25%测试时,我们使用:
inTrain<- createDataPartition(y=spam$type,p=0.75, list=FALSE)
Run Code Online (Sandbox Code Playgroud)
注意:数据集已命名spam,目标变量已命名type
我的问题是,包括y=spam$type论证的目的是什么?
创建数据分区的目的不是简单地根据培训与测试所需的比例拆分整个数据集吗?为什么需要在代码中包含该参数?
当我只使用mtry参数作为tuingrid,它工作,但当我添加ntree参数时,错误变为Error in train.default(x, y, weights = w, ...): The tuning parameter grid should have columns mtry.代码如下:
require(RCurl)
require(prettyR)
library(caret)
url <- "https://raw.githubusercontent.com/gastonstat/CreditScoring/master/CleanCreditScoring.csv"
cs_data <- getURL(url)
cs_data <- read.csv(textConnection(cs_data))
classes <- cs_data[, "Status"]
predictors <- cs_data[, -match(c("Status", "Seniority", "Time", "Age", "Expenses",
"Income", "Assets", "Debt", "Amount", "Price", "Finrat", "Savings"), colnames(cs_data))]
train_set <- createDataPartition(classes, p = 0.8, list = FALSE)
set.seed(123)
cs_data_train = cs_data[train_set, ]
cs_data_test = cs_data[-train_set, ]
# Define the tuned parameter
grid …Run Code Online (Sandbox Code Playgroud) 关于R中并行后端的2个问题:
1)parallel::detectCores()我的机器上没有检测到正确的内核数量:
parallel::detectCores(logical = FALSE)
[1] 24
parallel::detectCores(logical = TRUE)
[1] 48
Run Code Online (Sandbox Code Playgroud)
2)当运行插入符号模型时,每当我选择超过64个核心时,即使每个进程都已成功完成(因为它出现在日志中),模型也无法完成.无论我在32核计算机上尝试使用多少核心,我都会发生这种情况,当我运行makeCluster(64)它时,它会工作并makeCluster(65)挂起.
library(caret)
library(doParallel)
library(xgboost)
iris <- iris[1:100,]
iris$Species <- as.factor(as.character(iris$Species))
tc <- trainControl(method="repeatedcv",
classProb = TRUE,
verboseIter = TRUE,
allowParallel = TRUE)
stopCluster(cl)
cl <- makeCluster(65, outfile="D:\\R_data\\Log\\test.txt")
registerDoParallel(cl)
system.time(
train.rf <- train(Species ~ .,data=iris, method="xgbTree", trControl=tc, metric = "Accuracy", verbose = TRUE)
)
Run Code Online (Sandbox Code Playgroud)
上面挂起20-30分钟或更长时间,永远不会执行,而下面的代码在20秒内完成:
> cl <- makeCluster(64, outfile="D:\\R_data\\Log\\test2.txt")
> registerDoParallel(cl)
> system.time(
+ train.rf <- train(Species ~ .,data=iris, method="xgbTree", trControl=tc, metric …Run Code Online (Sandbox Code Playgroud)