public static bool isSampleOk(RichTextBox attrBox, SampleColection samplesColection, out string error)
        {
            if (attrBox.Text == "")
            {
                error = "You must enter a sample\n";
                return(false);
            }
            var text = attrBox.Text.Trim().Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < text.Length; i++)
            {
                text[i] = Data.convertDoubleFormat(text[i].Trim());
                double result;
                bool   parsed = double.TryParse(text[i], out result);
                if (!parsed)
                {
                    error = "Wrong sample format\n";
                    return(false);
                }
            }
            int fileSamplesLength = samplesColection.samples.First().attributes.Count;

            if (text.Length != fileSamplesLength)
            {
                error = "There must be the same amount of attributes in attributes filed like in samples in file";
                return(false);
            }
            error = "Ok";
            return(true);
        }
 public DataValidation(SampleColection sampleColection, DataLoader dataLoader, ComboBox comboBox, RichTextBox kBox, RichTextBox attrBox)
 {
     if (!isDataLoaded(dataLoader, out string dataError))
     {
         message += dataError;
         return;
     }
     if (!isSampleColectionLoaded(sampleColection, out string sampleColectionError))
     {
         message += sampleColectionError;
         return;
     }
     if (!isSampleOk(attrBox, sampleColection, out string sampleError))
     {
         message += sampleError;
         return;
     }
     if (!isCorrectK(kBox, sampleColection, out string kError))
     {
         message += kError;
         return;
     }
     if (!isMetricChosen(comboBox, out string metricError))
     {
         message += metricError;
         return;
     }
     isValid = true;
 }
 public static bool isSampleColectionLoaded(SampleColection sampleColection, out string error)
 {
     if (sampleColection == null)
     {
         error = "You must load correct file first";
         return(false);
     }
     error = "Ok";
     return(true);
 }
Exemple #4
0
        public SampleColection copy()
        {
            SampleColection newSampleColection = new SampleColection(new Data(new string[] { }).dataFromFile);

            foreach (Sample sample in samples)
            {
                newSampleColection.addSample(sample.copy());
            }
            return(newSampleColection);
        }
        public static SampleColection normalizeSampleColection(SampleColection sampleColection)
        {
            SampleColection normalizedSampleColection = new SampleColection(new Data(new string[] { }).dataFromFile);

            foreach (Sample sample in sampleColection.samples)
            {
                var normalizedSample = new Sample(normalizeAttributes(sampleColection, sample.attributes), sample.decision);
                normalizedSampleColection.samples.Add(normalizedSample);
            }
            return(normalizedSampleColection);
        }
        public static Dictionary <int, double> minAttributes(SampleColection sampleColection, int numberOfAttr)
        {
            var    minAttributes = new Dictionary <int, double>();
            double min;

            for (int i = 0; i < numberOfAttr; i++)
            {
                min = findMinAttr(sampleColection, i);
                minAttributes.Add(i, min);
            }
            return(minAttributes);
        }
        public static Dictionary <int, double> maxAttributes(SampleColection sampleColection, int numberOfAttr)
        {
            var    maxAttributes = new Dictionary <int, double>();
            double max;

            for (int i = 0; i < numberOfAttr; i++)
            {
                max = findMaxAttr(sampleColection, i);
                maxAttributes.Add(i, max);
            }
            return(maxAttributes);
        }
        public static double findMinAttr(SampleColection sampleColection, int attrNumber)
        {
            double min = sampleColection.samples[0].attributes[attrNumber];

            foreach (Sample sample in sampleColection.samples)
            {
                if (sample.attributes[attrNumber] < min)
                {
                    min = sample.attributes[attrNumber];
                }
            }
            return(min);
        }
        public static double findMaxAttr(SampleColection sampleColection, int attrNumber)
        {
            double max = sampleColection.samples[0].attributes[attrNumber];

            foreach (Sample sample in sampleColection.samples)
            {
                if (sample.attributes[attrNumber] > max)
                {
                    max = sample.attributes[attrNumber];
                }
            }
            return(max);
        }
