2016-11-23 4 views
1

Ich habe eine Multiclassification Problem und ich versuche KNN-Algorithmus zu finden, um die 50 nächsten Nachbarn um jeden Datenpunkt zu finden. Ich habe FNN-Paket in R verwendet, aber es dauert lange, da mein Datensatz etwa 29 Millionen Zeilen hat. Ich habe mich gefragt, ob es ein Paket in R gibt, das KNN parallel laufen lässt. Haben Sie einen Vorschlag mit einem Anwendungsbeispiel?Wie kann ich knn Algorithmus parallel mit r für eine Multi-Klassifizierung

Antwort

0
you can use the following by modifying it accordig to KNN .. If need i will provided you with exact code .. the following code is for svc 





pkgs <- c('foreach', 'doParallel') 

lapply(pkgs, require, character.only = T) 

registerDoParallel(cores = 4) 

### PREPARE FOR THE DATA ### 

df1 <- read.csv(...... your dataset path........) 

## do normalization if needed ## 


### SPLIT DATA INTO K FOLDS ### 
set.seed(2016) 

df1$fold <- caret::createFolds(1:nrow(df1), k = 10, list = FALSE) 


### PARAMETER LIST ### 
cost <- 10^(-1:4) 

gamma <- 2^(-4:-1) 

parms <- expand.grid(cost = cost, gamma = gamma) 

### LOOP THROUGH PARAMETER VALUES ### 
result <- foreach(i = 1:nrow(parms), .combine = rbind) %do% { 

    c <- parms[i, ]$cost 

    g <- parms[i, ]$gamma 

    ### K-FOLD VALIDATION ### 

    out <- foreach(j = 1:max(df1$fold), .combine = rbind, .inorder = FALSE) %dopar% { 

deve <- df1[df1$fold != j, ] 

    test <- df1[df1$fold == j, ] 

    mdl <- e1071::svm(Classification-type-column ~ ., data = deve, type = "C-classification", kernel = "radial", cost = c, gamma = g, probability = TRUE) 

    pred <- predict(mdl, test, decision.values = TRUE, probability = TRUE) 
    data.frame(y = test$DEFAULT, prob = attributes(pred)$probabilities[, 2]) 

    } 
    ### CALCULATE SVM PERFORMANCE ### 

    roc <- pROC::roc(as.factor(out$y), out$prob) 

    data.frame(parms[i, ], roc = roc$auc[1]) 

} 
Verwandte Themen