Exemple #1
0
        public object Calculate(CustomDataElement testDataObject, int k, List <string> fileData, string[] selectColumns, string classColumn)
        {
            var data = DataSetup.SetUpData(fileData, selectColumns, classColumn);

            var neighbors = GetNeighbors(data, testDataObject, k);

            var type = SelectType(neighbors);

            return(type);
        }
Exemple #2
0
        /// <summary>
        /// Calculates distance between points using Euclide algorithm.
        /// </summary>
        /// <param name="columnElements"> points</param>
        /// <param name="testDataObject"> testing object</param>
        /// <returns> distance between elements</returns>
        private double CalculateDistance(CustomDataElement testDataObject, List <double> columnElements)
        {
            var distance = 0.0;

            for (int i = 0; i < testDataObject.Elements.Count; i++)
            {
                distance += Math.Pow((columnElements[i] - testDataObject.Elements[i]), 2);
            }

            return(Math.Sqrt(distance));
        }
        /// <summary>
        /// Main calculation method.
        /// </summary>
        /// <param name="testDataElement">new test object data</param>
        /// <param name="fileData">input data</param>
        /// <param name="selectColumns">selected data column numbers</param>
        /// <param name="classColumn">class column number</param>
        /// <returns></returns>
        public string Calculate(CustomDataElement testDataElement, List <string> fileData, string[] selectColumns, string classColumn)
        {
            var data = DataSetup.SetUpData(fileData, selectColumns, classColumn);

            var classDictionary = SeparateData(data);

            var summary = GetClassSummary(classDictionary);

            var probabilities = GetProbabilities(summary, testDataElement);

            var prediction = GetPrediction(probabilities);

            return(prediction.Key);
        }
Exemple #4
0
        /// <summary>
        /// Returns reighbor list using selected test data.
        /// </summary>
        /// <returns> neighbor list </returns>
        private List <Distances> GetNeighbors(List <CustomDataElement> data, CustomDataElement testDataObject, int k)
        {
            List <Distances> distances = new List <Distances>();
            List <Distances> neighbors = new List <Distances>();

            foreach (var obj in data)
            {
                var distance = CalculateDistance(testDataObject, obj.Elements);
                distances.Add(new Distances()
                {
                    Data = obj, Distance = distance
                });
            }

            List <Distances> sortedDistances = distances.OrderBy(t => t.Distance).ToList();

            for (int i = 0; i < k; i++)
            {
                neighbors.Add(sortedDistances[i]);
            }

            return(neighbors);
        }
        /// <summary>
        /// Calculates every class probability by new test object
        /// </summary>
        /// <param name="summary">list</param>
        /// <param name="testDataElement"> new test object parameters </param>
        /// <returns>probabilities</returns>
        private Dictionary <string, double> GetProbabilities(Dictionary <string, List <SummaryListElement> > summary, CustomDataElement testDataElement)
        {
            Dictionary <string, double> probabilities = new Dictionary <string, double>();

            foreach (var summaryItem in summary)
            {
                probabilities.Add(summaryItem.Key, 1);

                for (int i = 0; i < summaryItem.Value.Count; i++)
                {
                    var item            = summaryItem.Value[i];
                    var testItemElement = testDataElement.Elements[i];

                    var prob = CalculateProbability(item, testItemElement);
                    if (!prob.Equals(0))
                    {
                        probabilities[summaryItem.Key] *= prob;
                    }
                }
            }
            return(probabilities);
        }