Main Content

ExhaustiveSearcher

Create exhaustive nearest neighbor searcher

Description

ExhaustiveSearcher模型对象存储训练数据,距离度量和距离度量的参数值,用于详尽的最近邻居搜索。详尽的搜索算法找到了从每个查询观察到的距离nobservations in the training data, which is ann-by-Knumeric matrix.

Once you create anExhaustiveSearcher模型对象,通过使用knnsearch或使用半径搜索rangesearch。这exhaustive search algorithm is more efficient than theKd-tree algorithm whenKis large (that is,K> 10), and it is more flexible than theKd-tree algorithm with respect to distance metric choices. TheExhaustiveSearcher模型对象还支持稀疏数据。万博1manbetx

Creation

Use either thecreatens功能或ExhaustiveSearcherfunction (described here) to create anExhaustiveSearcher目的。这两个函数都使用相同的语法,除了creatensfunction has the'NSMethod'名称值对参数,您用于选择最近的邻居搜索方法。这createns功能还创建一个Kdtreesearcher目的。Specify“ nsmethod',“详尽”to create anExhaustiveSearcher目的。这default is'exhaustive'ifK> 10, the training data is sparse, or the distance metric is not the Euclidean, city block, Chebychev, or Minkowski.

Description

example

MDL= EnteriveSearcher(X)creates an exhaustive nearest neighbor searcher object (MDL) using then-by-Knumeric matrix of training data (X).

example

MDL= EnteriveSearcher(X,姓名,Value)使用一个或多个名称值对参数指定其他选项。您可以指定距离度量并设置距离度量参数(DIST参数) property. For example,ExhaustiveSearcher(X,'Distance','chebychev')创建一个使用Chebychev距离的详尽的最近邻居搜索器对象。指定DIST参数, use theCov,P, 或者规模name-value pair argument.

Input Arguments

expand all

训练数据,该数据准备了详尽的搜索算法,该算法指定为数字矩阵。Xhasnrows, each corresponding to an observation (that is, an instance or example), andKcolumns, each corresponding to a predictor (that is, a feature).

Data Types:单身的|double

姓名-Value Arguments

将可选的参数对name1 = value1,...,namen = valuen, where姓名is the argument name and价值is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

在R2021a之前,请使用逗号分隔每个名称和值,并附上姓名in quotes.

Example:'距离','mahalanobis','Cov',eye(3)specifies to use the Mahalanobis distance when searching for nearest neighbors and a 3-by-3 identity matrix for the covariance matrix in the Mahalanobis distance metric.

Distance metric used when you callknnsearch或者rangesearchto find nearest neighbors for future query points, specified as the comma-separated pair consisting of'距离'和character vector, string scalar, or function handle.

This table describes the supported distance metrics specified as character vectors or string scalars.

价值 Description
'chebychev' Chebychev distance (maximum coordinate difference).
'城市街区' City block distance.
'correlation' One minus the sample linear correlation between observations (treated as sequences of values).
的余弦 One minus the cosine of the included angle between observations (treated as row vectors).
'euclidean' Euclidean distance.
'hamming' Hamming distance, which is the percentage of coordinates that differ.
'jaccard' 一个减去jaccard系数,这是不同的非零坐标百分比。
'minkowski' Minkowski distance. The default exponent is 2. To specify a different exponent, use the'P'name-value pair argument.
'mahalanobis' Mahalanobis distance, computed using a positive definite covariance matrix. To change the value of the covariance matrix, use the'Cov'name-value pair argument.
'seuclidean' Standardized Euclidean distance. Each coordinate difference between rows inmdl.x查询矩阵通过除以从mdl.x。指定another scaling, use the'规模'name-value pair argument.
'spearman' One minus the sample Spearman's rank correlation between observations (treated as sequences of values).

For more details, see距离度量s

