Class: ClassificationKNN
knearest neighbor classifier template for ensemble (to be removed)
ClassificationKNN.template
will be removed
in a future release. Use templateKNN
instead.
t = ClassificationKNN.template()
t = ClassificationKNN.template(Name,Value)
returns
a learner template suitable to use in the t
= ClassificationKNN.template()fitensemble
function.
creates
a template with additional options specified by one or more t
= ClassificationKNN.template(Name,Value
)Name,Value
pair
arguments.
Specify optional commaseparated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.
'BreakTies'
— Tiebreaking algorithm'smallest'
(default)  'nearest'
 'random'
Tiebreaking algorithm used by the predict
method
if multiple classes have the same smallest cost, specified as the
commaseparated pair consisting of 'BreakTies'
and
one of the following:
'smallest'
— Use the smallest
index among tied groups.
'nearest'
— Use the class
with the nearest neighbor among tied groups.
'random'
— Use a random
tiebreaker among tied groups.
By default, ties occur when multiple classes have the same number
of nearest points among the K
nearest neighbors.
Example: 'BreakTies','nearest'
'BucketSize'
— Maximum data points in node50
(default)  positive integer valueMaximum number of data points in the leaf node of the kdtree,
specified as the commaseparated pair consisting of 'BucketSize'
and
a positive integer value. This argument is meaningful only when NSMethod
is 'kdtree'
.
Example: 'BucketSize',40
Data Types: single
 double
'Cov'
— Covariance matrixnancov(X)
(default)  positive definite matrix of scalar valuesCovariance matrix, specified as the commaseparated pair consisting
of 'Cov'
and a positive definite matrix of scalar
values representing the covariance matrix when computing the Mahalanobis
distance. This argument is only valid when 'Distance'
is 'mahalanobis'
.
You cannot simultaneously specify 'Standardize'
and
either of 'Scale'
or 'Cov'
.
Data Types: single
 double
'Distance'
— Distance metricvalid distance metric string  function handleDistance metric, specified as the commaseparated pair consisting
of 'Distance'
and a valid distance metric string
or function handle. The allowable strings depend on the NSMethod
parameter,
which you set in fitcknn
, and which exists as
a field in ModelParameters
. If you specify CategoricalPredictors
as 'all'
,
then the default distance metric is 'hamming'
.
Otherwise, the default distance metric is 'euclidean'
.
NSMethod  Distance Metric Names 

exhaustive  Any distance metric of ExhaustiveSearcher 
kdtree  'citYblock' , 'chebychev' , 'euclidean' ,
or 'minkowski' 
For definitions, see Distance Metrics.
This table includes valid distance metrics of ExhaustiveSearcher
.
Value  Description 

'cityblock'  City block distance. 
'chebychev'  Chebychev distance (maximum coordinate difference). 
'correlation'  One minus the sample linear correlation between observations (treated as sequences of values). 
'cosine'  One minus the cosine of the included angle between observations (treated as vectors). 
'euclidean'  Euclidean distance. 
'hamming'  Hamming distance, percentage of coordinates that differ. 
'jaccard'  One minus the Jaccard coefficient, the percentage of nonzero coordinates that differ. 
'mahalanobis'  Mahalanobis distance, computed using a positive definite covariance
matrix C . The default value of C is
the sample covariance matrix of X , as computed
by nancov(X) . To specify a different value for C ,
use the 'Cov' namevalue pair argument. 
'minkowski'  Minkowski distance. The default exponent is 2 .
To specify a different exponent, use the 'Exponent' namevalue
pair argument. 
'seuclidean'  Standardized Euclidean distance. Each coordinate difference
between X and a query point is scaled, meaning
divided by a scale value S . The default value of S is
the standard deviation computed from X , S = nanstd(X) . To specify another
value for S , use the Scale namevalue
pair argument. 
'spearman'  One minus the sample Spearman's rank correlation between observations (treated as sequences of values). 
@  Distance function handle. distfun has
the formfunction D2 = DISTFUN(ZI,ZJ) % calculation of distance ...

