org.apache.ignite.ml.knn

## Class KNNTrainer<M extends KNNModel<Double>,Self extends KNNTrainer<M,Self>>

- java.lang.Object
- - org.apache.ignite.ml.trainers.DatasetTrainer<M,Double>
  - - org.apache.ignite.ml.trainers.SingleLabelDatasetTrainer<M>
    - - org.apache.ignite.ml.knn.KNNTrainer<M,Self>

- Type Parameters:
    `M` - Model type.
  Direct Known Subclasses:
    KNNClassificationTrainer, KNNRegressionTrainer

      public abstract class KNNTrainer<M extends KNNModel<Double>,Self extends KNNTrainer<M,Self>>
      extends SingleLabelDatasetTrainer<M>

  KNN model trader that trains model on top of distributed spatial indices. Be aware that this model is linked with cluster environment it's been built on and can't be saved or used in other places. Under the hood it keeps `Dataset` that consists of a set of resources allocated across the cluster.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class org.apache.ignite.ml.trainers.DatasetTrainer

      `DatasetTrainer.EmptyDatasetException`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected DistanceMeasure` | `distanceMeasure` Distance measure.
    `protected int` | `k` Number of neighbours.
    `protected boolean` | `weighted` Weighted or not.

    - ### Fields inherited from class org.apache.ignite.ml.trainers.DatasetTrainer

      `envBuilder, environment`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `KNNTrainer()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected abstract M` | `convertDatasetIntoModel(Dataset<EmptyContext,SpatialIndex<Double>> dataset)` Convers given dataset into KNN model (classification or regression depends on implementation).
    `protected <K,V> M` | `fitWithInitializedDeployingContext(DatasetBuilder<K,V> datasetBuilder, Preprocessor<K,V> preprocessor)` Trains model based on the specified data.
    `boolean` | `isUpdateable(M mdl)`
    `protected abstract Self` | `self()` Returns `this` instance.
    `protected <K,V> M` | `updateModel(M mdl, DatasetBuilder<K,V> datasetBuilder, Preprocessor<K,V> preprocessor)` Gets state of model in arguments, update in according to new data and return new model.
    `Self` | `withDataTtl(long dataTtl)` Sets up `dataTtl` parameter.
    `Self` | `withDistanceMeasure(DistanceMeasure distanceMeasure)` Sets up `distanceMeasure` parameter.
    `Self` | `withIdxType(SpatialIndexType idxType)` Sets up `idxType` parameter.
    `Self` | `withK(int k)` Sets up `k` parameter (number of neighbours).
    `Self` | `withWeighted(boolean weighted)` Sets up `weighted` parameter.

    - ### Methods inherited from class org.apache.ignite.ml.trainers.DatasetTrainer

      `fit, fit, fit, fit, fit, fit, getLastTrainedModelOrThrowEmptyDatasetException, identityTrainer, learningEnvironment, update, update, update, update, update, withConvertedLabels, withEnvironmentBuilder`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### distanceMeasure

          protected DistanceMeasure distanceMeasure

      Distance measure.

    - #### k

          protected int k

      Number of neighbours.

    - #### weighted

          protected boolean weighted

      Weighted or not.

  - ### Constructor Detail

    - #### KNNTrainer

          public KNNTrainer()

  - ### Method Detail

    - #### fitWithInitializedDeployingContext

          protected <K,V> M fitWithInitializedDeployingContext(DatasetBuilder<K,V> datasetBuilder,
                                                               Preprocessor<K,V> preprocessor)

      Trains model based on the specified data.
      Specified by:
        `fitWithInitializedDeployingContext` in class `DatasetTrainer<M extends KNNModel<Double>,Double>`
      Type Parameters:
        `K` - Type of a key in `upstream` data.
        `V` - Type of a value in `upstream` data.
      Parameters:
        `datasetBuilder` - Dataset builder.
        `preprocessor` - Extractor of `UpstreamEntry` into `LabeledVector`.
      Returns:
        Model.

    - #### isUpdateable

          public boolean isUpdateable(M mdl)

      Specified by:
        `isUpdateable` in class `DatasetTrainer<M extends KNNModel<Double>,Double>`
      Parameters:
        `mdl` - Model.
      Returns:
        True if current critical for training parameters correspond to parameters from last training.

    - #### updateModel

          protected <K,V> M updateModel(M mdl,
                                        DatasetBuilder<K,V> datasetBuilder,
                                        Preprocessor<K,V> preprocessor)

      Gets state of model in arguments, update in according to new data and return new model.
      Specified by:
        `updateModel` in class `DatasetTrainer<M extends KNNModel<Double>,Double>`
      Type Parameters:
        `K` - Type of a key in `upstream` data.
        `V` - Type of a value in `upstream` data.
      Parameters:
        `mdl` - Learned model.
        `datasetBuilder` - Dataset builder.
        `preprocessor` - Extractor of `UpstreamEntry` into `LabeledVector`.
      Returns:
        Updated model.

    - #### convertDatasetIntoModel

          protected abstract M convertDatasetIntoModel(Dataset<EmptyContext,SpatialIndex<Double>> dataset)

      Convers given dataset into KNN model (classification or regression depends on implementation).
      Parameters:
        `dataset` - Dataset of spatial indices.
      Returns:
        KNN model.

    - #### self

          protected abstract Self self()

      Returns `this` instance.
      Returns:
        This instance.

    - #### withIdxType

          public Self withIdxType(SpatialIndexType idxType)

      Sets up `idxType` parameter.
      Parameters:
        `idxType` - Index type.

    - #### withDataTtl

          public Self withDataTtl(long dataTtl)

      Sets up `dataTtl` parameter.
      Parameters:
        `dataTtl` - Partition data time-to-live in seconds (-1 for an infinite lifetime).
      Returns:
        This instance.

    - #### withDistanceMeasure

          public Self withDistanceMeasure(DistanceMeasure distanceMeasure)

      Sets up `distanceMeasure` parameter.
      Parameters:
        `distanceMeasure` - Distance measure.

    - #### withK

          public Self withK(int k)

      Sets up `k` parameter (number of neighbours).
      Parameters:
        `k` - Number of neighbours.

    - #### withWeighted

          public Self withWeighted(boolean weighted)

      Sets up `weighted` parameter.
      Parameters:
        `weighted` - Weighted or not.

2022 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.12.0
Release Date | : January 10 2022