您可以使用使用@(for example,@distfun). A custom distance function must:

  • Have the form函数d2 = distFun(zi,zj)

  • 作为参数:

    • 1乘K向量ZI包含一行X或从查询点Y, whereK是列中的数量X

    • Anm-by-KmatrixZJcontaining multiple rows ofX或者Y, wheremis a positive integer.

  • Return anm-by-1 vector of distanceD2, whereD2(j)是观测之间的距离ZIZJ(j,:)

这software does not use the distance metric for creating anExhaustiveSearchermodel object, so you can alter the distance metric by using dot notation after creating the object.

Example:'距离','mahalanobis'

Covariance matrix for the Mahalanobis distance metric, specified as the comma-separated pair consisting of'Cov'K-by-Kpositive definite matrix, whereK是列中的数量X。该参数仅在'距离'is'mahalanobis'

Example:'Cov',eye(3)

Data Types:单身的|double

Exponent for the Minkowski distance metric, specified as the comma-separated pair consisting of'P'和positive scalar. This argument is valid only if'距离'is'minkowski'

Example:'P',3

Data Types:单身的|double

标准化欧几里得距离度量的比例参数值,指定为逗号分隔对'规模'和非负数字矢量的lengthK, whereK是列中的数量X。这software scales each difference between the training and query data using the corresponding element of规模。该参数仅在'距离'is'seuclidean'

Example:“规模”,分位数(X,0.75) - quantile(X,0.25)

Data Types:单身的|double

Properties

expand all

This property is read-only.

训练数据,该数据准备了详尽的搜索算法,该算法指定为数字矩阵。Xhasnrows, each corresponding to an observation (that is, an instance or example), andKcolumns, each corresponding to a predictor (that is, a feature).

输入参数Xcreatens或者ExhaustiveSearcher设置此属性。

Data Types:单身的|double

Distance metric used when you callknnsearch或者rangesearch要查找未来查询点最近的邻居,请指定为字符向量或字符串标量('chebychev','城市街区','correlation',的余弦,'euclidean','hamming','jaccard','minkowski','mahalanobis','seuclidean', 或者'spearman'), or a function handle.

'距离'名称值对参数createns或者ExhaustiveSearcher设置此属性。

这software does not use the distance metric for creating anExhaustiveSearchermodel object, so you can alter it by using dot notation.

Distance metric parameter values, specified as empty ([]) or a positive scalar.

This table describes the distance parameters of the supported distance metrics.

距离度量 Parameter Description
'mahalanobis'

代表用于计算Mahalanobis距离的协方差矩阵的正定矩阵。默认情况下,软件使用cov(Mdl.X,'omitrows')

'Cov'名称值对参数createns或者ExhaustiveSearcher设置此属性。

您可以更改DIST参数by using dot notation, for example,MDL。DIST参数= CovNew, whereCovNewis aK-by-K正定数字矩阵。

'minkowski'

指示Minkowski距离指数的正标量。默认情况下,指数为2

'P'名称值对参数createns或者ExhaustiveSearcher设置此属性。

您可以更改DIST参数by using dot notation, for example,mdl.distparameter = pnew, wherePNewis a positive scalar.

'seuclidean'

A positive numeric vector indicating the values used by the software to scale the predictors when computing the standardized Euclidean distance. By default, the software:

  1. Estimates the standard deviation of each predictor (column) ofX使用scale = std(mdl.x,'omitnan')

  2. 规模s each coordinate difference between the rows inX以及通过除以相应元素的查询矩阵scale

'规模'名称值对参数createns或者ExhaustiveSearcher设置此属性。

您可以更改DIST参数by using dot notation, for example,MDL。DIST参数= sNew, wheresNewis aK-dimensional positive numeric vector.

Ifmdl.distance不是此表中列出的参数之一,然后MDL。DIST参数is[], which means that the specified distance metric formula has no parameters.

Data Types:单身的|double

Object Functions

knnsearch 寻找k- 使用搜索器对象的近邻居
rangesearch 寻找all neighbors within specified distance using searcher object

Examples

collapse all

Load Fisher's iris data set.

loadfisheririsX = meas; [n,k] = size(X)
n = 150
k = 4

