DataSharp/Data/Analysis/SupportVectorMachine.cs
/// <summary>
/// Training this SVM using the SMO algorithm.
/// </summary>
/// <param name="maxIterations">Max count of iterations.</param>
/// <exception cref="ArgumentNullException">If Kernel is null.</exception>
Training this SVM using the SMO algorithm.
Training this SVM using the SMO algorithm .
public void Train(int maxIterations = 100)
        {
            if (Kernel == null)
                throw new ArgumentNullException("Kernel");
            for (int i = 0; i < dataMatrix.RowCount; i++)
                kernelValues.SetColumn(i, Kernel.KernelTransform(dataMatrix, dataMatrix.GetRow(i)));
            int iterations = 0;
            int alphaPairsChanged = 0;
            bool entireSet = true;
            while (((iterations < maxIterations) && (alphaPairsChanged > 0)) || 
                   (entireSet))
            {
                alphaPairsChanged = 0;
                if (entireSet)
                {
                    for (int i = 0; i < sampleCount; i++)
                        alphaPairsChanged += InnerLoop(i);
                    iterations++;
                }
                else
                {
                    LinkedList<int> nonBoundIndices = GetNonBoundIndices();
                    foreach (int i in nonBoundIndices)
                        alphaPairsChanged += InnerLoop(i);
                    iterations++;
                }
                if (entireSet)
                    entireSet = false;
                else if (alphaPairsChanged == 0)
                    entireSet = true;
            }
             Calculate weights of this SVM.
            CalculateSupportVectors();
        }
public void Train ( int maxIterations = 100 ) { if ( Kernel == null ) throw new ArgumentNullException ( " " ) ; for ( int i = 0 ; i < dataMatrix . RowCount ; i ++ ) kernelValues . SetColumn ( i , Kernel . KernelTransform ( dataMatrix , dataMatrix . GetRow ( i ) ) ) ; int iterations = 0 ; int alphaPairsChanged = 0 ; bool entireSet = true ; while ( ( ( iterations < maxIterations ) && ( alphaPairsChanged > 0 ) ) || ( entireSet ) ) { alphaPairsChanged = 0 ; if ( entireSet ) { for ( int i = 0 ; i < sampleCount ; i ++ ) alphaPairsChanged += InnerLoop ( i ) ; iterations ++ ; } else { LinkedList < int > nonBoundIndices = GetNonBoundIndices ( ) ; foreach ( int i in nonBoundIndices ) alphaPairsChanged += InnerLoop ( i ) ; iterations ++ ; } if ( entireSet ) entireSet = false ; else if ( alphaPairsChanged == 0 ) entireSet = true ; } CalculateSupportVectors ( ) ; }
Training this SVM using the SMO algorithm.
Training this SVM using the SMO algorithm .
// Iteration count starts from 0. // The counter of how many pairs of lagrange multipliers changed in an iteration. // The boolean flag that indicate whether last iteration traversed the entire traning set or not. // Iterate until converge or reach maxIterations. // No lagrange multiplier changed at the beginning of this iteration. // // Calculate weights of this SVM.
If Kernel is null.
If Kernel is null .
maxIterations
int
Max count of iterations.
Max count of iterations .