主要内容

createns

创建最近邻居搜索器对象

Description

例子

NS= createns()创建令人疲惫的orKDTreeSearcher使用n-通过-K数值矩阵of the training data.

例子

NS= createns(,名称、值)specifies additional options using one or more name-value pair arguments. For example, you can specifyN方法to determine which type of object to create.

Examples

全部折叠

装载Fisher的Iris数据集。

负载鱼肝菌十= meas; [n,k] = size(X)
n=150
k= 4

has 150 observations and 4 predictors.

Prepare an exhaustive nearest neighbor searcher using the entire data set as training data.

mdl1 = utifiventsearcher(x)
Mdl1=穷尽搜索器,属性:Distance:“euclidean”DistParameter:[]X:[150x4 double]

Mdl1是an令人疲惫的模型对象,其属性将显示在命令窗口中。对象包含有关训练算法的信息,例如距离度量。可以使用点表示法更改属性值。

Alternatively, you can prepare an exhaustive nearest neighbor searcher by usingcreatens并指定'exhaustive'作为搜索方法。

mdl2 = createns(x,'nsmethod','exhaustive')
Mdl2 = ExhaustiveSearcher属性:Distance: 'euclidean' DistParameter: [] X: [150x4 double]

Mdl2型也是一个令人疲惫的模型对象,它等效于Mdl1.

搜索对于最近的邻居批量查询数据,通过令人疲惫的模型对象和查询数据KNN搜索orrangesearch.

生长four-dimensionalK使用欧几里德距离的D树。

装载Fisher的Iris数据集。

负载鱼肝菌十= meas; [n,k] = size(X)
n=150
k= 4

has 150 observations and 4 predictors.

生长four-dimensionalK用整个数据集作为训练数据的d-树。

Mdl1=KDTreeSearcher(X)
Mdl1=KDTreeSearcher,属性:BucketSize:50距离:“euclidean”DistParameter:[]X:[150x4 double]

Mdl1是aKDTreeSearcher模型对象,其属性将显示在命令窗口中。该对象包含有关成长的四维空间的信息Kd-tree, such as the distance metric. You can alter property values using dot notation.

Alternatively, you can grow aK使用d-树createns.

mdl2 = createens(x)
Mdl2=KDTreeSearcher,属性:BucketSize:50距离:“euclidean”DistParameter:[]X:[150x4 double]

Mdl2型也是A.KDTreeSearcher模型对象,它等效于Mdl1.因为has four columns and the default distance metric is Euclidean,createnscreates aKDTreeSearcher米odel by default.

To find the nearest neighbors in对于一批查询数据,传递KDTreeSearcher模型对象和查询数据KNN搜索orrangesearch.

生长Kd-tree that uses the Minkowski distance with an exponent of five.

加载Fisher的虹膜数据集。为花瓣尺寸创建一个变量。

负载鱼肝菌X=测量值(:,3:4);

生长Kd树。使用指数5指定Minkowski距离。

Mdl = createns(X,'Distance',“明可夫斯基”,“P”5)
Mdl = KDTreeSearcher with properties: BucketSize: 50 Distance: 'minkowski' DistParameter: 5 X: [150x2 double]

因为有两列,距离度量是Minkowski,createnscreates aKDTreeSearcher模型对象by default.

通过使用使用穷举搜索器对象createns功能。将对象和查询数据传递给KNN搜索要查找的函数k-最近的邻居。

装载Fisher的Iris数据集。

负载鱼肝菌

Remove five irises randomly from the predictor data to use as a query set.

RNG('默认');重复性的%n=尺寸(meas,1);%样本量qIdx = randsample(n,5);%数据查询X=测量值(~ismember(1:n,qIdx),:);Y=测量值(qIdx,:);

Prepare an exhaustive nearest neighbor searcher using the training data. Specify the Mahalanobis distance for finding nearest neighbors.

Mdl = createns(X,'Distance','mahalanobis')
Mdl = ExhaustiveSearcher with properties: Distance: 'mahalanobis' DistParameter: [4x4 double] X: [145x4 double]

因为距离度量是mahalanobis,createns创建令人疲惫的模型对象by default.

The software uses the covariance matrix of the predictors (columns) in the training data for computing the Mahalanobis distance. To display this value, useMdl.DistParameter.

Mdl.DistParameter
答案=4×4个0.6547 -0.0368 1.2320 0.5026 -0.0368 0.1914 -0.3227 -0.1193 1.2320 -0.3227 3.0671 1.2842 0.5026 -0.1193 1.2842 0.5800

Find the indices of the training data (Mdl.X)that are the two nearest neighbors of each point in the query data (是的).

IdxNN公司= knnsearch(Mdl,Y,'K'2)
IdxNN公司=5×2个5 6 98 95 104 128 135 65 102 115