Example: 'Distance','minkowski'
Data Types: function_handle
'DistanceWeight'
— Distance weighting function'equal'
(default)  'inverse'
 'squaredinverse'
 function handleDistance weighting function, specified as the commaseparated
pair consisting of 'DistanceWeight'
and either
a function handle or one of the following strings specifying the distance
weighting function.
DistanceWeight  Meaning 

'equal'  No weighting 
'inverse'  Weight is 1/distance 
'squaredinverse'  Weight is 1/distance^{2} 
@  fcn is a function that accepts a
matrix of nonnegative distances, and returns a matrix the same size
containing nonnegative distance weights. For example, 'squaredinverse' is
equivalent to @(d)d.^(2) . 
Example: 'DistanceWeight','inverse'
Data Types: function_handle
'Exponent'
— Minkowski distance exponent2
(default)  positive scalar valueMinkowski distance exponent, specified as the commaseparated
pair consisting of 'Exponent'
and a positive scalar
value. This argument is only valid when 'Distance'
is 'minkowski'
.
Example: 'Exponent',3
Data Types: single
 double
'IncludeTies'
— Tie inclusion flagfalse
(default)  true
Tie inclusion flag, specified as the commaseparated pair consisting
of 'IncludeTies'
and a logical value indicating
whether predict
includes all the neighbors whose
distance values are equal to the K
th smallest distance.
If IncludeTies
is true
, predict
includes
all these neighbors. Otherwise, predict
uses exactly K
neighbors.
Example: 'IncludeTies',true
Data Types: logical
'NSMethod'
— Nearest neighbor search method'kdtree'
 'exhaustive'
Nearest neighbor search method, specified as the commaseparated
pair consisting of 'NSMethod'
and 'kdtree'
or 'exhaustive'
.
'kdtree'
— Create and use
a kdtree to find nearest neighbors. 'kdtree'
is
valid when the distance metric is one of the following:
'euclidean'
'cityblock'
'minkowski'
'chebychev'
'exhaustive'
— Use the exhaustive
search algorithm. The distance values from all points in X
to
each point in Y
are computed to find nearest
neighbors.
The default is 'kdtree'
when X
has 10
or
fewer columns, X
is not sparse, and the distance
metric is a 'kdtree'
type; otherwise, 'exhaustive'
.
Example: 'NSMethod','exhaustive'
'NumNeighbors'
— Number of nearest neighbors to find1
(default)  positive integer valueNumber of nearest neighbors in X
to find
for classifying each point when predicting, specified as the commaseparated
pair consisting of 'NumNeighbors'
and a positive
integer value.
Example: 'NumNeighbors',3
Data Types: single
 double
'Scale'
— Distance scalenanstd(X)
(default)  vector of nonnegative scalar valuesDistance scale, specified as the commaseparated pair consisting
of 'Scale'
and a vector containing nonnegative
scalar values with length equal to the number of columns in X
.
Each coordinate difference between X
and a query
point is scaled by the corresponding element of Scale
.
This argument is only valid when 'Distance'
is 'seuclidean'
.
You cannot simultaneously specify 'Standardize'
and
either of 'Scale'
or 'Cov'
.
Data Types: single
 double
t
— Classification templateclassification template objectKnearest neighbor classification template
suitable to use in the fitensemble
function.
In an ensemble, t
specifies how to create the KNN
classifier.
Create a nondefault knearest
neighbor template for use in fitensemble
.
Create a template for 5nearest neighbor search.
t = ClassificationKNN.template('NumNeighbors',5)
t = Fit template for classification KNN. NumNeighbors: 5 NSMethod: '' Distance: '' BucketSize: [] IncludeTies: '' DistanceWeight: [] BreakTies: [] Exponent: [] Cov: [] Scale: [] Method: 'KNN' Type: 'classification'
You can use t
for ensemble learning.
You can also select a location from the following list: