Class cv::ml::KNearest#
The class implements K-Nearest Neighbors model. View details
#include <opencv2/ml.hpp>Collaboration diagram for cv::ml::KNearest:
Public Types#
Implementations of KNearest algorithm.
enum Types {
BRUTE_FORCE =1,
KDTREE =2
}Public Types inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
Public Member Functions#
Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
|
Computes error on the training or test dataset. |
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Returns the number of variables in training samples. |
|
|
Returns true if the model is classifier. |
|
|
Returns true if the model is trained. |
|
|
|
Predicts response(s) for the provided sample(s) |
|
Trains the statistical model. |
|
|
|
Trains the statistical model. |
Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Clears the algorithm state. |
|
|
Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read. |
|
|
Reads algorithm parameters from a file storage. |
|
|
||
|
|
|
|
Stores algorithm parameters in a file storage. |
|
|
Static Public Member Functions#
Static Public Member Functions inherited from cv::ml::StatModel
Return |
Name |
Description |
|---|---|---|
|
Create and train model with default parameters. |
Static Public Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
|
Loads algorithm from the file. |
|
|
Loads algorithm from a String. |
|
Reads algorithm from the file node. |
Additional Inherited Members#
Protected Member Functions inherited from cv::Algorithm
Return |
Name |
Description |
|---|---|---|
|
Detailed Description#
The class implements K-Nearest Neighbors model.
See also
ml_intro_knn
Member Enumeration Documentation#
enum Types
Implementations of KNearest algorithm.
|
|
Member Function Documentation#
findNearest()#
float cv::ml::KNearest::findNearest(
InputArray samples,
int k,
OutputArray results,
OutputArray neighborResponses = noArray(),
OutputArray dist = noArray() )
Python:
cv.ml.KNearest.findNearest(samples, k[, results[, neighborResponses[, dist]]]) -> retval, results, neighborResponses, dist
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.
Parameters
samples— Input samples stored by rows. It is a single-precision floating-point matrix of<number_of_samples> * ksize.k— Number of used nearest neighbors. Should be greater than 1.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> * ksize.dist— Optional output distances from the input vectors to the corresponding neighbors. It is a single-precision floating-point matrix of<number_of_samples> * ksize.
Here is the call graph for this function:
getAlgorithmType()#
int cv::ml::KNearest::getAlgorithmType()
Python:
cv.ml.KNearest.getAlgorithmType() -> retval
Algorithm type, one of KNearest::Types.
See also
getDefaultK()#
int cv::ml::KNearest::getDefaultK()
Python:
cv.ml.KNearest.getDefaultK() -> retval
Default number of neighbors to use in predict method.
See also
getEmax()#
int cv::ml::KNearest::getEmax()
Python:
cv.ml.KNearest.getEmax() -> retval
Parameter for KDTree implementation.
See also
getIsClassifier()#
bool cv::ml::KNearest::getIsClassifier()
Python:
cv.ml.KNearest.getIsClassifier() -> retval
Whether classification or regression model should be trained.
See also
setAlgorithmType()#
void cv::ml::KNearest::setAlgorithmType(int val)
Python:
cv.ml.KNearest.setAlgorithmType(val)
See also
setDefaultK()#
void cv::ml::KNearest::setDefaultK(int val)
Python:
cv.ml.KNearest.setDefaultK(val)
See also
setEmax()#
void cv::ml::KNearest::setEmax(int val)
Python:
cv.ml.KNearest.setEmax(val)
See also
setIsClassifier()#
void cv::ml::KNearest::setIsClassifier(bool val)
Python:
cv.ml.KNearest.setIsClassifier(val)
See also
create()#
static Ptr< KNearest > cv::ml::KNearest::create()
Python:
cv.ml.KNearest.create() -> retval
cv.ml.KNearest_create() -> retval
Creates the empty model.
The static method creates empty KNearest classifier. It should be then trained using StatModel::train method.
load()#
static Ptr< KNearest > cv::ml::KNearest::load(const String & filepath)
Python:
cv.ml.KNearest.load(filepath) -> retval
cv.ml.KNearest_load(filepath) -> retval
Loads and creates a serialized knearest from a file.
Use KNearest::save to serialize and store an KNearest to disk. Load the KNearest from this file again, by calling this function with the path to the file.
Parameters
filepath— path to serialized KNearest
Source file#
The documentation for this class was generated from the following file:
opencv2/ml.hpp