Each row ofIdxNN公司corresponds to a query data observation. The column order corresponds to the order of the nearest neighbors with respect to ascending distance. For example, based on the Mahalanobis metric, the second nearest neighbor of是(3,:)十(128,:).

输入参数

全部折叠

训练数据,指定为数值矩阵。hasn行,每个行对应一个观察值(即实例或示例),以及K列,每个列对应于预测器(即,特征)。

Data Types:single|double

名称-值对参数

指定可选的逗号分隔对名称、值论点。Name是参数名,并且Value是the corresponding value.Name必须出现在引号内。您可以按以下任意顺序指定多个名称和值对参数:name1,value1,...,namen,valuen.

例:NS=createns(X,'Distance','mahalanobis')创建令人疲惫的在搜索最近的邻居时使用Mahalanobis距离度量的模型对象。
For Exhaustive andKD-Tree最近的邻居搜索者

全部折叠

最近邻搜索法used to define the type of object created, specified as the comma-separated pair consisting of'nsmethod'and“kdtree”or'exhaustive'.

  • “kdtree”createnscreates aKDTreeSearcher使用Kd-tree algorithm.

  • 'exhaustive'createns创建令人疲惫的使用exhaustive search algorithm.

The default value is“kdtree”当这三个条件成立时:

  • The number of columns of(K)是less than or equal to 10 (that is,K≤ 10).

  • 不是稀疏。

  • 距离“欧几里得”,'cityblock','切比切夫',or“明可夫斯基”.

否则,默认值为'exhaustive'.

例:'nsmethod','exhaustive'

呼叫时使用的距离度量KNN搜索orrangesearch找到未来查询点的最近邻居,指定为逗号分隔的对组成'Distance'and a character vector or string scalar of distance metric name or function handle.

对于两种类型的最近邻景人,createnssupports these distance metrics.

Value Description
'切比切夫' 切比雪夫距离(最大坐标差)。
'cityblock' City block distance.
“欧几里得” 欧几里得距离。
“明可夫斯基” Minkowski距离。默认指数为2.要指定其他指数,请使用“P”名称-值对参数。

如果createns使用详尽的搜索算法('nsmethod''exhaustive'),thencreatensalso supports these distance metrics.

Value Description
“相关” 一减去观测值之间的样本线性相关性(作为值序列处理)
'余弦' 一个减去观察之间的包含角度的余弦(作为行向量处理)
“哈明” Hamming distance, which is the percentage of coordinates that differ
'jaccard' 1减去Jaccard系数,它是不同非零坐标的百分比
'mahalanobis' 马氏距离
'seuclidean' 标准化的欧几里德距离
'spearman' One minus the sample Spearman's rank correlation between observations (treated as sequences of values)

如果createns使用详尽的搜索算法('nsmethod''exhaustive'),then you can also specify a function handle for a custom distance metric by using@(例如,@distfun).A custom distance function must:

  • 有表格吗function D2 = distfun(ZI,ZJ).

  • Take as arguments:

    • 一个1-by-KvectorZIcontaining a single row fromor from the query points是的,whereK是the number of columns in.

    • -通过-K矩阵ZJ包含多行的or是的,where是一个正整数。

  • 返回-1-1距离矢量D2号,whereD2号(j)是the distance between the observationsZIandZJ(j,:).

For more details, see距离指标.

例:'Distance','minkowski'

用于Minkowski距离度量的指数,指定为逗号分隔对组成“P”and a positive scalar. This argument is valid only if'Distance'“明可夫斯基”.

例:“P”,3

Data Types:single|double

For Exhaustive Nearest Neighbor Searchers

全部折叠

马氏距离度量的协方差矩阵,指定为逗号分隔对,由'Cov'and aK-通过-K正定矩阵,其中K是the number of columns in.This argument is valid only if'Distance''mahalanobis'.

例:'Cov',Eye(3)

Data Types:single|double

Scale parameter value for the standardized Euclidean distance metric, specified as the comma-separated pair consisting of'比例'和一个非负的长度向量K,whereK是the number of columns in.软件使用相应的元素缩放训练和查询数据之间的每个区别Scale.This argument is valid only if'Distance''seuclidean'.

例:“刻度”,分位数(X,0.75)-分位数(X,0.25)

Data Types:single|double

对于最近的邻居搜索者使用Kd-Tree

全部折叠

Maximum number of data points in each leaf node of theKd-树,指定为逗号分隔对,由'BucketSize'和一个正整数。

仅当您创建一个时才有效KDTreeSearcher模型对象.

例:“BucketSize”,10岁

Data Types:single|double

Output Arguments

全部折叠

Nearest neighbor searcher, returned as an令人疲惫的模型对象或KDTreeSearcher模型对象.

创建最近的邻居搜索器模型对象后,您可以通过执行最近的邻居搜索来在训练数据中找到训练数据中的相邻点KNN搜索or a radius search usingrangesearch.

引入R2010a