private void init(double[,] inputs, int[] outputs)
        {
            // Gets the number of classes
            int startingClass = outputs.Min();

            this.classes = outputs.Max() - startingClass + 1;

            // Store the original data
            this.source    = inputs;
            this.outputs   = outputs;
            this.dimension = inputs.GetLength(1);

            // Creates simple structures to hold information later
            this.classCount     = new int[classes];
            this.classMeans     = new double[classes][];
            this.classStdDevs   = new double[classes][];
            this.classScatter   = new double[classes][, ];
            this.projectedMeans = new double[classes][];


            // Creates the object-oriented structure to hold information about the classes
            DiscriminantAnalysisClass[] collection = new DiscriminantAnalysisClass[classes];
            for (int i = 0; i < classes; i++)
            {
                collection[i] = new DiscriminantAnalysisClass(this, i, startingClass + i);
            }
            this.classCollection = new DiscriminantAnalysisClassCollection(collection);
        }
        //---------------------------------------------


        #region Constructor
        /// <summary>
        ///   Constructs a new Linear Discriminant Analysis object.
        /// </summary>
        /// <param name="inputs">The source data to perform analysis. The matrix should contain
        /// variables as columns and observations of each variable as rows.</param>
        /// <param name="outputs">The labels for each observation row in the input matrix.</param>
        public LinearDiscriminantAnalysis(double[,] inputs, int[] outputs)
        {
            // Initial argument checking
            if (inputs == null)
            {
                throw new ArgumentNullException("inputs");
            }
            if (outputs == null)
            {
                throw new ArgumentNullException("outputs");
            }

            if (inputs.GetLength(0) != outputs.Length)
            {
                throw new ArgumentException("The number of rows in the input array must match the number of given outputs.");
            }


            // Gets the number of classes
            int startingClass = outputs.Min();

            this.classes = outputs.Max() - startingClass + 1;

            // Store the original data
            this.source    = inputs;
            this.outputs   = outputs;
            this.dimension = inputs.GetLength(1);

            // Creates simple structures to hold information later
            this.classCount     = new int[classes];
            this.classMeans     = new double[classes][];
            this.classStdDevs   = new double[classes][];
            this.classScatter   = new double[classes][, ];
            this.projectedMeans = new double[classes][];


            // Creates the object-oriented structure to hold information about the classes
            DiscriminantAnalysisClass[] collection = new DiscriminantAnalysisClass[classes];
            for (int i = 0; i < classes; i++)
            {
                collection[i] = new DiscriminantAnalysisClass(this, i, startingClass + i);
            }
            this.classCollection = new DiscriminantAnalysisClassCollection(collection);
        }
예제 #3
0
        /// <summary>
        ///   Creates additional information about principal components.
        /// </summary>
        ///
        protected void CreateDiscriminants()
        {
            if (classCollection == null)
            {
                // Creates simple structures to hold information later
                this.classCount     = new int[NumberOfOutputs];
                this.classMeans     = new double[NumberOfOutputs][];
                this.classStdDevs   = new double[NumberOfOutputs][];
                this.classScatter   = new double[NumberOfOutputs][][];
                this.projectedMeans = new double[NumberOfOutputs][];


                // Creates the object-oriented structure to hold information about the classes
                var collection = new DiscriminantAnalysisClass[NumberOfOutputs];
                for (int i = 0; i < collection.Length; i++)
                {
                    collection[i] = new DiscriminantAnalysisClass(this, i, i);
                }
                this.classCollection = new DiscriminantAnalysisClassCollection(collection);
            }

            int numDiscriminants = eigenvalues.Length;

            discriminantProportions = new double[numDiscriminants];
            discriminantCumulative  = new double[numDiscriminants];


            // Calculate total scatter matrix
            St = Sw.Add(Sb);

            // Calculate proportions
            double sum = 0.0;

            for (int i = 0; i < numDiscriminants; i++)
            {
                sum += System.Math.Abs(eigenvalues[i]);
            }
            sum = (sum == 0) ? 0 : (1.0 / sum);

            for (int i = 0; i < numDiscriminants; i++)
            {
                discriminantProportions[i] = System.Math.Abs(eigenvalues[i]) * sum;
            }


            // Calculate cumulative proportions
            if (numDiscriminants > 0)
            {
                this.discriminantCumulative[0] = this.discriminantProportions[0];
                for (int i = 1; i < this.discriminantCumulative.Length; i++)
                {
                    this.discriminantCumulative[i] = this.discriminantCumulative[i - 1] + this.discriminantProportions[i];
                }
            }


            // Creates the object-oriented structure to hold the linear discriminants
            var discriminants = new Discriminant[numDiscriminants];

            for (int i = 0; i < discriminants.Length; i++)
            {
                discriminants[i] = new Discriminant(this, i);
            }
            this.discriminantCollection = new DiscriminantCollection(discriminants);
        }