我似乎无法找到关于svmLinear和svmLinear2之间的区别如下页面。
有什么不同?
对于 CART 模型,caret 似乎只提供复杂性参数的调整。有没有办法调整其他参数,例如 minbucket?
我正在使用插入符号拟合模型,但我缺少一些数据。我记得在传递参数以训练“preProcess =”medianImpute”之前有一次,但是我收到了一个意外错误:
library(caret)
x <- mtcars
x[1:5, "cyl"] <- c(NA, NA, NA, NA, NA)
mod.mt <- train(
mpg ~.,
method = "rpart", # decision tree
tuneLength = 3,
preProcess = "medianImpute",
data = x)
Run Code Online (Sandbox Code Playgroud)
给出:
Error in na.fail.default(list(mpg = c(21, 21, 22.8, 21.4, 18.7, 18.1, :
missing values in object
Run Code Online (Sandbox Code Playgroud)
因为我正在使用 preProcess 我以为我是在告诉 caret 对任何缺失值使用中值插补。所以这个错误是出乎意料的?
使用 RFE,您可以获得特征的重要性等级,但现在我只能使用包内的模型和参数,例如:lmFuncs(linear model),rfFuncs(random forest)
似乎
caretFuncs
Run Code Online (Sandbox Code Playgroud)
可以对自己的模型和参数做一些自定义设置,但是我不知道细节,正式文档没有给出细节,我想在这个RFE过程中应用svm和gbm,因为这是我当前使用的模型训练,有人知道吗?
我正在使用虹膜数据集在R中练习SVM,并且我想从模型中获取特征权重/系数,但是鉴于我的输出为我提供了32个支持向量,因此我认为我可能会误解某些东西。假设我要分析四个变量,我将得到四个。我知道使用该svm()函数时有一种方法,但是我尝试使用train()插入符号中的函数来生成我的SVM。
library(caret)
# Define fitControl
fitControl <- trainControl(## 5-fold CV
method = "cv",
number = 5,
classProbs = TRUE,
summaryFunction = twoClassSummary )
# Define Tune
grid<-expand.grid(C=c(2^-5,2^-3,2^-1))
##########
df<-iris head(df)
df<-df[df$Species!='setosa',]
df$Species<-as.character(df$Species)
df$Species<-as.factor(df$Species)
# set random seed and run the model
set.seed(321)
svmFit1 <- train(x = df[-5],
y=df$Species,
method = "svmLinear",
trControl = fitControl,
preProc = c("center","scale"),
metric="ROC",
tuneGrid=grid )
svmFit1
Run Code Online (Sandbox Code Playgroud)
我以为这很简单,svmFit1$finalModel@coef但是当我相信我应该得到4时,我得到了32个向量。为什么呢?
我想我已经被派到这里接受培训了。
library(caret)
library(mlbench)
library(plotROC)
library(pROC)
data(Sonar)
ctrl <- trainControl(method="cv",
summaryFunction=twoClassSummary,
classProbs=T,
savePredictions = T)
rfFit <- train(Class ~ ., data=Sonar,
method="rf", preProc=c("center", "scale"),
trControl=ctrl)
# Select a parameter setting
selectedIndices <- rfFit$pred$mtry == 2
Run Code Online (Sandbox Code Playgroud)
我想绘制 ROC。
plot.roc(rfFit$pred$obs[selectedIndices],
rfFit$pred$M[selectedIndices])
Run Code Online (Sandbox Code Playgroud)
然而,当我尝试 ggplot2 方法时,它给了我完全不同的东西。
g <- ggplot(rfFit$pred[selectedIndices, ], aes(m=M, d=factor(obs, levels = c("R", "M")))) +
geom_roc(n.cuts=0) +
coord_equal() +
style_roc()
g + annotate("text", x=0.75, y=0.25, label=paste("AUC =", round((calc_auc(g))$AUC, 4)))
Run Code Online (Sandbox Code Playgroud)
我在这里做了一些非常错误的事情,但我不知道它是什么。谢谢。
我注意到predict()只会在完整案例中创建预测.我已经包含medianImpute在preProcess选项中,例如:
train(outcome ~ .,
data = df,
method = "rf",
tuneLength = 5,
preProcess = c("YeoJohnson", "center", "scale", "medianImpute"),
metric = 'ROC',
trControl = train_ctrl)
}
Run Code Online (Sandbox Code Playgroud)
这是否意味着我应该在训练集之前对缺失的值进行估算?如果没有,我无法为测试集中的所有情况创建预测.我曾在Kuhn博士的书中读过,在交叉验证过程中应该进行预处理......谢谢!
我正在尝试使用R的Caret包来使用应用于UCI机器学习中的"鲍鱼"数据库的KNN(链接到数据).但是当有分类值时,它不允许使用KNN.如何将分类值(在此数据库中"M","F","I")转换为数值,例如1,2,3?
我发现了与此类似的问题,但并没有解决我的问题:我使用插入号和游侠方法来拟合随机森林,然后使用预测来预测我的评估数据。这可行。但是,当我尝试获取预测概率时,出现以下错误:
[.data.frame(out,,obsLevels,drop = FALSE)中的错误:未定义的列已选择
代码(示例)
require(caret)
mtcars$carb <- as.factor(mtcars$carb)
tuneGrid <- expand.grid(mtry = c(10), min.node.size = c(1), splitrule = "extratrees")
rf_model<-train(carb~.,data=mtcars,method="ranger",
trControl=trainControl(method="none")
, tuneGrid = tuneGrid
)
predict(rf_model, mtcars, type="prob")
Run Code Online (Sandbox Code Playgroud)
我确保碳水化合物是其他地方建议的因素。
有什么想法吗?
可以使用mlr?进行递归特征消除功能(rfe)。我知道用插入号可以实现此功能,但是即使有一些有关使用mlr选择功能的文档,我也找不到与rfe等效的文档。