Class KNNClassifier<O>

  • Type Parameters:
    O - the type of objects handled by this algorithm
    All Implemented Interfaces:
    Algorithm, Classifier<O>

    @Title("kNN-classifier")
    @Description("Lazy classifier classifies a given instance to the majority class of the k-nearest neighbors.")
    @Priority(100)
    public class KNNClassifier<O>
    extends java.lang.Object
    implements Classifier<O>
    KNNClassifier classifies instances based on the class distribution among the k nearest neighbors in a database.
    Since:
    0.7.0
    Author:
    Arthur Zimek
    • Field Detail

      • k

        protected int k
        Holds the value of @link #K_PARAM}.
      • distance

        protected Distance<? super O> distance
        Distance function
    • Constructor Detail

      • KNNClassifier

        public KNNClassifier​(Distance<? super O> distance,
                             int k)
        Constructor.
        Parameters:
        distance - Distance function
        k - Number of nearest neighbors to access.
    • Method Detail

      • getInputTypeRestriction

        public TypeInformation[] getInputTypeRestriction()
        Description copied from interface: Algorithm
        Get the input type restriction used for negotiating the data query.
        Specified by:
        getInputTypeRestriction in interface Algorithm
        Returns:
        Type restriction
      • buildClassifier

        public void buildClassifier​(Database database,
                                    Relation<? extends ClassLabel> labels)
        Description copied from interface: Classifier
        Performs the training. Sets available labels.
        Specified by:
        buildClassifier in interface Classifier<O>
        Parameters:
        database - the database to build the model on
        labels - the classes to be learned
      • classify

        public ClassLabel classify​(O instance)
        Description copied from interface: Classifier
        Classify a single instance.
        Specified by:
        classify in interface Classifier<O>
        Parameters:
        instance - an instance to classify
        Returns:
        predicted class label of the given instance
      • classProbabilities

        public double[] classProbabilities​(O instance,
                                           java.util.ArrayList<ClassLabel> labels)
      • model

        public java.lang.String model()
        Description copied from interface: Classifier
        Produce a String representation of the classification model.
        Specified by:
        model in interface Classifier<O>
        Returns:
        a String representation of the classification model
      • getDistance

        public Distance<? super O> getDistance()
        Returns the distance.
        Returns:
        the distance