コード例 #1
0
        /*************************************************************************
        This function performs clustering by k-means++ algorithm.

        You may change algorithm properties by calling:
        * ClusterizerSetKMeansLimits() to change number of restarts or iterations
        * ClusterizerSetKMeansInit() to change initialization algorithm

        By  default,  one  restart  and  unlimited number of iterations are  used.
        Initialization algorithm is chosen automatically.

        COMMERCIAL EDITION OF ALGLIB:

          ! Commercial version of ALGLIB includes  two important  improvements  of
          ! this function:
          ! * multicore support (can be used from C# and C++)
          ! * access to high-performance C++ core (actual for C# users)
          !
          ! K-means clustering  algorithm has two  phases:  selection  of  initial
          ! centers  and  clustering  itself.  ALGLIB  parallelizes  both  phases.
          ! Parallel version is optimized for the following  scenario:  medium  or
          ! high-dimensional problem (20 or more dimensions) with large number  of
          ! points and clusters. However, some speed-up can be obtained even  when
          ! assumptions above are violated.
          !
          ! As for native-vs-managed comparison, working with native  core  brings
          ! 30-40% improvement in speed over pure C# version of ALGLIB.
          !
          ! We recommend you to read 'Working with commercial version' section  of
          ! ALGLIB Reference Manual in order to find out how to  use  performance-
          ! related features provided by commercial edition of ALGLIB.

        INPUT PARAMETERS:
            S       -   clusterizer state, initialized by ClusterizerCreate()
            K       -   number of clusters, K>=0.
                        K  can  be  zero only when algorithm is called  for  empty
                        dataset,  in   this   case   completion  code  is  set  to
                        success (+1).
                        If  K=0  and  dataset  size  is  non-zero,  we   can   not
                        meaningfully assign points to some center  (there  are  no
                        centers because K=0) and  return  -3  as  completion  code
                        (failure).

        OUTPUT PARAMETERS:
            Rep     -   clustering results; see description of KMeansReport
                        structure for more information.

        NOTE 1: k-means  clustering  can  be  performed  only  for  datasets  with
                Euclidean  distance  function.  Algorithm  will  return   negative
                completion code in Rep.TerminationType in case dataset  was  added
                to clusterizer with DistType other than Euclidean (or dataset  was
                specified by distance matrix instead of explicitly given points).

          -- ALGLIB --
             Copyright 10.07.2012 by Bochkanov Sergey
        *************************************************************************/
        public static void clusterizerrunkmeans(clusterizerstate s,
            int k,
            kmeansreport rep)
        {
            double[,] dummy = new double[0,0];

            alglib.ap.assert(k>=0, "ClusterizerRunKMeans: K<0");
            
            //
            // Incorrect distance type
            //
            if( s.disttype!=2 )
            {
                rep.npoints = s.npoints;
                rep.terminationtype = -5;
                rep.k = k;
                rep.iterationscount = 0;
                rep.energy = 0.0;
                return;
            }
            
            //
            // K>NPoints or (K=0 and NPoints>0)
            //
            if( k>s.npoints || (k==0 && s.npoints>0) )
            {
                rep.npoints = s.npoints;
                rep.terminationtype = -3;
                rep.k = k;
                rep.iterationscount = 0;
                rep.energy = 0.0;
                return;
            }
            
            //
            // No points
            //
            if( s.npoints==0 )
            {
                rep.npoints = 0;
                rep.terminationtype = 1;
                rep.k = k;
                rep.iterationscount = 0;
                rep.energy = 0.0;
                return;
            }
            
            //
            // Normal case:
            // 1<=K<=NPoints, Euclidean distance 
            //
            rep.npoints = s.npoints;
            rep.nfeatures = s.nfeatures;
            rep.k = k;
            rep.npoints = s.npoints;
            rep.nfeatures = s.nfeatures;
            kmeansgenerateinternal(s.xy, s.npoints, s.nfeatures, k, s.kmeansinitalgo, s.kmeansmaxits, s.kmeansrestarts, s.kmeansdbgnoits, ref rep.terminationtype, ref rep.iterationscount, ref dummy, false, ref rep.c, true, ref rep.cidx, ref rep.energy, s.kmeanstmp);
        }
コード例 #2
0
 /*************************************************************************
 Single-threaded stub. HPC ALGLIB replaces it by multithreaded code.
 *************************************************************************/
 public static void _pexec_clusterizerrunkmeans(clusterizerstate s,
     int k,
     kmeansreport rep)
 {
     clusterizerrunkmeans(s,k,rep);
 }
コード例 #3
0
 public override alglib.apobject make_copy()
 {
     kmeansreport _result = new kmeansreport();
     _result.npoints = npoints;
     _result.nfeatures = nfeatures;
     _result.terminationtype = terminationtype;
     _result.iterationscount = iterationscount;
     _result.energy = energy;
     _result.k = k;
     _result.c = (double[,])c.Clone();
     _result.cidx = (int[])cidx.Clone();
     return _result;
 }