matlab的kNN
knnsearch
Find k-nearest neighbors using dataexpand all in page Syntax IDX = knnsearch(X,Y) [IDX,D] = knnsearch(X,Y) [IDX,D] = knnsearch(X,Y,'Name',Value)
Description
IDX = knnsearch(X,Y) finds the nearest neighbor in X for each point in Y. X is an mx-by-n matrix and Y is an my-by-n matrix. Rows of X and Y correspond to observations and columns correspond to variables. IDX is a column vector with my rows. Each row in IDX contains the index of nearest neighbor in X for the corresponding row in Y.
[IDX,D] = knnsearch(X,Y) returns an my-by-1 vector D containing the distances between each observation in Y and the corresponding closest observation in X. That is, D(i) is the distance between X(IDX(i),:) and Y(i,:).
[IDX,D] = knnsearch(X,Y,'Name',Value) accepts one or more optional comma-separated name/value pairs. Specify Name inside single quotes.
knnsearch,能输出knn的最近邻的下标,如
[n,d] = knnsearch(x,newpoint,'k',10); %Find the 10 sample points closest to the new point.
newpoint2 = [5 1.45;6 2;2.75 .75];
[n2,d2] = knnsearch(x,newpoint2,'k',10); %
输入参数分别是,已知样本,新样本,参数名称及值。新样本可以包含多个,已知样本不包含类别,与
输出参数是,最近邻的下标及与新样本的距离。
Find the species of the 10 neighbors.
tabulate(species(n))
用于输出统计指标。
注意:
For a fixed positive integer K, knnsearch finds the K points in X that are nearest each point in Y. In contrast, for a fixed positive real value r, rangesearch finds all the points in X that are within a distance r of each point in Y.
knnsearch找到最近的k个邻居,而rangesearch找到一定范围内的所有点,二者既相关又不同。其格式与knnsearch类似
idx = rangesearch(X,Y,r)
[idx,D]= rangesearch(X,Y,r)
[idx,D]= rangesearch(X,Y,r,Name,Value)
浙公网安备 33010602011771号