Exemplo n.º 1
0
        private void HeavyMethod()
        {
            AllRows allColumns = AllRows.GetInstance();
            List <List <RowView> > MinMaxRows = PercentMinMax.DoPercentMinMaxCut(MinInterval, MaxInterval, ColumnNumber);

            for (int i = 0; i < allColumns.FullFile[0].Value.Count; i++)
            {
                Binding            binding = new Binding(String.Format("Value[{0}]", i));
                DataGridTextColumn column  = new DataGridTextColumn();

                //binding.Converter = new PricesConverter();
                binding.Mode = BindingMode.OneWay;
                binding.ValidatesOnDataErrors = true;
                column.Binding     = binding;
                column.CanUserSort = false;
                column.Header      = allColumns.HeaderName[i];
                DataGrid_Min.Columns.Add(column);
            }
            DataGrid_Min.ItemsSource = MinMaxRows[0];
            for (int i = 0; i < allColumns.FullFile[0].Value.Count; i++)
            {
                Binding            binding = new Binding(String.Format("Value[{0}]", i));
                DataGridTextColumn column  = new DataGridTextColumn();

                //binding.Converter = new PricesConverter();
                binding.Mode = BindingMode.OneWay;
                binding.ValidatesOnDataErrors = true;
                column.Binding     = binding;
                column.CanUserSort = false;
                column.Header      = allColumns.HeaderName[i];
                DataGrid_Max.Columns.Add(column);
            }
            DataGrid_Max.ItemsSource = MinMaxRows[1];
        }
        public static List <List <RowView> > DoPercentMinMaxCut(double min, double max, int columnNumber)
        {
            List <RowView> allRows = AllRows.GetInstance().FullFile;

            allRows = allRows.OrderBy(o => Convert.ToDouble(o.Value[columnNumber])).ToList();
            List <RowView>         minResultRows = new List <RowView>();
            List <RowView>         maxResultRows = new List <RowView>();
            List <List <RowView> > resultRows    = new List <List <RowView> >();

            int minInterval = Convert.ToInt32(Math.Round(min / 100 * allRows.Count));
            int maxInterval = allRows.Count - Convert.ToInt32(Math.Round(max / 100 * allRows.Count));

            for (int i = 0; i < minInterval; i++)
            {
                minResultRows.Add(allRows[i]);
            }
            for (int i = maxInterval; i < allRows.Count; i++)
            {
                maxResultRows.Add(allRows[i]);
            }

            resultRows.Add(minResultRows);
            resultRows.Add(maxResultRows);
            return(resultRows);
        }
        private void SaveButton_Quality_Click(object sender, RoutedEventArgs e)
        {
            bool       IsError        = false;
            int        decisionColumn = AllRows.GetInstance().HeaderName.ToList().Count - 1; //jeżeli nie wybrano klasy decyzyjnej program domyślnie wybierze ostatnią kolumne
            MetricName metric         = MetricName.Euklides;
            int        k;
            decimal    quality = 0;

            if (ClassifyColumnCombo_Quality.SelectedItem == null)
            {
                decisionColumn = ClassifyColumnCombo_Quality.SelectedIndex;
            }
            if (MetricCombo_Quality.SelectedItem != null)
            {
                metric = (MetricName)MetricCombo_Quality.SelectedItem;
            }
            k = Convert.ToInt32(NeighboursText_Quality.Text);
            if (k > AllRows.GetInstance().FullFile.Count)
            {
                MessageBox.Show("Liczba sasiadow nie moze byc wieksza od ilosci obiektow w zbiorze");
                IsError = true;
            }

            if (!IsError)
            {
                Classification classificator = new Classification(k, metric, decisionColumn);
                quality = classificator.GetClassificationQuality();
                MessageBox.Show("Jakosc klasyfikatora k-nn dla metryki '" + metric + "' i k=" + k + " wynosi:\nQUALITY=" + quality);
            }
        }