X有150个观察结果和4个预测因子。

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

MDL1 = ExhaustiveSearcher(X)
MDL1 = ExhaustiveSearcher with properties: Distance: 'euclidean' DistParameter: [] X: [150x4 double]

MDL1is anExhaustiveSearchermodel object, and its properties appear in the Command Window. The object contains information about the trained algorithm, such as the distance metric. You can alter property values using dot notation.

另外,您可以使用createns和specifying'exhaustive'as the search method.

MDL2 = createns(X,'NSMethod','exhaustive')
MDL2 = EnterviveRearcher具有属性:距离:'Euclidean'Distparameter:[] X:[150x4 double]

MDL2is also anExhaustiveSearchermodel object, and it is equivalent toMDL1

To searchXfor the nearest neighbors to a batch of query data, pass theExhaustiveSearchermodel object and the query data toknnsearch或者rangesearch

Load Fisher's iris data set. Focus on the petal dimensions.

loadfisheririsx = meas(:,[3 4]);% Predictors

Prepare an exhaustive nearest neighbor searcher. Specify the Mahalanobis distance metric.

mdl = createns(x,'距离','mahalanobis')
MDL = EnteriveSearcher具有属性:距离:'Mahalanobis'Distparameter:[2x2 double] X:[150x2 double]

Because the distance metric is Mahalanobis,createnscreates anExhaustiveSearcher默认情况下模型对象。

Access properties ofMDLby using dot notation. For example, useMDL。DIST参数to access the Mahalanobis covariance parameter.

MDL。DIST参数
ans =2×23.1163 1.2956 1.2956 0.5810

你可以通过query data andMDLto:

  • knnsearch查找最近邻居的索引和距离

  • rangesearch在您指定的距离内找到所有最近邻居的索引

Create anExhaustiveSearchermodel object and alter theDistanceproperty by using dot notation.

Load Fisher's iris data set.

loadfisheririsX = meas;

使用整个数据集作为培训数据训练默认的详尽搜索器算法。

MDL= EnteriveSearcher(X)
MDL= ExhaustiveSearcher with properties: Distance: 'euclidean' DistParameter: [] X: [150x4 double]

指定Mahalan邻居搜索者使用obis metric to compute the distances between the training and query data.

mdl.distance='mahalanobis'
MDL= ExhaustiveSearcher with properties: Distance: 'mahalanobis' DistParameter: [4x4 double] X: [150x4 double]

你可以通过MDL和the query data to eitherknnsearch或者rangesearchto find the nearest neighbors to the points in the query data based on the Mahalanobis distance.

Create an exhaustive searcher object by using thecreatensfunction. Pass the object and query data to theknnsearchfunction to findk-nearest neighbors.

Load Fisher's iris data set.

loadfisheriris

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

rng('default');% For reproducibilityn = size(meas,1);% Sample sizeqidx = randsample(n,5);% Indices of query dataX = meas(~ismember(1:n,qIdx),:); Y = meas(qIdx,:);

使用培训数据准备详尽的最近邻居搜索者。指定Mahalanobis距离以找到最近的邻居。

mdl = createns(x,'距离','mahalanobis')
MDL =属性的EnterviveRearcher:距离:'Mahalanobis'Distparameter:[4x4 double] X:[145x4 double]

Because the distance metric is Mahalanobis,createnscreates anExhaustiveSearcher默认情况下模型对象。

该软件在训练数据中使用预测变量(列)的协方差矩阵来计算Mahalanobis距离。要显示此值,请使用MDL。DIST参数

MDL。DIST参数
ans =4×40.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

查找培训数据的索引(mdl.x) that are the two nearest neighbors of each point in the query data (Y).

idxnn = knnsearch(mdl,y,'K',2)
IdxNN =5×25 6 98 95 104 128 135 65 102 115

每一行IdxNNcorresponds 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 ofY(3,:)isx(128,:)

Extended Capabilities

Version History

Introduced in R2010a