org.apache.ignite.ml.knn

## Class NNClassificationModel

- java.lang.Object
- - org.apache.ignite.ml.knn.NNClassificationModel

- All Implemented Interfaces:
    Serializable, AutoCloseable, DeployableObject, Exportable<KNNModelFormat>, IgniteModel<Vector,Double>, Model<Vector,Double>
  Direct Known Subclasses:
    ANNClassificationModel

      public abstract class NNClassificationModel
      extends Object
      implements IgniteModel<Vector,Double>, Exportable<KNNModelFormat>, DeployableObject

  Common methods and fields for all kNN and aNN models to predict label based on neighbours' labels.
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected DistanceMeasure` | `distanceMeasure` Distance measure.
    `protected int` | `k` Amount of nearest neighbors.
    `protected boolean` | `weighted` kNN strategy.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NNClassificationModel()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected LabeledVectorSet<LabeledVector>` | `buildLabeledDatasetOnListOfVectors(List<LabeledVector> neighborsFromPartitions)`
    `protected void` | `copyParametersFrom(NNClassificationModel mdl)` Sets parameters from other model to this model.
    `boolean` | `equals(Object obj)`
    `protected double` | `getClassVoteForVector(boolean weighted, double distance)`
    `protected double` | `getClassWithMaxVotes(Map<Double,Double> clsVotes)`
    `List<Object>` | `getDependencies()` Returns dependencies of this object that can be object with class defined by client side and unknown for server.
    `DistanceMeasure` | `getDistanceMeasure()`
    `protected @NotNull TreeMap<Double,Set<Integer>>` | `getDistances(Vector v, LabeledVectorSet<LabeledVector> trainingData)` Computes distances between given vector and each vector in training dataset.
    `int` | `getK()`
    `protected @NotNull LabeledVector[]` | `getKClosestVectors(LabeledVectorSet<LabeledVector> trainingData, TreeMap<Double,Set<Integer>> distanceIdxPairs)` Iterates along entries in distance map and fill the resulting k-element array.
    `int` | `hashCode()`
    `boolean` | `isWeighted()`
    `protected void` | `putDistanceIdxPair(Map<Double,Set<Integer>> distanceIdxPairs, int i, double distance)`
    `abstract <P> void` | `saveModel(Exporter<KNNModelFormat,P> exporter, P path)` Save model by the given path.
    `String` | `toString()`
    `String` | `toString(boolean pretty)`
    `NNClassificationModel` | `withDistanceMeasure(DistanceMeasure distanceMeasure)` Set up parameter of the NN model.
    `NNClassificationModel` | `withK(int k)` Set up parameter of the NN model.
    `NNClassificationModel` | `withWeighted(boolean weighted)` Sets up `weighted` parameter.

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

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

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

      `andBefore, andThen, andThen, close, combine`

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

      `predict`

- - ### Field Detail

    - #### k

          protected int k

      Amount of nearest neighbors.

    - #### distanceMeasure

          protected DistanceMeasure distanceMeasure

      Distance measure.

    - #### weighted

          protected boolean weighted

      kNN strategy.

  - ### Constructor Detail

    - #### NNClassificationModel

          public NNClassificationModel()

  - ### Method Detail

    - #### withK

          public NNClassificationModel withK(int k)

      Set up parameter of the NN model.
      Parameters:
        `k` - Amount of nearest neighbors.
      Returns:
        Model.

    - #### withWeighted

          public NNClassificationModel withWeighted(boolean weighted)

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

    - #### withDistanceMeasure

          public NNClassificationModel withDistanceMeasure(DistanceMeasure distanceMeasure)

      Set up parameter of the NN model.
      Parameters:
        `distanceMeasure` - Distance measure.
      Returns:
        Model.

    - #### buildLabeledDatasetOnListOfVectors

          protected LabeledVectorSet<LabeledVector> buildLabeledDatasetOnListOfVectors(List<LabeledVector> neighborsFromPartitions)

    - #### getKClosestVectors

          @NotNull
          protected @NotNull LabeledVector[] getKClosestVectors(LabeledVectorSet<LabeledVector> trainingData,
                                                                         TreeMap<Double,Set<Integer>> distanceIdxPairs)

      Iterates along entries in distance map and fill the resulting k-element array.
      Parameters:
        `trainingData` - The training data.
        `distanceIdxPairs` - The distance map.
      Returns:
        K-nearest neighbors.

    - #### getDistances

          @NotNull
          protected @NotNull TreeMap<Double,Set<Integer>> getDistances(Vector v,
                                                                                LabeledVectorSet<LabeledVector> trainingData)

      Computes distances between given vector and each vector in training dataset.
      Parameters:
        `v` - The given vector.
        `trainingData` - The training dataset.
      Returns:
        Key - distanceMeasure from given features before features with idx stored in value. Value is presented with Set because there can be a few vectors with the same distance.

    - #### putDistanceIdxPair

          protected void putDistanceIdxPair(Map<Double,Set<Integer>> distanceIdxPairs,
                                            int i,
                                            double distance)

    - #### getClassWithMaxVotes

          protected double getClassWithMaxVotes(Map<Double,Double> clsVotes)

    - #### getClassVoteForVector

          protected double getClassVoteForVector(boolean weighted,
                                                 double distance)

    - #### getDistanceMeasure

          public DistanceMeasure getDistanceMeasure()

    - #### getK

          public int getK()

    - #### isWeighted

          public boolean isWeighted()

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Object`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### toString

          public String toString(boolean pretty)

      Specified by:
        `toString` in interface `IgniteModel<Vector,Double>`
      Parameters:
        `pretty` - Use pretty mode.

    - #### copyParametersFrom

          protected void copyParametersFrom(NNClassificationModel mdl)

      Sets parameters from other model to this model.
      Parameters:
        `mdl` - Model.

    - #### saveModel

          public abstract <P> void saveModel(Exporter<KNNModelFormat,P> exporter,
                                             P path)

      Save model by the given path.
      Specified by:
        `saveModel` in interface `Exportable<KNNModelFormat>`
      Parameters:
        `exporter` - Exporter.
        `path` - Path to saved model.

    - #### getDependencies

          public List<Object> getDependencies()

      Returns dependencies of this object that can be object with class defined by client side and unknown for server.
      Specified by:
        `getDependencies` in interface `DeployableObject`

2022 Copyright © Apache Software Foundation
| Follow @ApacheIgnite
Ignite Database and Caching Platform | : ver. 2.13.0
Release Date | : April 20 2022