pub trait KNearestTraitConst: StatModelTraitConst {
// Required method
fn as_raw_KNearest(&self) -> *const c_void;
// Provided methods
fn get_default_k(&self) -> Result<i32> { ... }
fn get_is_classifier(&self) -> Result<bool> { ... }
fn get_emax(&self) -> Result<i32> { ... }
fn get_algorithm_type(&self) -> Result<i32> { ... }
fn find_nearest(
&self,
samples: &impl ToInputArray,
k: i32,
results: &mut impl ToOutputArray,
neighbor_responses: &mut impl ToOutputArray,
dist: &mut impl ToOutputArray,
) -> Result<f32> { ... }
fn find_nearest_def(
&self,
samples: &impl ToInputArray,
k: i32,
results: &mut impl ToOutputArray,
) -> Result<f32> { ... }
}
Expand description
Constant methods for crate::ml::KNearest
Required Methods§
fn as_raw_KNearest(&self) -> *const c_void
Provided Methods§
Sourcefn get_default_k(&self) -> Result<i32>
fn get_default_k(&self) -> Result<i32>
Sourcefn get_is_classifier(&self) -> Result<bool>
fn get_is_classifier(&self) -> Result<bool>
Sourcefn get_algorithm_type(&self) -> Result<i32>
fn get_algorithm_type(&self) -> Result<i32>
Sourcefn find_nearest(
&self,
samples: &impl ToInputArray,
k: i32,
results: &mut impl ToOutputArray,
neighbor_responses: &mut impl ToOutputArray,
dist: &mut impl ToOutputArray,
) -> Result<f32>
fn find_nearest( &self, samples: &impl ToInputArray, k: i32, results: &mut impl ToOutputArray, neighbor_responses: &mut impl ToOutputArray, dist: &mut impl ToOutputArray, ) -> Result<f32>
Finds the neighbors and predicts responses for input vectors.
§Parameters
- samples: Input samples stored by rows. It is a single-precision floating-point matrix of
<number_of_samples> * k
size. - 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> * k
size. - dist: Optional output distances from the input vectors to the corresponding neighbors. It
is a single-precision floating-point matrix of
<number_of_samples> * k
size.
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.
§C++ default parameters
- neighbor_responses: noArray()
- dist: noArray()
Sourcefn find_nearest_def(
&self,
samples: &impl ToInputArray,
k: i32,
results: &mut impl ToOutputArray,
) -> Result<f32>
fn find_nearest_def( &self, samples: &impl ToInputArray, k: i32, results: &mut impl ToOutputArray, ) -> Result<f32>
Finds the neighbors and predicts responses for input vectors.
§Parameters
- samples: Input samples stored by rows. It is a single-precision floating-point matrix of
<number_of_samples> * k
size. - 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> * k
size. - dist: Optional output distances from the input vectors to the corresponding neighbors. It
is a single-precision floating-point matrix of
<number_of_samples> * k
size.
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.
§Note
This alternative version of KNearestTraitConst::find_nearest function uses the following default values for its arguments:
- neighbor_responses: noArray()
- dist: noArray()
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.