Packages

class KNN_Classifier extends ClassifierReal

The KNN_Classifier class is used to classify a new vector 'z' into one of 'k' classes. It works by finding its 'knn' nearest neighbors. These neighbors essentially vote according to their classification. The class with most votes is selected as the classification of 'z'. Using a distance metric, the 'knn' vectors nearest to 'z' are found in the training data, which is stored row-wise in the data matrix 'x'. The corresponding classifications are given in the vector 'y', such that the classification for vector 'x(i)' is given by 'y(i)'.

Linear Supertypes
ClassifierReal, Error, Classifier, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. KNN_Classifier
  2. ClassifierReal
  3. Error
  4. Classifier
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new KNN_Classifier(x: MatriD, y: VectoI, fn: Array[String], k: Int, cn: Array[String], knn: Int = 3)

    x

    the vectors/points of classified data stored as rows of a matrix

    y

    the classification of each vector in x

    fn

    the names for all features/variables

    k

    the number of classes

    cn

    the names for all classes

    knn

    the number of nearest neighbors to consider

Value Members

  1. def calcCorrelation: MatriD

    Calculate the correlation matrix for the feature vectors 'fea'.

    Calculate the correlation matrix for the feature vectors 'fea'. If the correlations are too high, the independence assumption may be dubious.

    Definition Classes
    ClassifierReal
  2. def classify(z: VectoD): (Int, String, Double)

    Given a new point/vector 'z', determine which class it belongs to (i.e., the class getting the most votes from its 'knn' nearest neighbors.

    Given a new point/vector 'z', determine which class it belongs to (i.e., the class getting the most votes from its 'knn' nearest neighbors. Return the best class, its name and its votes

    z

    the vector to classify

    Definition Classes
    KNN_ClassifierClassifier
  3. def classify(z: VectoI): (Int, String, Double)

    Given a new discrete (integer-valued) data vector 'z', determine which class it belongs to, by first converting it to a vector of doubles.

    Given a new discrete (integer-valued) data vector 'z', determine which class it belongs to, by first converting it to a vector of doubles. Return the best class, its name and its relative probability

    z

    the vector to classify

    Definition Classes
    ClassifierRealClassifier
  4. def crossValidate(nx: Int = 10): Double

    Test the accuracy of the classified results by cross-validation, returning the accuracy.

    Test the accuracy of the classified results by cross-validation, returning the accuracy. The "test data" starts at 'testStart' and ends at 'testEnd', the rest of the data is "training data'.

    nx

    the number of crosses and cross-validations (defaults to 5x).

    Definition Classes
    Classifier
  5. def crossValidateRand(nx: Int = 10): Double

    Test the accuracy of the classified results by cross-validation, returning the accuracy.

    Test the accuracy of the classified results by cross-validation, returning the accuracy. This version of cross-validation relies on "subtracting" frequencies from the previously stored global data to achieve efficiency.

    nx

    number of crosses and cross-validations (defaults to 10x).

    Definition Classes
    Classifier
  6. def distance(u: VectoD, v: VectoD): Double

    Compute a distance metric between vectors/points u and v.

    Compute a distance metric between vectors/points u and v.

    u

    the first vector/point

    v

    the second vector/point

  7. final def flaw(method: String, message: String): Unit
    Definition Classes
    Error
  8. def kNearest(z: VectoD): Unit

    Find the 'knn' nearest neighbors (top-'knn') to vector 'z' and store in the 'topK' array.

    Find the 'knn' nearest neighbors (top-'knn') to vector 'z' and store in the 'topK' array.

    z

    the vector to be classified

  9. def reset(): Unit

    Reset or re-initialize the frequency tables and the probability tables.

    Reset or re-initialize the frequency tables and the probability tables.

    Definition Classes
    KNN_ClassifierClassifier
  10. def size: Int

    Return the number of data vectors in training/test-set (# rows).

    Return the number of data vectors in training/test-set (# rows).

    Definition Classes
    ClassifierRealClassifier
  11. def test(xx: MatrixD, yy: VectorI): Double

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    xx

    the real-valued test vectors stored as rows of a matrix

    yy

    the test classification vector, where 'yy_i = class for row i of xx'

    Definition Classes
    ClassifierReal
  12. def test(testStart: Int, testEnd: Int): Double

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    testStart

    beginning of test region (inclusive)

    testEnd

    end of test region (exclusive)

    Definition Classes
    ClassifierRealClassifier
  13. def test(itest: VectorI): Double

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    Test the quality of the training with a test-set and return the fraction of correct classifications.

    itest

    the indices of the instances considered test data

    Definition Classes
    Classifier
  14. def train(testStart: Int, testEnd: Int): Unit

    Training involves resetting the data structures before each classification.

    Training involves resetting the data structures before each classification. It uses lazy training, so most of it is done during classification.

    testStart

    starting index of test region (inclusive) used in cross-validation.

    testEnd

    ending index of test region (exclusive) used in cross-validation.

    Definition Classes
    KNN_ClassifierClassifier
  15. def train(): Unit

    Given a set of data vectors and their classifications, build a classifier.

    Given a set of data vectors and their classifications, build a classifier.

    Definition Classes
    Classifier
  16. def train(itest: IndexedSeq[Int]): Unit

    Given a set of data vectors and their classifications, build a classifier.

    Given a set of data vectors and their classifications, build a classifier.

    itest

    the indices of the instances considered as testing data

    Definition Classes
    Classifier