r - 如何使用 r 并行运行 knn 算法进行多分类

标签 r parallel-processing knn

我有一个多分类问题,我正在尝试运行 KNN 算法来查找每个数据点周围的 50 个最近邻。我在 R 中使用了 FNN 包,但是由于我的数据集有大约 2900 万行,因此需要很长时间。我想知道R中是否有一个可以并行运行KNN的包。您有什么建议及其用法示例吗?

最佳答案

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])

}

关于r - 如何使用 r 并行运行 knn 算法进行多分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770854/

相关文章:

r - 如何在中断分段时间序列回归中向 ggplot 添加线性段

r - 使用 knitr::include_graphics 将图像插入 RMarkdown 时的替代文本?

r - 将工作表添加到 Excel 文件

java - 有什么提高并行/分布式编程的好项目

r - 在具有分类值的 R 中使用 k-NN

pandas - Knn 对距离上的特定特征赋予更多权重

r - 使用两个条件过滤数据集以创建函数

java - 任务执行器与 Java 8 并行流

Javascript并行运行两个循环

python - 从 scikit KNeighborsClassifier 打印最近邻居的标签?