创建最近邻居搜索器对象
装载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,createns
creates 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,createns
creates 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距离度量的模型对象。
'nsmethod'
—最近邻搜索法“kdtree”
|'exhaustive'
最近邻搜索法used to define the type of object created, specified as the comma-separated pair consisting of'nsmethod'
and“kdtree”
or'exhaustive'
.
“kdtree”
—createns
creates aKDTreeSearcher
使用Kd-tree algorithm.
'exhaustive'
—createns
创建令人疲惫的
使用exhaustive search algorithm.
The default value is“kdtree”
当这三个条件成立时:
否则,默认值为'exhaustive'
.
例:'nsmethod','exhaustive'
'Distance'
—距离公制“欧几里得”
(default) |距离度量名称的字符向量或字符串标量|自定义距离功能呼叫时使用的距离度量KNN搜索
orrangesearch
找到未来查询点的最近邻居,指定为逗号分隔的对组成'Distance'
and a character vector or string scalar of distance metric name or function handle.
对于两种类型的最近邻景人,createns
supports these distance metrics.
Value | Description |
---|---|
'切比切夫' |
切比雪夫距离(最大坐标差)。 |
'cityblock' |
City block distance. |
“欧几里得” |
欧几里得距离。 |
“明可夫斯基” |
Minkowski距离。默认指数为2.要指定其他指数,请使用“P” 名称-值对参数。 |
如果createns
使用详尽的搜索算法('nsmethod'
是'exhaustive'
),thencreatens
also 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-KvectorZI
containing a single row from十
or from the query points是的
,whereK是the number of columns in十
.
安米-通过-K矩阵ZJ
包含多行的十
or是的
,where米是一个正整数。
返回米-1-1距离矢量D2号
,whereD2号(
是the distance between the observationsj
)ZI
andZJ(
.j
,:)
For more details, see距离指标.
例:'Distance','minkowski'
“P”
—Minkowski距离度量的指数2
(default) |正标量用于Minkowski距离度量的指数,指定为逗号分隔对组成“P”
and a positive scalar. This argument is valid only if'Distance'
是“明可夫斯基”
.
例:“P”,3
Data Types:single
|double
'Cov'
—Covariance matrix for Mahalanobis distance metricCOV(x,'omitrows')
(default) |positive definite matrix马氏距离度量的协方差矩阵,指定为逗号分隔对,由'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
'比例'
—标准欧氏距离度量的尺度参数值标准(X,'omitnan')
(default) |nonnegative numeric vectorScale 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
'BucketSize'
—Maximum number of data points in each leaf node50
(default) |positive integerMaximum number of data points in each leaf node of theKd-树,指定为逗号分隔对,由'BucketSize'
和一个正整数。
仅当您创建一个时才有效KDTreeSearcher
模型对象.
例:“BucketSize”,10岁
Data Types:single
|double
NS
-最近邻搜索器令人疲惫的
模型对象|KDTreeSearcher
模型对象Nearest neighbor searcher, returned as an令人疲惫的
模型对象或KDTreeSearcher
模型对象.
创建最近的邻居搜索器模型对象后,您可以通过执行最近的邻居搜索来在训练数据中找到训练数据中的相邻点KNN搜索
or a radius search usingrangesearch
.
您单击了与此MATLAB命令相对应的链接:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
Select网站您也可以从以下列表中选择网站:
选择中国网站(中文或英文)以获得最佳的网站表现。其他MathWorks国家网站没有针对您所在地的访问进行优化。