Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在knngow中返回最近邻索引_R_Knn_Nearest Neighbor - Fatal编程技术网

R 如何在knngow中返回最近邻索引

R 如何在knngow中返回最近邻索引,r,knn,nearest-neighbor,R,Knn,Nearest Neighbor,我想在dprep包中使用knngow。除了为测试数据返回适当的标签外,我还想将行索引返回到最近的邻居(列车内数据)。这个软件包中有没有关于这个工作的功能?我的数据如下 df1<-data.frame(c("a","b","c"),c(1,2,3),c("T","F","T")) df2<-data.frame(c("a","d","f"),c(4,1,3),c("F","F","T")) mylist1<-list() mylist1[[1]]<-df1 mylist1[

我想在dprep包中使用knngow。除了为测试数据返回适当的标签外,我还想将行索引返回到最近的邻居(列车内数据)。这个软件包中有没有关于这个工作的功能?我的数据如下

df1<-data.frame(c("a","b","c"),c(1,2,3),c("T","F","T"))
df2<-data.frame(c("a","d","f"),c(4,1,3),c("F","F","T"))
mylist1<-list()
mylist1[[1]]<-df1
mylist1[[2]]<-df2
tst1<-data.frame(c("f"),c(2))
library(dprep)
for(i in 1:length(mylist1)){
    knn_model<-knngow(mylist1[[i]],tst1,1)}
df1根据您的评论进行更新

我没有看到任何函数返回关于dprep包的列车数据中最近邻的索引(希望我没有遗漏什么)。 但是,您可以首先使用gower距离(软件包)计算距离矩阵,然后将该矩阵传递给k近邻函数(软件包接受距离矩阵作为输入)。如果您决定使用KernelKnn包,那么首先使用devtools::install_github('mlampros/KernelKnn')安装最新版本

如果还需要类标签的概率,请首先转换为数值,然后使用distMat.KernelKnn函数

或者,您可以查看dprep::knngow,尤其是函数的第二部分,它实际上是您感兴趣的部分

> print(dprep::knngow)

....
    else {
        for (i in 1:ntest) {

            tempo = order(StatMatch::gower.dist(test[i, -p], train[, -p]))[1:k]

            classes[i] = moda(train[tempo, p])[1]
        }
    }
.....

请帮帮我。我需要这个问题的答案非常感谢你的建议。但在gowdis函数中,它计算数据帧内样本之间的距离。当我们将该函数的矩阵传递给distMat.knn.index.dist时,因此,对于每个实例,索引给出了同一数据帧内的最近邻。但我的测试样本在一个单独的数据框中,列车数据在另一个数据框中。所以,我想要列车数据中测试实例的最近邻索引。你对此有什么建议吗?谢谢你的帮助
print(idxs)

$test_knn_idx
     [,1] [,2]
[1,]    3    1

$test_knn_dist
     [,1] [,2]
[1,]    0 0.75
y_numeric = as.numeric(df1$col3)

labels = KernelKnn::distMat.KernelKnn(dist_gower, TEST_indices = c(4), y = y_numeric, k = 2, regression = F, threads = 1, Levels = sort(unique(y_numeric)), minimize = T)

print(labels)

     class_1 class_2
[1,]       0       1

# class_2 corresponds to "T" from col3 (df1 data.frame)
> print(dprep::knngow)

....
    else {
        for (i in 1:ntest) {

            tempo = order(StatMatch::gower.dist(test[i, -p], train[, -p]))[1:k]

            classes[i] = moda(train[tempo, p])[1]
        }
    }
.....