org.apache.ignite.ml.knn

## Class KNNModel<L>

- java.lang.Object
- - org.apache.ignite.ml.knn.KNNModel<L>

- Type Parameters:
    `L` - Label type.
  All Implemented Interfaces:
    Serializable, AutoCloseable, IgniteModel<Vector,L>, Model<Vector,L>, SpatialIndex<L>
  Direct Known Subclasses:
    KNNClassificationModel, KNNRegressionModel

      public abstract class KNNModel<L>
      extends Object
      implements IgniteModel<Vector,L>, SpatialIndex<L>

  KNN model build 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.
  See Also:
    Serialized Form

- - ### 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.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `KNNModel(Dataset<EmptyContext,SpatialIndex<L>> dataset, DistanceMeasure distanceMeasure, int k, boolean weighted)` Constructs a new instance of KNN model.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `List<LabeledVector<L>>` | `findKClosest(int k, Vector pnt)` Finds `k` closest elements to the specified point.

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

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

    - ### Methods inherited from interface org.apache.ignite.ml.IgniteModel

      `andBefore, andThen, andThen, combine, toString`

    - ### Methods inherited from interface org.apache.ignite.ml.inference.Model

      `predict`

- - ### Field Detail

    - #### distanceMeasure

          protected final DistanceMeasure distanceMeasure

      Distance measure.

    - #### k

          protected final int k

      Number of neighbours.

    - #### weighted

          protected final boolean weighted

      Weighted or not.

  - ### Constructor Detail

    - #### KNNModel

          protected KNNModel(Dataset<EmptyContext,SpatialIndex<L>> dataset,
                             DistanceMeasure distanceMeasure,
                             int k,
                             boolean weighted)

      Constructs a new instance of KNN model.
      Parameters:
        `dataset` - Dataset with `SpatialIndex` as a partition data.
        `distanceMeasure` - Distance measure.
        `k` - Number of neighbours.
        `weighted` - Weighted or not.

  - ### Method Detail

    - #### findKClosest

          public List<LabeledVector<L>> findKClosest(int k,
                                                     Vector pnt)

      Finds `k` closest elements to the specified point.
      Specified by:
        `findKClosest` in interface `SpatialIndex<L>`
      Parameters:
        `k` - Number of elements to be returned.
        `pnt` - Point to be used to calculate distance to other points.
      Returns:
        An array of the `k` closest elements to the specified point.

    - #### close

          public void close()

      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `IgniteModel<Vector,L>`
      Specified by:
        `close` in interface `Model<Vector,L>`
      Specified by:
        `close` in interface `SpatialIndex<L>`

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