代码之家  ›  专栏  ›  技术社区  ›  user3576287

如何使用R中的机器学习和Caret包在新数据集上测试调整后的支持向量机模型?

  •  0
  • user3576287  · 技术社区  · 9 年前

    伙计们!

    我是机器学习方法的新手,对此有疑问。我尝试使用 插入符号 R 启动此方法并使用我的数据集。

    .

    数据集1 ,我有关于基因突变的信息 形式为 Mut公司 非Mut 支持向量机模型 我调整了我的模型,并使用测试数据集(使用ROC曲线)评估了模型的性能。 直到这一步,一切都很顺利。

    我有另一个数据集。 数据集2 没有关于 . 我现在想做的是使用我的 调谐支持向量机模型 上 数据集2 基因A 在 数据集2 Mut/非Mut . 我经历过 插入符号

    我不确定我是否选择了正确的方法。任何建议或帮助都将不胜感激。

    这是我的代码,直到我从第一个数据集优化了我的模型。

    M_train <- Dataset1[Dataset1$Case=='train',-1] #creating train feature data frame
    
    M_test <- Dataset1[Dataset1$Case=='test',-1] #creating test feature data frame
    
    y=as.factor(M_train$Class) # Target variable for training
    
    
    ctrl <- trainControl(method="repeatedcv", # 10fold cross validation
                         repeats=5, # do 5 repititions of cv
                         summaryFunction=twoClassSummary, # Use AUC to pick the best model
                         classProbs=TRUE)
    
    
    #Use the expand.grid to specify the search space 
    #Note that the default search grid selects 3 values of each tuning parameter
    
    grid <- expand.grid(interaction.depth = seq(1,4,by=2), #tree depths from 1 to 4
                        n.trees=seq(10,100,by=10), # let iterations go from 10 to 100
                        shrinkage=c(0.01,0.1), # Try 2 values fornlearning rate 
                        n.minobsinnode = 20)
    
    
    # Set up for parallel processing
    #set.seed(1951)
    registerDoParallel(4,cores=2)
    
    
    #Train and Tune the SVM
    svm.tune <- train(x=M_train,
                      y= M_train$Class,
                      method = "svmRadial",
                      tuneLength = 9, # 9 values of the cost function
                      preProc = c("center","scale"),
                      metric="ROC",
                      trControl=ctrl) # same as for gbm above
    
    #Finally, assess the performance of the model using the test data set.
    
    #Make predictions on the test data with the SVM Model
    svm.pred <- predict(svm.tune,M_test)
    
    confusionMatrix(svm.pred,M_test$Class)
    
    svm.probs <- predict(svm.tune,M_test,type="prob") # Gen probs for ROC
    
    svm.ROC <- roc(predictor=svm.probs$mut,
                   response=as.factor(M_test$Class),
                   levels=y))
    
    plot(svm.ROC,main="ROC for SVM built with GA selected features")
    

    所以,这里是我坚持的地方,我如何使用支持向量机。tune模型预测基因突变 在里面 数据集2 ?

    1 回复  |  直到 9 年前
        1
  •  2
  •   sconfluentus    9 年前

    现在,您只需使用 predict :

    D2.predictions <- predict(svm.tune, newdata = Dataset2)
    

    它们的关键是确保你在这个集合中拥有相同的预测变量,具有相同的列名(在我的偏执世界中,以相同的顺序)。

    D2.predictions