Exemple #10
0
        public static int maxK(SampleColection sampleColection)
        {
            var segregatedSamples = sampleColection.segregateSamplesByDecision();
            int minAmount         = segregatedSamples.First().Value.Count;

            foreach (var item in segregatedSamples)
            {
                if (item.Value.Count < minAmount)
                {
                    minAmount = item.Value.Count;
                }
            }
            return(minAmount);
        }
        public static List <double> normalizeAttributes(SampleColection sampleColection, List <double> attributes)
        {
            List <double> normalizedAttributes = new List <double>();
            var           minDict = new Dictionary <int, double>();
            var           maxDict = new Dictionary <int, double>();

            minDict = minAttributes(sampleColection, attributes.Count);
            maxDict = maxAttributes(sampleColection, attributes.Count);
            for (int i = 0; i < attributes.Count; i++)
            {
                normalizedAttributes.Add(normalizeAttribute(attributes[i], minDict[i], maxDict[i]));
            }
            return(normalizedAttributes);
        }
        public static bool isCorrectK(RichTextBox kBox, SampleColection sampleColection, out string error)
        {
            int  maxK = KChecker.maxK(sampleColection);
            int  result;
            bool parsed = int.TryParse(kBox.Text, out result);

            if (!parsed)
            {
                error = "K must be an integer\n";
                return(false);
            }
            if (result < 1 || result > maxK)
            {
                error = "K must be an integer between 1 and " + maxK + "\n";
                return(false);
            }
            error = "Ok";
            return(true);
        }
Exemple #13
0
        public static double precision(SampleColection sampleColection, int k, Metrics.metric choosedMetric, int p)
        {
            SampleColection newSampleColection = sampleColection.copy();
            int             probes             = 0;
            int             correct            = 0;
            var             samples            = newSampleColection.samples;
            Sample          tmp;

            for (int i = 0; i < samples.Count; i++)
            {
                probes++;
                tmp = newSampleColection.samples.First();
                newSampleColection.removeSample(tmp);
                int?result = Algorithm.chooseDecision(tmp.attributes, newSampleColection, k, choosedMetric, p);
                if (result == tmp.decision)
                {
                    correct++;
                }
                newSampleColection.addSample(tmp);
            }
            return((double)correct / probes * 100);
        }
        private void loadData_Click(object sender, EventArgs e)
        {
            dataLoader = new DataLoader(ofd);
            if (!DataValidation.isDataLoaded(dataLoader, out string dataError))
            {
                dataInfo.Text = dataError;
                return;
            }
            sampleColection = new SampleColection(dataLoader.data);
            var samplesAmount    = sampleColection.countSamples();
            var decisionAmount   = sampleColection.possibleDecisions().Count;
            var attributesAmount = sampleColection.samples.First().attributes.Count;
            var maxK             = KChecker.maxK(sampleColection);

            dataInfo.Text        = "File OK\n";
            dataInfo.Text       += "Loaded " + samplesAmount + " samples\n";
            dataInfo.Text       += "There are " + decisionAmount + " decisions possible\n";
            dataInfo.Visible     = true;
            attributesInfo.Text  = "Attributes";
            attributesInfo.Text += "\nEnter " + attributesAmount + " attributes";
            kInfo.Text           = "K";
            kInfo.Text          += "\nK must be between 1 and " + maxK;
        }
        public static int?chooseDecision(List <double> attributesToCheck, SampleColection sampleColection, int k, Metrics.metric choosedMetric, int p)
        {
            var normalizedAttributes      = Normalization.normalizeAttributes(sampleColection, attributesToCheck);
            var normalizedSampleColection = Normalization.normalizeSampleColection(sampleColection);
            var minDistances = new Dictionary <int, double>();

            minDistances = countSumOfKminDistances(normalizedAttributes, normalizedSampleColection, k, choosedMetric, p);
            double min      = minDistances.First().Value;
            int    decision = minDistances.First().Key;

            foreach (var item in minDistances)
            {
                if (item.Value < min)
                {
                    min      = item.Value;
                    decision = item.Key;
                }
            }
            if (isOnlyOneMin(minDistances, min))
            {
                return(decision);
            }
            return(null);
        }
        static Dictionary <int, double> countSumOfKminDistances(List <double> sampleToCheck, SampleColection data, int k, Metrics.metric choosedMetric, int p)
        {
            var distances = new Dictionary <int, List <double> >();

            distances = countDistances(sampleToCheck, data, choosedMetric, p);
            double sumOfKminDistances = 0;
            var    minDistances       = new Dictionary <int, double>();

            foreach (var item in distances)
            {
                item.Value.Sort();
                for (int i = 0; i < k; i++)
                {
                    sumOfKminDistances += item.Value[i];
                }
                minDistances.Add(item.Key, sumOfKminDistances);
                sumOfKminDistances = 0;
            }
            return(minDistances);
        }
        static Dictionary <int, List <double> > countDistances(List <double> sampleToCheck, SampleColection data, Metrics.metric choosedMetric, int p)
        {
            var    distances = new Dictionary <int, List <double> >();
            double distance;

            foreach (Sample sample in data.samples)
            {
                distance = choosedMetric(sampleToCheck, sample.attributes, p);
                if (!distances.ContainsKey(sample.decision))
                {
                    distances.Add(sample.decision, new List <double> {
                        distance
                    });
                }
                else
                {
                    distances[sample.decision].Add(distance);
                }
            }
            return(distances);
        }