我必须实现k个最近邻居在kd-tree中搜索10维数据.
但问题是我的算法对于k = 1来说非常快,但是对于k> 1(k = 2,5,10,20,100),我的算法慢了2000倍
对于kd树来说这是正常的,还是我在做什么?
我正在使用sklearn的KNN实现.虽然我的输入数据有大约20个功能,但我相信其中一些功能比其他功能更重要.有办法:
在相关的说明中,我理解KNN通常不需要培训,但由于sklearn使用KDTrees实现它,因此必须从训练数据生成树.然而,这听起来像是将KNN转变为二叉树问题.是这样的吗?
谢谢.
在哪里可以找到k-最近邻算法的串行C/C++实现?
你知道有这个库吗?
我找到了openCV,但实现已经并行了.
我想从串行实现开始,并使用pthreads openMP和MPI并行化.
谢谢,
亚历克斯
在接受采访时,我遇到了雇主提出的问题.他问我为什么KNN分类器比决策树快得多,例如在字母识别或面部识别中?
那时我完全不知道.所以我想知道在哪些术语中我应该比较速度性能中的两种分类方法?谢谢.
我正试图从包中插入knnreg.出于某种原因,这个训练集有效:
> summary(train1)
V1 V2 V3
13 : 10474 1 : 6435 7 : 8929
10 : 10315 2 : 6435 6 : 8895
4 : 10272 3 : 6435 9 : 8892
1 : 10244 4 : 6435 10 : 8892
2 : 10238 7 : 6435 15 : 8874
24 : 10228 8 : 6435 40 : 8870
(Other):359799 (Other):382960 (Other):368218
Run Code Online (Sandbox Code Playgroud)
虽然这个不起作用:
> summary(train2)
V1 V2 V3 V4
13 : 10474 1 : 6436 7 : 8929 Christmas …Run Code Online (Sandbox Code Playgroud) 与线性回归等其他算法不同,KNN 在训练阶段似乎不执行任何计算。就像在线性回归的情况下,它在训练阶段找到系数。但是 KNN 呢?
我正在尝试拟合KNN模型并使用R中的ISLR包中的Auto数据集获取决策边界。
在这里,我很难确定3类问题的决策边界。到目前为止,这是我的代码。我无法确定决策边界。
我在该网站的其他地方看到了使用ggplot解决此类问题的答案。但是我想使用plot函数以经典方式获得答案。
library("ISLR")
trainxx=Auto[,c(1,3)]
trainyy=(Auto[,8])
n.grid1 <- 50
x1.grid1 <- seq(f = min(trainxx[, 1]), t = max(trainxx[, 1]), l = n.grid1)
x2.grid1 <- seq(f = min(trainxx[, 2]), t = max(trainxx[, 2]), l = n.grid1)
grid <- expand.grid(x1.grid1, x2.grid1)
library("class")
mod.opt <- knn(trainxx, grid, trainyy, k = 10, prob = T)
prob_knn <- attr(mod.opt, "prob")
Run Code Online (Sandbox Code Playgroud)
我的问题主要是在此代码段之后。我非常确定我必须修改以下部分。但是我不知道如何。我是否需要在此处使用“嵌套的条件”?
prob_knn <- ifelse(mod.opt == "3", prob_knn, 1 - prob_knn)
prob_knn <- matrix(prob_knn, n.grid1, n.grid1)
plot(trainxx, col = ifelse(trainyy == "3", "green",ifelse(trainyy=="2", "red","blue")))
title(main = …Run Code Online (Sandbox Code Playgroud) 试图绘制k-NN分类器的决策边界,但无法这样做,但得到TypeError:'(slice(None,None,None),0)'是无效键`
h = .01 # step size in the mesh
# Create color maps
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF','#AFAFAF'])
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF','#AFAFAF'])
for weights in ['uniform', 'distance']:
# we create an instance of Neighbours Classifier and fit the data.
clf = KNeighborsClassifier(n_neighbors=6, weights=weights)
clf.fit(X_train, y_train)
# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, x_max]x[y_min, y_max].
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 …Run Code Online (Sandbox Code Playgroud) I have a time-series dataset with two lables (0 and 1). I am using Dynamic Time Warping (DTW) as a similarity measure for classification using k-nearest neighbour (kNN) as described in these two wonderful blog posts:
http://alexminnaar.com/2014/04/16/Time-Series-Classification-and-Clustering-with-Python.html
Arguments
---------
n_neighbors : int, optional (default = 5)
Number of neighbors to use by default for KNN
max_warping_window : int, optional (default = infinity)
Maximum warping window allowed by the DTW dynamic
programming function
subsample_step : int, optional (default …Run Code Online (Sandbox Code Playgroud)有人为我的matlab程序发光吗?我有来自两个传感器的数据,我正在kNN分别对它们进行分类.在这两种情况下,训练集看起来像一组总共42行的向量,如下所示:
[44 12 53 29 35 30 49;
54 36 58 30 38 24 37;..]
Run Code Online (Sandbox Code Playgroud)
然后我得到一个样本,例如[40 30 50 25 40 25 30],我想将样本分类到最近的邻居.作为接近度的标准,我使用欧几里德度量,sqrt(sum(Y 2)),其中Y是每个元素之间的差异,它给出了Sample和每个训练集类别之间的距离数组.
那么,有两个问题:
补充:到目前为止我正在使用公式:probability = distance/sum of distances但我无法绘制正确的cdf或直方图.这给了我一些分布,但我看到了一个问题,因为如果距离很大,例如700,那么最接近的类将获得最大的概率,但它是错的,因为距离太大而不能与任何课程相比.
任何帮助或评论都非常感谢.
matlab classification knn euclidean-distance probability-density
knn ×10
scikit-learn ×3
python ×2
r ×2
algorithm ×1
c ×1
c++ ×1
kdtree ×1
matlab ×1
r-caret ×1
regression ×1
search ×1
time-series ×1