예제 #1
0
        public void inverse_gauss()
        {
            var rand    = new Random(Seed);
            var n       = 1000;
            var samples = new float[n];

            for (var i = 0; i < samples.Length; i++)
            {
                samples[i] = (float)GaussHelper.InvPhi(rand.NextDouble());
            }

            Assert.True(Math.Abs(0 - samples.Mean()) < 0.1);
            Assert.True(Math.Abs(1.0 - samples.Variance()) < 0.5);
        }
예제 #2
0
        public int[] Train(InstanceRepresentation set)
        {
            set.Standardize();
            var instances = set.Instances.ToArray();

            _weights      = new float[_gridDimensions[0] * _gridDimensions[1], set.FeauturesCount];
            FeaturesCount = set.FeauturesCount;

            // Intialize weights with gaussians
            var n = _gridDimensions[0] * _gridDimensions[1];

            if (!_usePlusPlusInit || n >= instances.Length)
            {
                for (ushort i = 0; i < n; i++)
                {
                    for (ushort j = 0; j < FeaturesCount; j++)
                    {
                        _weights[i, j] = (float)GaussHelper.InvPhi(_random.NextDouble());
                    }
                }
            }
            else
            {
                _weights = PlusPlusInitializer.InitializeCentroids(n, instances, _random);
            }

            for (var i = 0; i < _iterationsCount; i++)
            {
                var instance = instances[_random.Next(instances.Length)];
                // Best Matching Unit
                var bmuIndex = Instances.MinEucDistanceIndex(instance, _weights);
                BMUCoordinates = ToCoordinates(bmuIndex);
                UpdateHexagonWeights((ushort)NeighbourhoodRadius(i), LearningRate(i), BMUCoordinates, instance.GetValues());
            }

            var instancesClusters = new int[instances.Length];

            for (var i = 0; i < instances.Length; i++)
            {
                instancesClusters[i] = Instances.MinEucDistanceIndex(instances[i], _weights);
            }

            return(instancesClusters);
        }