Exemplo n.º 4
0
        public static void OrderTextToNumber(int collumn)
        {
            
            AllRows allRows = AllRows.GetInstance();
            List<TextToNumberHelper> tmpHealerList = new List<TextToNumberHelper>();
            TextToNumberHelper textToNumberHealper = new TextToNumberHelper();
            int counter = 1;
            
            allRows.HeaderName.Insert(collumn + 1, allRows.HeaderName[collumn] + "__num__k");

            for (int i= 0; i<allRows.FullFile.Count(); i++)
            {
                var text = tmpHealerList.SingleOrDefault(s => s.text.Equals(allRows.FullFile[i].Value[collumn]));
                if (text == null)
                {
                    textToNumberHealper = new TextToNumberHelper();
                    textToNumberHealper.text = allRows.FullFile[i].Value[collumn];
                    textToNumberHealper.number = counter;
                    allRows.FullFile[i].Value.Insert(collumn + 1, counter.ToString()) ;
                    counter++;
                    tmpHealerList.Add(textToNumberHealper);
                }
                else
                {
                    allRows.FullFile[i].Value.Insert(collumn + 1, text.number.ToString());
                }
               // if (allRows.FullFile[i].Value[collumn])
            }

        }
Exemplo n.º 5
0
        public static void AlfabeticTextToNumber(int collumn)
        {
            AllRows allRows = AllRows.GetInstance();
            List<TextToNumberHelper> tmpHelperList = new List<TextToNumberHelper>();
            TextToNumberHelper textToNumberHealper = new TextToNumberHelper();

            allRows.HeaderName.Insert(collumn + 1, allRows.HeaderName[collumn] + "__num__a");

            for (int i = 0; i < allRows.FullFile.Count(); i++)
            {
                var text = tmpHelperList.SingleOrDefault(s => s.text.Equals(allRows.FullFile[i].Value[collumn]));
                if (text == null)
                {
                    textToNumberHealper = new TextToNumberHelper();
                    textToNumberHealper.text = allRows.FullFile[i].Value[collumn];
                    tmpHelperList.Add(textToNumberHealper);
                }
            }
            tmpHelperList.Sort(delegate (TextToNumberHelper x, TextToNumberHelper y)
            {
                if (x.text == null && y.text == null) return 0;
                else if (x.text == null) return -1;
                else if (y.text == null) return 1;
                else return x.text.CompareTo(y.text); 
            });
            for (int i = 0; i< tmpHelperList.Count(); i++)
            {
                tmpHelperList[i].number = i + 1;
            }
            for (int i = 0; i < allRows.FullFile.Count(); i++)
            {
                var text = tmpHelperList.SingleOrDefault(s => s.text.Equals(allRows.FullFile[i].Value[collumn]));
                allRows.FullFile[i].Value.Insert(collumn + 1, text.number.ToString());
            }
        }
        private void ClasifyButton_Click(object sender, RoutedEventArgs e)
        {
            ClasifyNewWindow win = new ClasifyNewWindow();

            win.ShowDialog();
            dataGrid.ItemsSource = null;
            dataGrid.ItemsSource = AllRows.GetInstance().FullFile;
        }
