Beispiel #1
0
        /// <summary>
        /// Calculates the distance.
        /// </summary>
        /// <param name="metric">The metric.</param>
        /// <param name="bmu">The bmu.</param>
        /// <param name="gridNeuron">The grid neuron.</param>
        /// <returns>System.Double.</returns>
        public double CalculateDistance(IMetric metric, IBestMatchingUnit bmu, IGridNeuron gridNeuron)
        {
            var distanceToBmu = metric.CalculateDistance(bmu.GridCoordinates, gridNeuron.GridCoordinates);

            if (!IsToroidal)
            {
                return(distanceToBmu);
            }

            // on spherical grids, check the wrap-around distances
            //
            //
            //   X 1 2 2 1
            //   1 2 3 3 2
            //   2 3 4 4 3
            //   1 2 3 3 2

            var coordinates   = gridNeuron.GridCoordinates;
            var coordinatesX  = new[] { coordinates[0] - Width + 2, coordinates[1] };
            var coordinatesY  = new[] { coordinates[0], coordinates[1] - Height + 2 };
            var coordinatesXy = new[] { coordinatesX[0], coordinatesY[1] };

            distanceToBmu = Math.Min(distanceToBmu, metric.CalculateDistance(bmu.GridCoordinates, coordinatesX));
            distanceToBmu = Math.Min(distanceToBmu, metric.CalculateDistance(bmu.GridCoordinates, coordinatesY));
            distanceToBmu = Math.Min(distanceToBmu, metric.CalculateDistance(bmu.GridCoordinates, coordinatesXy));
            return(distanceToBmu);
        }
Beispiel #2
0
        private void QueryHelper(IMetric <T> metric, int radius, IDictionary <BKTreeNode <T>, int> acc)
        {
            int distanceToTargetElement = metric.CalculateDistance(_data);

            if (distanceToTargetElement <= radius)
            {
                acc.Add(this, distanceToTargetElement);
            }

            int lowerBound = distanceToTargetElement - radius;
            int upperBound = distanceToTargetElement + radius;

            foreach (int distance in _children.Keys)
            {
                if (distance > lowerBound || distance <= upperBound)
                {
                    _children[distance].QueryHelper(metric, radius, acc);
                }
            }
        }
        public int Classify(ArticleRepo trainingSet, ArticleRepo testedSet, int neighboursNumber, IMetric metric)
        {
            Dictionary <Article[], double> distancesDict = new Dictionary <Article[], double>();
            List <string> trainingLabels = new List <string>();

            int truePositiveCounter = 0;


            foreach (Article testedArticle in testedSet.articles)
            {
                foreach (Article trainingArticle in trainingSet.articles)
                {
                    double    distance      = metric.CalculateDistance(trainingArticle.AllCharacteristicValues, testedArticle.AllCharacteristicValues);
                    Article[] articleIDPair = { testedArticle, trainingArticle };
                    distancesDict[articleIDPair] = distance;
                }

                var sortedDict = distancesDict.OrderBy(x => x.Value).Take(neighboursNumber);

                foreach (var item in sortedDict)
                {
                    string trainingLabel = item.Key.ElementAt(1).Label;
                    trainingLabels.Add(trainingLabel);
                }

                string testedLabel = trainingLabels.GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key;

                if (testedLabel == testedArticle.Label)
                {
                    truePositiveCounter++;
                }

                trainingLabels.Clear();
                distancesDict.Clear();
            }

            return(truePositiveCounter);
        }