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
功能或ExhaustiveSearcher
function (described here) to create anExhaustiveSearcher
目的。这两个函数都使用相同的语法,除了createns
function 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
creates an exhaustive nearest neighbor searcher object (MDL
= EnteriveSearcher(X
)MDL
) using then-by-Knumeric matrix of training data (X
).
Input Arguments
X
—Training data
numeric matrix
训练数据,该数据准备了详尽的搜索算法,该算法指定为数字矩阵。X
hasnrows, each corresponding to an observation (that is, an instance or example), andKcolumns, each corresponding to a predictor (that is, a feature).
Data Types:单身的
|double
将可选的参数对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
—Distance metric
'euclidean'
(default) |character vector|字符串标量|custom distance function
Distance metric used when you callknnsearch
或者rangesearch
to 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-Kmatrix
ZJ
containing multiple rows ofX
或者Y
, wheremis a positive integer.
Return anm-by-1 vector of distance
D2
, whereD2(
是观测之间的距离j
)ZI
和ZJ(
。j
,:)
这software does not use the distance metric for creating anExhaustiveSearcher
model object, so you can alter the distance metric by using dot notation after creating the object.
Example:'距离','mahalanobis'
Cov
—Covariance matrix for Mahalanobis distance metric
cov(X,'omitrows')
(default) |positive definite matrix
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
P
—Exponent for Minkowski distance metric
2
(default) |positive scalar
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
规模
—标准化欧几里得距离度量的比例参数值
性病(X, omitnan)
(default) |非负数字矢量
标准化欧几里得距离度量的比例参数值,指定为逗号分隔对'规模'
和非负数字矢量的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
X
—Training data
numeric matrix
This property is read-only.
训练数据,该数据准备了详尽的搜索算法,该算法指定为数字矩阵。X
hasnrows, each corresponding to an observation (that is, an instance or example), andKcolumns, each corresponding to a predictor (that is, a feature).
输入参数X
的createns
或者ExhaustiveSearcher
设置此属性。
Data Types:单身的
|double
Distance
—Distance metric
character vector|字符串标量|custom distance function
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 anExhaustiveSearcher
model object, so you can alter it by using dot notation.
DIST参数
—Distance metric parameter values
[]
|positive scalar
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距离的协方差矩阵的正定矩阵。默认情况下,软件使用 这 您可以更改 |
'minkowski' |
指示Minkowski距离指数的正标量。默认情况下,指数为 这 您可以更改 |
'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:
这 您可以更改 |
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
Train Default Exhaustive Nearest Neighbor Searcher
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]
MDL1
is anExhaustiveSearcher
model 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]
MDL2
is also anExhaustiveSearcher
model object, and it is equivalent toMDL1
。
To searchX
for the nearest neighbors to a batch of query data, pass theExhaustiveSearcher
model object and the query data toknnsearch
或者rangesearch
。
Specify the Mahalanobis Distance for Nearest Neighbor Search
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,createns
creates anExhaustiveSearcher
默认情况下模型对象。
Access properties ofMDL
by 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 andMDL
to:
knnsearch
查找最近邻居的索引和距离rangesearch
在您指定的距离内找到所有最近邻居的索引
Alter属性的属性ExhaustiveSearcher
Model
Create anExhaustiveSearcher
model object and alter theDistance
property 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
或者rangesearch
to find the nearest neighbors to the points in the query data based on the Mahalanobis distance.
Search for Nearest Neighbors of Query Data Using Mahalanobis Distance
Create an exhaustive searcher object by using thecreatens
function. Pass the object and query data to theknnsearch
function 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,createns
creates 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
每一行IdxNN
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 ofY(3,:)
isx(128,:)
。
Extended Capabilities
C/C++ Code Generation
使用MATLAB®CODER™生成C和C ++代码。
Usage notes and limitations:
这
knnsearch
和rangesearch
功能支持代码生成。万博1manbetx当你训练
ExhaustiveSearcher
model object, the value of the'距离'
name-value pair argument cannot be a custom distance function.
For more information, seeIntroduction to Code Generation和Code Generation for Nearest Neighbor Searcher。
Version History
MATLAB 명령
다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.
명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.
Select a Web Site
选择一个网站以获取可用的翻译内容,并查看本地事件和优惠。根据您的位置,我们建议您选择:。
您还可以从以下列表中选择一个网站:
如何获得最佳网站性能
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
欧洲
- Netherlands(English)
- 挪威(English)
- Österreich(德意志)
- Portugal(English)
- 瑞典(English)
- Switzerland
- United Kingdom(English)