mercredi 7 juillet 2021

loop over supervised and unsupervised learning in r

I have a code as below. My goal is to find the relevant df.clstr for (k in 2:5) and implement supervised learning and save the model outcomes as a list. So expected results should be rf and rf.auc from k = 2 till 5. Many thanks in advance.

data(iris); head(iris)
iris2 <- iris[,-5]

tree.mdls <- function(x){
  
  for (k in 2:5) {
    
    
    set.seed(87)
    km.out[[k]] <- kmeans(iris2, centers = k, nstart = 20, iter.max = 50)
    
    df.clstr <- cbind(km.out$cluster, iris2)
    names(df.clstr)[names(df.clstr) == "km.out$cluster"] <- "Cluster.No"
    
    df.clstr$Cluster.No <- factor(df.clstr$Cluster.No)

    set.seed(87)
    inTrainingSet <- createDataPartition(df.clstr$Cluster.No, p=.80,list=0)
    train <- df.clstr[inTrainingSet,]
    test  <- df.clstr[-inTrainingSet,]
    #                                                                           
    xx <- train[,c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")] 
    yy <- train$Cluster.No
    #
    ctrl <- trainControl(method = "cv", number = 2,  verboseIter = TRUE)
    
  }
  
  rf.auc <- boost.auc <- treebag.auc <- NULL
  rf <- NULL
  
  
  if(x == "rf"){
    
    rf <- train(x = xx, y = yy, method = "rf",
                trControl = ctrl, preProcess = c("knnImpute","center","scale"))
    
    rf.TestPred <- predict(rf, test) #,type="prob"
    #head(rf.TestPred)
    
    test$RFclass <- predict(rf, test, type = "prob")
    cnfsion.matrix.rf <- caret::confusionMatrix(data = rf.TestPred, test[["Cluster.No"]] )
    
    rf.TestPred <- as.ordered(rf.TestPred)
    
    rocCurve <- multiclass.roc(response = test$Cluster.No,
                               predictor = as.numeric(rf.TestPred),
                               levels = base::levels(test$Cluster.No),
                               percent = TRUE )
    #attributes(rocCurve)
    rf.auc[[k]] <- auc(rocCurve)
    #
    #return(rf) #return(rf.auc) #print(return(cnfsion.matrix.rf))
    
    output <- list(rf.auc = rf.auc,
                   rf = rf)
    return(output)
    
    
    
  }else if(x == "boost"){
  
    #other model
    
  }else(x == "treebag")
  
    #other model

}

rf1 <- tree.mdls("rf");rf

Aucun commentaire:

Enregistrer un commentaire