Search code examples
c#alglib

C# Linear Discriminant Analysis with Alglib


Suppose I have a data set with 10 classes. Each class contains 3 points in 3D:

Class 1: (1,2,3),(4,5,6),(7,8,9)
Class 2: (2,3,4),(5,6,7),(8,9,10)
.
.
Class 10: (10,15,20),(10,11,19),(4,8,9)

How can I determine if class X belongs to to one of the classes above using Alglib LDA (or any other free LDA library)?

Example code will be appreciated.


Solution

  • After some time I finally figured it out:

    static public double[,] Test()
        {
            // This example is for points in 3D.
            // The forth variable is the class label. In this case 2 classes: 0 and 1.
            double[,] xy = new double[,]
            {
            { 4,2,1, 0 }, { 2,4,2, 0 }, { 2,3,3, 0 }, { 3,6,4, 0 }, { 4,4,5, 0 },
            { 9,10,10, 1 }, { 6,8,11, 1 }, { 9,5,12, 1 }, { 8,7,9, 1 }, { 10,8,10, 1 }
            };
    
            int NPoints = 10;
            int NVars = 3; // 1 for 1 dimension, 2 for 2D and so on...
            int NClasses = 2;
    
            int info = 0;
            double[,] w = new double[0, 0];
    
            alglib.lda.fisherldan(xy, NPoints, NVars, NClasses, ref info, ref w);
    
            return w; // The projection vector/s.
        }