コード例 #1
0
        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);
        }
コード例 #2
0
        protected void init(double[,] inputs, int[] outputs)
        {
            // Gets the number of classes
            int startingClass = outputs.Min();

            this.NumberOfClasses = outputs.Max() - startingClass + 1;
            this.NumberOfSamples = inputs.Rows();
            this.NumberOfInputs  = inputs.Columns();
            this.NumberOfOutputs = inputs.Columns();

            // Store the original data
            this.Source          = inputs;
            this.Classifications = outputs;

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

            // Creates the object-oriented structure to hold information about the classes
            var collection = new DiscriminantAnalysisClass[NumberOfClasses];

            for (int i = 0; i < collection.Length; i++)
            {
                collection[i] = new DiscriminantAnalysisClass(this, i, startingClass + i);
            }
            this.Classes = new DiscriminantAnalysisClassCollection(collection);
        }
コード例 #3
0
        /// <summary>
        ///   Initializes common properties.
        /// </summary>
        ///
        protected void Init(double[][] inputs, int[] outputs)
        {
#pragma warning disable 612, 618
            if (this.Classifications != null)
            {
                return; // TODO: remove this
            }
#pragma warning restore 612, 618

            // Gets the number of classes
            this.NumberOfClasses = outputs.Max() + 1;
            this.NumberOfSamples = inputs.Rows();
            this.NumberOfInputs  = inputs.Columns();
            //if (this.NumberOfOutputs == 0)
            //    this.NumberOfOutputs = inputs.Columns();

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

            // Creates the object-oriented structure to hold information about the classes
            var collection = new DiscriminantAnalysisClass[NumberOfClasses];
            for (int i = 0; i < collection.Length; i++)
            {
                collection[i] = new DiscriminantAnalysisClass(this, i, i);
            }
            this.Classes = new DiscriminantAnalysisClassCollection(collection);
        }
コード例 #4
0
        //---------------------------------------------


        #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);
        }
コード例 #5
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);
        }