org.opencv.ml
public class CvKNearest extends CvStatModel
The class implements K-Nearest Neighbors model as described in the beginning of this section.
Note:
Constructor and Description |
---|
CvKNearest()
Default and training constructors.
|
CvKNearest(Mat trainData,
Mat responses)
Default and training constructors.
|
CvKNearest(Mat trainData,
Mat responses,
Mat sampleIdx,
boolean isRegression,
int max_k)
Default and training constructors.
|
Modifier and Type | Method and Description |
---|---|
float |
find_nearest(Mat samples,
int k,
Mat results,
Mat neighborResponses,
Mat dists)
Finds the neighbors and predicts responses for input vectors.
|
boolean |
train(Mat trainData,
Mat responses)
Trains the model.
|
boolean |
train(Mat trainData,
Mat responses,
Mat sampleIdx,
boolean isRegression,
int maxK,
boolean updateBase)
Trains the model.
|
load, load, save, save
public CvKNearest()
Default and training constructors.
See "CvKNearest.train" for additional parameters descriptions.
public CvKNearest(Mat trainData, Mat responses)
Default and training constructors.
See "CvKNearest.train" for additional parameters descriptions.
trainData
- a trainDataresponses
- a responsespublic CvKNearest(Mat trainData, Mat responses, Mat sampleIdx, boolean isRegression, int max_k)
Default and training constructors.
See "CvKNearest.train" for additional parameters descriptions.
trainData
- a trainDataresponses
- a responsessampleIdx
- a sampleIdxisRegression
- a isRegressionmax_k
- a max_kpublic float find_nearest(Mat samples, int k, Mat results, Mat neighborResponses, Mat dists)
Finds the neighbors and predicts responses for input vectors.
For each input vector (a row of the matrix samples
), the method
finds the k
nearest neighbors. In case of regression, the
predicted result is a mean value of the particular vector's neighbor
responses. In case of classification, the class is determined by voting.
For each input vector, the neighbors are sorted by their distances to the vector.
In case of C++ interface you can use output pointers to empty matrices and the function will allocate memory itself.
If only a single input vector is passed, all output matrices are optional and the predicted value is returned by the method.
The function is parallelized with the TBB library.
samples
- Input samples stored by rows. It is a single-precision
floating-point matrix of number_of_samples x number_of_features
size.k
- Number of used nearest neighbors. It must satisfy constraint: k
<= "CvKNearest.get_max_k".results
- Vector with results of prediction (regression or
classification) for each input sample. It is a single-precision
floating-point vector with number_of_samples
elements.neighborResponses
- Optional output values for corresponding
neighbors
. It is a single-precision floating-point matrix of
number_of_samples x k size.dists
- a distspublic boolean train(Mat trainData, Mat responses)
Trains the model.
The method trains the K-Nearest model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:
CV_ROW_SAMPLE
data layout is supported.
is_regression=false
)
or ordered (is_regression=true
).
var_idx
) and missing measurements are
not supported.
trainData
- a trainDataresponses
- a responsespublic boolean train(Mat trainData, Mat responses, Mat sampleIdx, boolean isRegression, int maxK, boolean updateBase)
Trains the model.
The method trains the K-Nearest model. It follows the conventions of the generic "CvStatModel.train" approach with the following limitations:
CV_ROW_SAMPLE
data layout is supported.
is_regression=false
)
or ordered (is_regression=true
).
var_idx
) and missing measurements are
not supported.
trainData
- a trainDataresponses
- a responsessampleIdx
- a sampleIdxisRegression
- Type of the problem: true
for regression and
false
for classification.maxK
- Number of maximum neighbors that may be passed to the method
"CvKNearest.find_nearest".updateBase
- Specifies whether the model is trained from scratch
(update_base=false
), or it is updated using the new training
data (update_base=true
). In the latter case, the parameter
maxK
must not be larger than the original value.