Esempio n. 1
0
        private void Button_Click_Start_Clasification(object sender, RoutedEventArgs e)
        {
            int assignedProperly    = 0;
            int assingedNotProperly = 0;

            Utils.DistributeArticles(ChosenSet, ref TrainingSet, ref TestingSet, int.Parse(trainingDataPercentageInput.Text));
            for (int j = 0; j < int.Parse(countOfLoopsInput.Text); ++j)
            {
                Dictionary <string, List <Article> > labelArticlesMap = new Dictionary <string, List <Article> >();
                SetExtractor();
                PrepareLabelArticleMap(ref labelArticlesMap);

                //Taking some Articles from TrainingSet to resolve problem of cold start
                List <Article> coldStart = new List <Article>();
                foreach (var pair in labelArticlesMap)
                {
                    int amountToTake = TrainingSet.Count / labelArticlesMap.Count / 10;
                    coldStart.AddRange(pair.Value.Take(amountToTake > pair.Value.Count ? pair.Value.Count : amountToTake).ToList());
                }

                //Creating mapping from coldstart Articles to characteristics values, assigning actual labels
                Dictionary <Article, List <double> > ColdStart = new Dictionary <Article, List <double> >();
                foreach (var item in coldStart)
                {
                    ColdStart.Add(item, characteristicExtractor.GetWeights(item));
                    item.AssignedLabel = item.ActualLabel;
                }

                //Computing characteristics for first run of KNN - resolving problem of cold start
                Dictionary <Article, List <double> > KnnMap = new Dictionary <Article, List <double> >();
                foreach (var item in TestingSet)
                {
                    KnnMap.Add(item, characteristicExtractor.GetWeights(item));
                }
                KNN.ColdStart(ref KnnMap, ref ColdStart, int.Parse(kInput.Text), AssignMetric());

                //Actual KNN algorithm
                KNN.Testing(ref KnnMap, int.Parse(kInput.Text), Metrics.EuclideanMetricDistance);

                assignedProperly    += KnnMap.Count(i => i.Key.AssignedLabel == i.Key.ActualLabel);
                assingedNotProperly += KnnMap.Count(i => i.Key.AssignedLabel != i.Key.ActualLabel);
            }
            UpdateStatus("Klasyfikacja zakończona");
            succededDisplay.Text   = assignedProperly.ToString();
            failedDisplay.Text     = assingedNotProperly.ToString();
            accuracityDisplay.Text = (100.0 * assignedProperly / (assignedProperly + assingedNotProperly)).ToString() + "%";
        }