Beispiel #1
0
        /// <summary>
        /// Starts the task.
        /// </summary>
        /// <param name="dataSet">The list of objects that to be converted.</param>
        /// <param name="distanceMetric">The distance metric used to scale down dimensions.</param>
        public void Start(IdentifiableDataPointCollection dataSet, IDistanceMetric distanceMetric)
        {
            var args = new TaskRunnerArgumentSet {
                Data = dataSet, DistanceMetric = distanceMetric
            };

            var task = Task.Factory.StartNew <DataConversionResult>(TaskRunner, args);

            // Make sure Success and Failure events are run within the caller thread.
            TaskScheduler currentContext = TaskScheduler.FromCurrentSynchronizationContext();

            task.ContinueWith(TaskComplete, currentContext);
            task.ContinueWith(TaskFaulted, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, currentContext);
        }
Beispiel #2
0
        public KMeans(IdentifiableDataPointCollection dataCollection, int[] centroidIndicies, IDistanceMetric distanceMetric, int maxIterations = 100)
        {
            this.dataCollection = dataCollection;
            this.maxIterations  = maxIterations;
            this.distanceMetric = distanceMetric;

            if (centroidIndicies.Length != centroidIndicies.Distinct().Count())
            {
                throw new ArgumentException(
                          "Array contains dublicate indicies, which is not allowed.", "centroidIndicies");
            }

            this.centroids = centroidIndicies
                             .Select(index => this.dataCollection[index].Clone())
                             .ToList();

            EnsureDistinctCentroid();
        }
Beispiel #3
0
        public KMeans(IdentifiableDataPointCollection dataCollection, int k, IDistanceMetric distanceMetric, int maxIterations = 100)
        {
            if (k < 1)
            {
                throw new ArgumentException("Clusters cannot be generated for less than one centroid");
            }

            this.dataCollection = dataCollection;
            this.maxIterations  = maxIterations;
            this.distanceMetric = distanceMetric;
            this.centroids      = dataCollection
                                  .OrderBy(dataPoint => Guid.NewGuid()) // Random order
                                  .Take(k)
                                  .Select(dataPoint => dataPoint.Clone())
                                  .ToList();

            EnsureDistinctCentroid();
        }
        public IdentifiableDataPointCollection Generate()
        {
            distanceMetric = new EuclideanMetric();
            dataCollection = new IdentifiableDataPointCollection();

            int id         = 0;
            int dimentions = 5;

            dataCollection = new IdentifiableDataPointCollection();
            for (int i = 0; i < collectionSize; i++)
            {
                dataPoint = new IdentifiableDataPoint(++id, dimentions);
                dataCollection.AddItem(dataPoint);
            }

            for (int i = 0; i < collectionSize; i++)
            {
                if ((i % 10) < 9)
                {
                    dataCollection[i].AddAttribute("Gender", 1d);
                }
                else
                {
                    dataCollection[i].AddAttribute("Gender", 0d);
                }

                if ((i % 10) < 4)
                {
                    dataCollection[i].AddAttribute("Income", 1d);
                }
                else if ((i % 10) > 8)
                {
                    dataCollection[i].AddAttribute("Income", 0.1429);
                }
                else
                {
                    dataCollection[i].AddAttribute("Income", 0.2858d);
                }

                dataCollection[i].AddAttribute("Age", 0.16d);

                if ((i % 10) < 2 || (i % 10) > 3)
                {
                    dataCollection[i].AddAttribute("Purchase", 1d);
                }
                else
                {
                    dataCollection[i].AddAttribute("Purchase", 0.5d);
                }

                if ((i % 10) < 2)
                {
                    dataCollection[i].AddAttribute("Control", 0.5d);
                }
                else if ((i % 10) > 8)
                {
                    dataCollection[i].AddAttribute("Control", 0d);
                }
                else
                {
                    dataCollection[i].AddAttribute("Control", 1d);
                }
            }
            return(dataCollection);
        }