我是编写C代码的初学者。但是,这是从FNN库中提取的,用于测试。
这是我的KNN_ANN.cpp代码:
#include <ANN.h> // ANN header
#include <R.h> // R header
using namespace std;
void Rvector2ANNarray(ANNpointArray data_pts,
double *data,
const int n, const int d)
{
//copy data point address into n x d matrix from d x n R vector column by column
for (int i = 0; i < n; i++) {
data_pts[i] = data+i*d;
}
}
extern "C" {
void get_KNNX_kd(double *data, double* query,
int *k, int *dim,
int *n_pts, int *m_pts,
int *nn_idx, double *nn_dist)
{
const int d=*dim; // Actual Dimension
const int n=*n_pts; // Number of Base Data points
const int m=*m_pts; // Number of Query Data points
const int K = *k; // Max. num of NN
const double error_bound = 0.0;
ANNidxArray index = new ANNidx[K]; // Near neighbor indices
ANNdistArray dist = new ANNdist[K]; // Near neighbor squared distance
ANNpointArray data_pts = new ANNpoint[n];
ANNpointArray query_pts = new ANNpoint[m];
//copy data into matrix from a vector column by column
Rvector2ANNarray(data_pts, data, n, d);
Rvector2ANNarray(query_pts, query, m, d);
ANNkd_tree *kd_tree = new ANNkd_tree(data_pts, n, d);
register int ptr = 0;
for(int i = 0; i < m; i++) // read query points
{
kd_tree->annkSearch( // search
query_pts[i], // query point
K, // number of near neighbors
index, // nearest neighbors index
dist, // squared distance
error_bound); // error bound
for (int j = 0; j < K; j++) //return result row by row
{
nn_dist[ptr] = sqrt(dist[j]); // unsquare distance
nn_idx[ptr] = index[j] + 1; // put indexes in returned array
ptr++;
} // end inner for
} // end for
delete[] index;
delete[] dist;
// delete kd_tree;
// annDeallocPts(data_pts);
// annDeallocPts(query_pts);
delete[] data_pts;
delete[] query_pts;
delete kd_tree;
annClose();
}//end get_KNNX_kd
}//end extern
我已将ANN.h和R.h放在正确的目录中: /Library/Frameworks/R.framework/Versions/3.5/Resources/include
然后我使用以下命令在终端中编译CPP代码以生成KNN_ANN.o和KNN_ANN.so: R CMD SHLIB KNN_ANN.cpp
当我回到R并致电
dyn.load("KNN_ANN.so")
我收到以下错误消息:
Error in dyn.load("KNN_ANN.so") : unable to load shared object '/Users/ypeng/KNN_ANN.so': dlopen(/Users/ypeng/KNN_ANN.so, 6):
找不到符号:__ Z8annClosev引用自: /Users/ypeng/KNN_ANN.so预期输入位置: /Users/ypeng/KNN_ANN.so
任何人,如果我的CPP代码有问题,请告诉我。.
编辑: 这是我提到的基于kmeans集群进行KNN搜索的代码。
Cluster_Scoring <- function(score_table, newdata){ centers <- score_table n_centers <- nrow(centers) #for() #n_1=1:(nrow(newdata)/100) dist_mat <- as.matrix(dist(rbind(centers, newdata[n_1,]))) dist_mat <- dist_mat[-seq(n_centers), seq(n_centers)] a=max.col(-dist_mat) }
再次,此代码仅适用于小型数据集,并且在像我的大型数据集(1.5亿行/ 200列)上运行时返回错误