Example #1
0
        /// <summary>
        /// Get coefficients from the Langrange Multipliers
        ///
        /// This function assumed the last attribute in trainingData is the target value
        /// </summary>
        /// <returns></returns>
        public ModelKernel computeSupportVectors(double[][] trainingData,
                                                 int indexTargetAttribute,
                                                 IKernel kernel,
                                                 int maxThreads)
        {
            //Update private Vars -----------------
            _kernel      = kernel;
            _lagrangeMul = new double[trainingData[0].Length];
            _error_cache = new double[trainingData[0].Length];
            SupportFunctions.InitArray(_lagrangeMul, maxThreads);
            _indexTarget  = indexTargetAttribute;
            _trainingData = trainingData;
            //---------------------------

            //main routine:
            int  numChanged = 0;
            bool examineAll = true;

            while (numChanged > 0 || examineAll)
            {
                numChanged = 0;
                if (examineAll)
                {
                    //loop I over all training examples
                    for (int row = 0; row < trainingData[0].Length; row++)
                    {
                        numChanged += examineExample(row);
                    }
                }
                else
                {
                    //loop I over examples where alpha is not 0 & not C
                    for (int row = 0; row < trainingData[0].Length; row++)
                    {
                        if (_lagrangeMul[row] != 0 && _lagrangeMul[row] != _C)
                        {
                            double[] data = SupportFunctions.GetLinearArray(trainingData, row);
                            numChanged += examineExample(row);
                        }
                    }
                }

                if (examineAll)
                {
                    examineAll = false;
                }
                else if (numChanged == 0)
                {
                    examineAll = true;
                }
            }
            return(getModelKernel());
        }