Psychometrica - Linear Discriminant Analysis (LDA) in Java / Diskriminanzanalyse in Java

Java-Implementation of the Linear Discriminant Analysis

Linear discriminant analysis is a statistical approach to classify objects, persons or events based on their features in two or more seperate groups (see LDA on Wikipedia). Below you can download a java implementation of this method based on the tutorial of Kardi Teknomo. It is the first release (beta) and you need JAMA to run these routines. The code is licensed under Lesser General Public License (LGPL), which means, you can use it for free in commercial and non-commercial applications, as long as you preserve the copyright-notice within the source code. The code is documented, so you should find all necessary explanations there. Comments and extensions for the code are very wellcome. Feel free to write us.



Java-Implementierung der Diskriminanzanalyse

Die Diskriminanzanalyse ist ein statistisches Verfahren zur Klassifikation von Objekten, Personen oder Ereignissen auf der Basis ihrer Eigenschaften (vgl. Diskriminanzanalyse auf Wikipedia). Unten können Sie eine freie Implementierung dieser Methode laden. Die Implementierung basiert auf dem Tutorium von Kardi Teknomo. Es ist eine erste Veröffentlichung des Codes und Sie benötigen zum jetzigen Zeitpunkt JAMA um die Routinen ausführen zu können. Der Code ist unter der Lesser General Public License (LGPL) verfügbar, d. h. Sie könne ihn frei in kommerziellen und nicht-kommerziellen Anwendungen verwenden, sofern die Urheberschaftshinweise im Code erhalten bleiben. Kommentare, Hinweise und Ergänzungen zum Code sind herzlich willkommen.



Download

Here you can download the second release of LDA.java (Version 0.2, 07/16/2014). It features the basic approach to linear discriminant analysis. Planed features:

  • Estimate probability for group membership
  • Calculate weights for Fisher's linear discriminant functions
  • Remove JAMA dependency
  • Testing / Bugs / Performance...

Changelog, v0.2:
  • Error corrected in matrixMultiplication(double[] A, double[][] B), many thanks to David Cabanillas for finding and fixing it
  • Changed license to LGPL



Usage / Anwendung

The usage is quite simple and straight forward: First, you have to "train" (line 5) and then, you can predict the group membership of new objects (line 6 and below).

Example:


// You need a double array with the features of the objects
// and an int-array with their group membership
int[] group = { 1, 1, 1, 1, 2, 2, 2 };
double[][] data = { { 2.95, 6.63 }, { 2.53, 7.79 }, { 3.57, 5.65 },
   { 3.16, 5.47 }, { 2.58, 4.46 }, { 2.16, 6.22 }, { 3.27, 3.52 } };

//The LDA is "trained"
LDA test = new LDA(data, group, true);

//Now we will try to classify new data
double[] testData = { 2.81, 5.46 };
System.out.println("Predicted group: " + test.predict(testData));

//Let's have a look at the values of the discriminat functions
double[] values = test.getDiscriminantFunctionValues(testData);
for(int i = 0; i < values.length; i++){
   System.out.println("Discriminant function " + (i+1) 
                                            + ": " + values[i]);
}