Esempio n. 1
0
        public void NaiveBayesIrisTest()
        {
            DataSetLoader dataSetLoader = new DataSetLoader();

            Console.WriteLine(" Reading DataSet.. ");
            var irisis = dataSetLoader.SelectIrises();

            for (double i = 0; i < 1; i = i + 1)
            {
                NaiveBayesClassifier naiveBayes =
                    new NaiveBayesClassifier(irisis, new LinearBayesKernel(irisis));
                var irisesTests = dataSetLoader.SelectIrises();
                var trueCounter = 0;
                var counter     = 0;
                foreach (var item in irisesTests)
                {
                    var outputValue = naiveBayes.Classify(item.Item1);
                    if (outputValue == item.Item2)
                    {
                        trueCounter++;
                    }
                    Debug.WriteLine(string.Format("Value {0} - Predicted {1} = {2}",
                                                  item.Item2, outputValue, (outputValue == item.Item2) ? "true" : "false"));
                    counter++;
                }
                Debug.WriteLine(string.Format(" i = {0} Data {1} - True {2} Verhältnis: {3}", i,
                                              counter.ToString(), trueCounter.ToString(), (Convert.ToDouble(trueCounter) / Convert.ToDouble(counter)).ToString()));
            }
        }
Esempio n. 2
0
        public void CreditDataNaiveBayesTest()
        {
            DataSetLoader dataSetLoader = new DataSetLoader();

            Console.WriteLine(" Reading DataSet.. ");
            var creditData = dataSetLoader.SelectCreditData();
            NaiveBayesClassifier naiveBayes =
                new NaiveBayesClassifier(creditData);
            var creditDataTest = dataSetLoader.SelectCreditData();
            var trueCounter    = 0;
            var counter        = 0;

            foreach (var item in creditDataTest)
            {
                var outputValue = naiveBayes.Classify(item.Item1);
                if (outputValue == item.Item2)
                {
                    trueCounter++;
                }
                Debug.WriteLine(string.Format("Value {0} - Predicted {1} = {2}",
                                              item.Item2, outputValue, (outputValue == item.Item2) ? "true" : "false"));
                counter++;
            }
            Debug.WriteLine(string.Format("Data {0} - True {1} Verhältnis: {2}",
                                          counter.ToString(), trueCounter.ToString(), (Convert.ToDouble(trueCounter) / Convert.ToDouble(counter)).ToString()));
        }
Esempio n. 3
0
        public IEnumerable <Tuple <LanguageInfo, double> > Identify(string text)
        {
            var extractor       = new CharacterNGramExtractor(MaxNGramLength, OnlyReadFirstNLines);
            var tokens          = extractor.GetFeatures(text);
            var likelyLanguages = _classifier.Classify(tokens);

            return(likelyLanguages);
        }
Esempio n. 4
0
        public void ClassifyTest()
        {
            var classifier = new NaiveBayesClassifier();

            var trainingData = new List <Tuple <string, IEnumerable <string> > >
            {
                new Tuple <string, IEnumerable <string> >("Food", new[] { "apple", "orange" }),
                new Tuple <string, IEnumerable <string> >("Food", new[] { "apple", "cake", "banana" }),

                new Tuple <string, IEnumerable <string> >("Animal", new[] { "cat", "dog" }),
                new Tuple <string, IEnumerable <string> >("Animal", new[] { "bird", "cat", "apple" }),
            };

            classifier.Train(trainingData);

            var category = classifier.Classify(new[] { "dog" });

            Assert.AreEqual("Animal", category);

            category = classifier.Classify(new[] { "apple", "banana" });
            Assert.AreEqual("Food", category);
        }
Esempio n. 5
0
        public void NaiveBayesGaussianTest()
        {
            Tuple <double[], double>         tuple1 = new Tuple <double[], double>(new double[] { 5.0, 1.0, 1.0 }, 0);
            Tuple <double[], double>         tuple2 = new Tuple <double[], double>(new double[] { 1.0, 5.0, 1.0 }, 1);
            Tuple <double[], double>         tuple3 = new Tuple <double[], double>(new double[] { 1.0, 1.0, 5.0 }, 2);
            List <Tuple <double[], double> > movies =
                new List <Tuple <double[], double> >()
            {
                tuple1, tuple2, tuple3
            };
            NaiveBayesClassifier naiveBayes =
                new NaiveBayesClassifier(movies, new GaussianBayesKernel(movies));
            var result = naiveBayes.Classify(new double[] { 5.0, 1.0, 1.0 });

            result = naiveBayes.Classify(new double[] { 1.0, 5.0, 1.0 });
            result = naiveBayes.Classify(new double[] { 1.0, 1.0, 5.0 });
            result = naiveBayes.Classify(new double[] { 0.5, 9.0, 0.1 });
            result = naiveBayes.Classify(new double[] { 0.4, 0.2, 2.1 });
            result = naiveBayes.Classify(new double[] { 0.3, 0.3, 2.5 });
        }