Exemplo n.º 7
0
 private void ComboBoxFill()
 {
     ClassifyColumnCombo.ItemsSource = new List <string>();
     ClassifyColumnCombo.Text        = "-- K.Decyzyjna --";
     ClassifyColumnCombo.ItemsSource = AllRows.GetInstance().HeaderName;
     MetricCombo.Text = "-- Metryka --";
     MetricCombo.Items.Add(MetricName.Euklides);
     MetricCombo.Items.Add(MetricName.Manhattan);
     MetricCombo.Items.Add(MetricName.Czebyszew);
     MetricCombo.Items.Add(MetricName.Mahalanobis);
 }
        public void ReadFileFromPath(string path, bool firstRowHeader)
        {
            AllRows.ClearFullFile();
            AllRows allColumns = AllRows.GetInstance();

            char[] delimiters = new[] { ' ', ';', '\t' };
            string line;

            string[]     splitLine;
            bool         firstRow = true;
            StreamReader file     = new StreamReader(path);

            while ((line = file.ReadLine()) != null)
            {
                splitLine = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                if (splitLine.Length > 0)
                {
                    if (splitLine[0].FirstOrDefault() != '#')
                    {
                        if (firstRowHeader && firstRow)
                        {
                            allColumns.HeaderName = new List <string>();
                            for (int i = 0; i < splitLine.Length; i++)
                            {
                                allColumns.HeaderName.Add(splitLine[i]);
                            }
                            firstRow = false;
                        }
                        else if (!firstRowHeader && firstRow)
                        {
                            RowView column = new RowView();
                            allColumns.HeaderName = new List <string>();
                            for (int i = 0; i < splitLine.Length; i++)
                            {
                                column.Value.Add(splitLine[i]);
                                allColumns.HeaderName.Add("Kolumna__" + (i + 1));
                            }
                            allColumns.FullFile.Add(column);
                        }
                        else
                        {
                            RowView column = new RowView();
                            for (int i = 0; i < splitLine.Length; i++)
                            {
                                //List<string> column = new List<string>();
                                column.Value.Add(splitLine[i]);
                            }
                            allColumns.FullFile.Add(column);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void ClassifyButton_Click(object sender, RoutedEventArgs e)
        {
            bool IsError = false;

            //jeżeli nie wybrano klasy decyzyjnej program domyślnie wybierze ostatnią kolumne
            if (ClassifyColumnCombo.SelectedItem == null)
            {
                DecisionColumn = AllRows.GetInstance().HeaderName.ToList().Count - 1;
            }
            else
            {
                DecisionColumn = ClassifyColumnCombo.SelectedIndex;
            }

            if (MetricCombo.SelectedItem == null)
            {
                Metric = MetricName.Euklides;
            }
            else
            {
                Metric = (MetricName)MetricCombo.SelectedItem;
            }
            //MessageBox.Show(Metric.ToString());

            k = Convert.ToInt32(NeighboursText.Text);
            if (k > AllRows.GetInstance().FullFile.Count)
            {
                MessageBox.Show("k nie moze byc wieksze od ilosci obiektow");
                IsError = true;
            }

            for (int i = 0; i < NewObjects[0].Value.Count - 1; i++)
            {
                if (NewObjects[0].Value[i] == "")
                {
                    MessageBox.Show("Uzupelnij wszystkie pola aby kontynuowac");
                    IsError = true;
                    break;
                }
            }

            if (!IsError)
            {
                Classification classificator = new Classification(k, Metric, DecisionColumn);
                string         decision      = classificator.Classify(NewObjects[0]);
                //MessageBox.Show(decision);
                NewObjects[0].Value[NewObjects[0].Value.Count - 1] = decision;
                dataGrid.ItemsSource = null;
                //Thread.Sleep(1000);
                dataGrid.ItemsSource = NewObjects;
            }
        }
        private void DisplayNewDataInMenu(int columnNumber)
        {
            AllRows            allColumns = AllRows.GetInstance();
            Binding            binding    = new Binding(String.Format("Value[{0}]", columnNumber));
            DataGridTextColumn column     = new DataGridTextColumn();

            //binding.Converter = new PricesConverter();
            binding.Mode = BindingMode.TwoWay;
            binding.ValidatesOnDataErrors = true;
            column.Binding     = binding;
            column.CanUserSort = false;
            column.Header      = allColumns.HeaderName[columnNumber];
            dataGrid.Columns.Insert(columnNumber, column);
            ComboBoxFill();
        }
        public static void DoScalingIntervals(int numOfColumn, decimal newMin, decimal newMax)
        {
            double[] minmax = MathHelper.FindMinMax(AllRows.GetInstance().FullFile, numOfColumn);
            decimal  k      = newMin / Convert.ToDecimal(minmax[0]); //skala rzutowania przedziałów

            //rzutowanie przedziału na nowy przez dodanie nowej kolumny
            AllRows.GetInstance().HeaderName.Insert(numOfColumn + 1, AllRows.GetInstance().HeaderName[numOfColumn] + "__skal");
            string  cell  = "";
            decimal value = 0;

            for (int i = 0; i < AllRows.GetInstance().FullFile.Count; i++)
            {
                value = Convert.ToDecimal(AllRows.GetInstance().FullFile[i].Value[numOfColumn]);
                cell  = (value * k).ToString();
                AllRows.GetInstance().FullFile[i].Value.Insert(numOfColumn + 1, cell);
            }
        }
        // Jeszcze trzeba dokończyć albo inaczej zacząć na dobre
        public static void WriteToFile(string fileName, bool firstRowAsName)
        {
            AllRows allColumns = AllRows.GetInstance();

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            StreamWriter sw   = new StreamWriter(fileName);
            string       line = "";

            if (firstRowAsName)
            {
                foreach (string header in allColumns.HeaderName)
                {
                    if (line == "")
                    {
                        line = header;
                    }
                    else
                    {
                        line = line + " ; " + header;
                    }
                }
                sw.WriteLine(line);
                line = "";
            }
            foreach (RowView rowView in allColumns.FullFile)
            {
                foreach (string str in rowView.Value)
                {
                    if (line == "")
                    {
                        line = str;
                    }
                    else
                    {
                        line = line + " ; " + str;
                    }
                }
                sw.WriteLine(line);
                line = "";
            }
            sw.Close();
        }
 private void ComboBoxFill()
 {
     ColumnCombo_Numeric.ItemsSource   = new List <string>();
     ColumnCombo_Numeric.Text          = "-- Kolumna --";
     ColumnCombo_Numeric.ItemsSource   = AllRows.GetInstance().HeaderName;
     ColumnCombo_Discret.ItemsSource   = new List <string>();
     ColumnCombo_Discret.Text          = "-- Kolumna --";
     ColumnCombo_Discret.ItemsSource   = AllRows.GetInstance().HeaderName;
     ColumnCombo_Normal.ItemsSource    = new List <string>();
     ColumnCombo_Normal.Text           = "-- Kolumna --";
     ColumnCombo_Normal.ItemsSource    = AllRows.GetInstance().HeaderName;
     ColumnCombo_Intervals.ItemsSource = new List <string>();
     ColumnCombo_Intervals.Text        = "-- Kolumna --";
     ColumnCombo_Intervals.ItemsSource = AllRows.GetInstance().HeaderName;
     ColumnCombo_Percent.ItemsSource   = new List <string>();
     ColumnCombo_Percent.Text          = "-- Kolumna --";
     ColumnCombo_Percent.ItemsSource   = AllRows.GetInstance().HeaderName;
     Column1Combo_2D.ItemsSource       = new List <string>();
     Column1Combo_2D.Text                    = "-- Kolumna --";
     Column1Combo_2D.ItemsSource             = AllRows.GetInstance().HeaderName;
     Column2Combo_2D.ItemsSource             = new List <string>();
     Column2Combo_2D.Text                    = "-- Kolumna --";
     Column2Combo_2D.ItemsSource             = AllRows.GetInstance().HeaderName;
     Column1Combo_3D.ItemsSource             = new List <string>();
     Column1Combo_3D.Text                    = "-- Kolumna --";
     Column1Combo_3D.ItemsSource             = AllRows.GetInstance().HeaderName;
     Column2Combo_3D.ItemsSource             = new List <string>();
     Column2Combo_3D.Text                    = "-- Kolumna --";
     Column2Combo_3D.ItemsSource             = AllRows.GetInstance().HeaderName;
     Column3Combo_3D.ItemsSource             = new List <string>();
     Column3Combo_3D.Text                    = "-- Kolumna --";
     Column3Combo_3D.ItemsSource             = AllRows.GetInstance().HeaderName;
     ColumnCombo_Histogram.ItemsSource       = new List <string>();
     ColumnCombo_Histogram.Text              = "-- Kolumna --";
     ColumnCombo_Histogram.ItemsSource       = AllRows.GetInstance().HeaderName;
     Column3Combo_2D.ItemsSource             = new List <string>();
     Column3Combo_2D.Text                    = "-- K.Decyzyjna --";
     Column3Combo_2D.ItemsSource             = AllRows.GetInstance().HeaderName;
     ClassifyColumnCombo_Quality.Text        = "-- K.Decyzyjna --";
     ClassifyColumnCombo_Quality.ItemsSource = AllRows.GetInstance().HeaderName;
 }
Exemplo n.º 14
0
        public static void DoNormalization(int numOfColumn)
        {
            //dyskretyzacja przez dodanie nowej kolumny
            AllRows.GetInstance().HeaderName.Insert(numOfColumn + 1, AllRows.GetInstance().HeaderName[numOfColumn] + "__norm");
            string  cell      = "";
            decimal avg       = MathHelper.CalcAverage(AllRows.GetInstance().FullFile, numOfColumn);
            decimal deviation = MathHelper.CalcStandardDeviation(AllRows.GetInstance().FullFile, numOfColumn);
            decimal result    = 0;
            decimal value     = 0;
            decimal testAvg   = 0; //zmienna do testowania wyniku - dla danych poprawnie znormalizowanych średnia=0

            for (int i = 0; i < AllRows.GetInstance().FullFile.Count; i++)
            {
                value    = Convert.ToDecimal(AllRows.GetInstance().FullFile[i].Value[numOfColumn]);
                result   = (value - avg) / deviation;
                testAvg += result;
                cell     = result.ToString();
                AllRows.GetInstance().FullFile[i].Value.Insert(numOfColumn + 1, cell);
            }
            testAvg /= AllRows.GetInstance().FullFile.Count;
        }
Exemplo n.º 15
0
        public void HeavyMethod()
        {
            AllRows allColumns = AllRows.GetInstance();
            RowView row        = new RowView();

            for (int i = 0; i < allColumns.FullFile[0].Value.Count; i++)
            {
                Binding            binding = new Binding(String.Format("Value[{0}]", i));
                DataGridTextColumn column  = new DataGridTextColumn();
                binding.Mode = BindingMode.TwoWay;
                binding.ValidatesOnDataErrors = true;
                column.Binding     = binding;
                column.CanUserSort = false;
                column.Header      = allColumns.HeaderName[i];
                dataGrid.Columns.Add(column);
                row.Value.Add("");
            }
            NewObjects.Add(row);
            dataGrid.ItemsSource = NewObjects;

            //dataGrid.ItemsSource = allColumns.FullFile;
        }
        private async Task HeavyMethod(string filePath)
        {
            FileReadWrite fileReadWrite = new FileReadWrite();

            fileReadWrite.ReadFileFromPath(filePath, firstRowHaveHeader);
            AllRows allColumns = AllRows.GetInstance();

            for (int i = 0; i < allColumns.FullFile[0].Value.Count; i++)
            {
                Binding            binding = new Binding(String.Format("Value[{0}]", i));
                DataGridTextColumn column  = new DataGridTextColumn();
                //binding.Converter = new PricesConverter();
                binding.Mode = BindingMode.TwoWay;
                binding.ValidatesOnDataErrors = true;
                column.Binding     = binding;
                column.CanUserSort = false;
                column.Header      = allColumns.HeaderName[i];
                dataGrid.Columns.Add(column);
            }
            dataGrid.ItemsSource = allColumns.FullFile;
            ComboBoxFill();
            await Task.Delay(50);
        }
Exemplo n.º 17
0
        public static void DoDiscretization(int numOfColumn, int countOfIntervals)
        {
            //tworzenie listy przedziałów
            List <double> intervals = new List <double>();

            double[] minmax       = MathHelper.FindMinMax(AllRows.GetInstance().FullFile, numOfColumn);
            double   intervalSize = (minmax[1] - minmax[0]) / countOfIntervals;
            double   leftLimit    = minmax[0];

            while (leftLimit < minmax[1])
            {
                intervals.Add(leftLimit);
                leftLimit += intervalSize;
            }
            //dyskretyzacja przez dodanie nowej kolumny
            AllRows.GetInstance().HeaderName.Insert(numOfColumn + 1, AllRows.GetInstance().HeaderName[numOfColumn] + "__dyskret");
            string cell = "";

            for (int i = 0; i < AllRows.GetInstance().FullFile.Count; i++)
            {
                for (int j = 0; j < intervals.Count; j++)
                {
                    if (j == intervals.Count - 1)
                    {
                        cell = (j + 1).ToString();
                        AllRows.GetInstance().FullFile[i].Value.Insert(numOfColumn + 1, cell);
                        break;
                    }
                    else if (Convert.ToDouble(AllRows.GetInstance().FullFile[i].Value[numOfColumn]) < intervals[j + 1])
                    {
                        cell = (j + 1).ToString();
                        AllRows.GetInstance().FullFile[i].Value.Insert(numOfColumn + 1, cell);
                        break;
                    }
                }
            }
        }
        public decimal GetClassificationQuality()
        {
            List <bool> results = new List <bool>();
            string      classifyResult;
            decimal     correctCount;
            decimal     quality;

            foreach (RowView row in AllRows.GetInstance().FullFile)
            {
                classifyResult = Classify(row);
                if (classifyResult == row.Value[DecisionColNumber])
                {
                    results.Add(true);
                }
                else
                {
                    results.Add(false);
                }
            }

            correctCount = results.Where(x => x == true).ToList().Count();
            quality      = correctCount / results.Count();
            return(quality);
        }
Exemplo n.º 19
0
        public HistogramWindow(int columnNumber, int countOfIntervals)
        {
            InitializeComponent();

            AllRows allRows     = AllRows.GetInstance();
            bool    rowIsString = false;

            try
            {
                double tmp = Convert.ToDouble(allRows.FullFile[0].Value[columnNumber]);
            }
            catch
            {
                rowIsString = true;
            }

            if (rowIsString)
            {
                List <string>        allString = new List <string>();
                ChartValues <double> rowCount  = new ChartValues <double>();
                for (int i = 0; i < allRows.FullFile.Count(); i++)
                {
                    var text = allString.SingleOrDefault(s => s.Equals(allRows.FullFile[i].Value[columnNumber]));
                    if (text == null)
                    {
                        var counter = allRows.FullFile.Where(s => s.Value[columnNumber] == allRows.FullFile[i].Value[columnNumber]).Count();
                        allString.Add(allRows.FullFile[i].Value[columnNumber]);
                        rowCount.Add(counter);
                    }
                }

                string[] converter = allString.ToArray();

                SeriesCollection = new SeriesCollection
                {
                    new ColumnSeries
                    {
                        Values = rowCount
                    }
                };

                Labels    = converter;
                Formatter = value => value.ToString("N");
            }
            else
            {
                List <double>        allValue = new List <double>();
                ChartValues <double> rowCount = new ChartValues <double>();

                List <double> intervals    = new List <double>();
                double[]      minmax       = MathHelper.FindMinMax(AllRows.GetInstance().FullFile, columnNumber);
                double        intervalSize = (minmax[1] - minmax[0]) / countOfIntervals;
                double        leftLimit    = minmax[0];
                while (leftLimit < minmax[1])
                {
                    intervals.Add(leftLimit);
                    leftLimit += intervalSize;
                }
                double cell;
                for (int i = 0; i < AllRows.GetInstance().FullFile.Count; i++)
                {
                    for (int j = 0; j < intervals.Count; j++)
                    {
                        if (j == intervals.Count - 1)
                        {
                            cell = j + 1;
                            allValue.Add(cell);
                            break;
                        }
                        else if (Convert.ToDouble(AllRows.GetInstance().FullFile[i].Value[columnNumber]) < intervals[j + 1])
                        {
                            cell = j + 1;
                            allValue.Add(cell);
                            break;
                        }
                    }
                }

                string[] label = new string[intervals.Count()];

                for (int i = 0; i < intervals.Count(); i++)
                {
                    if (i == intervals.Count() - 1)
                    {
                        label[i] = "<" + intervals[i].ToString() + " : " + minmax[1].ToString() + ">";
                    }
                    else
                    {
                        label[i] = "<" + intervals[i].ToString() + " : " + intervals[i + 1].ToString() + ")";
                    }
                    var counter = allValue.Where(c => c == i + 1).Count();
                    rowCount.Add(counter);
                }

                SeriesCollection = new SeriesCollection
                {
                    new ColumnSeries
                    {
                        Values = rowCount
                    }
                };


                Labels    = label;
                Formatter = value => value.ToString("N");
            }


            ////adding series will update and animate the chart automatically
            //SeriesCollection.Add(new ColumnSeries
            //{
            //    Title = "2016",
            //    Values = new ChartValues<double> { 11, 56, 42 }
            //});

            ////also adding values updates and animates the chart automatically
            //SeriesCollection[1].Values.Add(48d);



            DataContext = this;
        }
Exemplo n.º 20
0
 private void SaveButton_Click(object sender, RoutedEventArgs e)
 {
     AllRows.GetInstance().FullFile.Add(NewObjects[0]);
     this.Close();
 }
        public string Classify(RowView newObject)
        {
            NewObject = newObject;
            Dictionary <int, double> results = new Dictionary <int, double>();
            //List<double> results = new List<double>();
            List <RowView>           neighbours    = new List <RowView>();
            Dictionary <int, double> sortedResults = new Dictionary <int, double>();
            //List<double> sortedResults = new List<double>();
            Dictionary <string, int> decisionsCounter = new Dictionary <string, int>();
            int    objectID;
            string decisionValue;
            double minDistance   = 0;
            string minResult     = "";
            string finalDecision = "";

            //wybór metryki do zliczenia odleglosci miedzy wszystkimi obiektami
            switch (Metric)
            {
            case MetricName.Euklides:
                results = ClasificationMetrics.Euklides(NewObject, AllRows.GetInstance().FullFile);
                break;

            case MetricName.Manhattan:
                results = ClasificationMetrics.Manhatan(NewObject, AllRows.GetInstance().FullFile);
                break;

            case MetricName.Czebyszew:
                results = ClasificationMetrics.Czebyszew(NewObject, AllRows.GetInstance().FullFile);
                break;

            case MetricName.Mahalanobis:
                results = ClasificationMetrics.Mahalanobis(NewObject, AllRows.GetInstance().FullFile);
                break;

            default:
                results = ClasificationMetrics.Euklides(NewObject, AllRows.GetInstance().FullFile);
                break;
            }

            //wybor k najblizszych sasiadow
            sortedResults = results.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            for (int k = 0; k < NeighboursCount; k++)
            {
                objectID = sortedResults.ElementAt(k).Key;
                neighbours.Add(AllRows.GetInstance().FullFile[objectID]);
                if (sortedResults.ElementAt(k).Value < minDistance || k == 0)
                {
                    minDistance = sortedResults.ElementAt(k).Value;
                    minResult   = neighbours[k].Value[DecisionColNumber];
                }
            }

            //zliczanie ktora decyzja wsrod sasiadow dominuje
            int temp;

            for (int i = 0; i < neighbours.Count; i++)
            {
                decisionValue = neighbours[i].Value[DecisionColNumber];
                if (decisionsCounter.Where(x => x.Key == decisionValue).ToList().Count == 0)
                {
                    temp = neighbours.Where(x => x.Value[DecisionColNumber] == decisionValue).ToList().Count;
                    decisionsCounter.Add(decisionValue, temp);
                }
            }
            decisionsCounter = decisionsCounter.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            int distance = decisionsCounter.ElementAt(0).Value;

            if (decisionsCounter.Where(x => x.Value == distance).ToList().Count > 1)
            {
                finalDecision = minResult;
            }
            else
            {
                finalDecision = decisionsCounter.ElementAt(0).Key;
            }

            return(finalDecision);
        }
Exemplo n.º 22
0
        public LineChartWindow(int axisX, int axisY, int decisionClass)
        {
            InitializeComponent();
            AllRows allRows = AllRows.GetInstance();

            if (decisionClass == -1)
            {
                ChartValues <ObservablePoint> ValuesA = new ChartValues <ObservablePoint>();
                ScatterSeries scatter1 = new ScatterSeries();
                scatter1.Title = "No class";

                Wykres.Series.Add(scatter1);

                for (int i = 0; i < allRows.FullFile.Count; i++)
                {
                    ValuesA.Add(new ObservablePoint(Convert.ToDouble(allRows.FullFile[i].Value[axisX]), Convert.ToDouble(allRows.FullFile[i].Value[axisY])));
                }
                scatter1.Values = ValuesA;
            }
            else
            {
                List <string> classDecisionList = new List <string>();
                List <ChartValues <ObservablePoint> > observablePoints = new List <ChartValues <ObservablePoint> >();
                List <ScatterSeries> scatterPoints = new List <ScatterSeries>();
                for (int i = 0; i < allRows.FullFile.Count(); i++)
                {
                    var text = classDecisionList.SingleOrDefault(s => s.Equals(allRows.FullFile[i].Value[decisionClass]));
                    if (text == null)
                    {
                        classDecisionList.Add(allRows.FullFile[i].Value[decisionClass]);
                        ScatterSeries sc = new ScatterSeries();
                        sc.Title = allRows.FullFile[i].Value[decisionClass];
                        scatterPoints.Add(sc);
                        ChartValues <ObservablePoint> ob = new ChartValues <ObservablePoint>();
                        observablePoints.Add(ob);
                        ob.Add(new ObservablePoint(Convert.ToDouble(allRows.FullFile[i].Value[axisX]), Convert.ToDouble(allRows.FullFile[i].Value[axisY])));
                    }
                    else
                    {
                        var textToSearch = allRows.FullFile[i].Value[decisionClass];
                        int index        = classDecisionList.FindIndex(c => c == textToSearch);
                        observablePoints[index].Add(new ObservablePoint(Convert.ToDouble(allRows.FullFile[i].Value[axisX]), Convert.ToDouble(allRows.FullFile[i].Value[axisY])));
                    }
                }

                for (int i = 0; i < classDecisionList.Count(); i++)
                {
                    Wykres.Series.Add(scatterPoints[i]);
                    scatterPoints[i].Values = observablePoints[i];
                }
            }



            /*
             * AllRows allRows = AllRows.GetInstance();
             * ChartValues<double> chartY = new ChartValues<double>();
             * AxisXName = allRows.HeaderName[axisX];
             * AxisYName = allRows.HeaderName[axisY];
             * string[] label = new string[allRows.FullFile.Count];
             * for(int i=0; i< allRows.FullFile.Count; i++)
             * {
             *  chartY.Add(Convert.ToDouble(allRows.FullFile[i].Value[axisY]));
             *  label[i] = allRows.FullFile[i].Value[axisX];
             * }
             * SeriesCollection = new SeriesCollection
             * {
             *  new LineSeries
             *  {
             *      Title = AxisXName + " " + AxisYName,
             *      Values = chartY
             *  }
             * };
             * Labels = label;
             * YFormatter = value => value.ToString("C");
             */
            DataContext = this;
        }