Exemple #1
0
        private void DoBayesAnalyze()
        {
            Codification codebook = new Codification(specialValueColumnNames, data);

            int[][] symbols = codebook.Transform(data);
            int[][] inputs  = symbols.Get(null, 0, -1);
            int[]   outputs = symbols.GetColumn(-1);

            // Create a new Naive Bayes learning
            var        learner       = new NaiveBayesLearning();
            NaiveBayes nb            = learner.Learn(inputs, outputs);
            var        currentSprint = App.GetReleaseScrumData().CurrentSprintProxy.CurrentSprint;

            foreach (var story in currentSprint.Stories)
            {
                if (excludeOwners.Contains(story.Owner))
                {
                    story.PredicateSuccessRate = noResultReasonExcludeUser;
                    continue;
                }
                if (story.Size <= 0)
                {
                    story.PredicateSuccessRate = noResultReasonStorySize0;
                    continue;
                }

                int[]    storyInstance = codebook.Transform(new string[] { story.Size.ToString(), story.Owner });
                double[] probs         = nb.Probabilities(storyInstance);
                var      sucessRate    = probs[0] * 100;
                var      pSuccessRate  = string.Format("{0:N2}", sucessRate);
                story.PredicateSuccessRate = string.Format("{0}%", pSuccessRate);
            }
        }
Exemple #2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (nb == null)
            {
                return;
            }

            double height = double.Parse(tbHeight.Text);
            double weight = double.Parse(tbWeight.Text);
            double foot   = double.Parse(tbFootSize.Text);

            double[] input  = { height, weight, foot };
            double[] result = nb.Probabilities(input);

            tbMale.Text   = result[0].ToString("0.00");
            tbFemale.Text = result[1].ToString("0.00");

            if (result[0] > result[1])
            {
                tbResult.Text = "남자";
            }
            else
            {
                tbResult.Text = "여자";
            }
        }
        public void Predict(params string[] args)
        {
            int[] instance;
            try
            {
                instance = codeBook.Transform(args);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                return;
            }

            int    c      = nativeBayes.Decide(instance);
            string result = codeBook.Revert(headerToPredict, c);

            System.Console.WriteLine(result);

            double[] probs = nativeBayes.Probabilities(instance);

            foreach (var item in probs)
            {
                System.Console.WriteLine(item);
            }
        }
        public void learn_no_datatable()
        {
            #region doc_mitchell_no_datatable
            string[] columnNames = { "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis" };

            string[][] data =
            {
                new string[] { "Sunny",    "Hot",  "High",   "Weak",   "No"  },
                new string[] { "Sunny",    "Hot",  "High",   "Strong", "No"  },
                new string[] { "Overcast", "Hot",  "High",   "Weak",   "Yes" },
                new string[] { "Rain",     "Mild", "High",   "Weak",   "Yes" },
                new string[] { "Rain",     "Cool", "Normal", "Weak",   "Yes" },
                new string[] { "Rain",     "Cool", "Normal", "Strong", "No"  },
                new string[] { "Overcast", "Cool", "Normal", "Strong", "Yes" },
                new string[] { "Sunny",    "Mild", "High",   "Weak",   "No"  },
                new string[] { "Sunny",    "Cool", "Normal", "Weak",   "Yes" },
                new string[] { "Rain",     "Mild", "Normal", "Weak",   "Yes" },
                new string[] { "Sunny",    "Mild", "Normal", "Strong", "Yes" },
                new string[] { "Overcast", "Mild", "High",   "Strong", "Yes" },
                new string[] { "Overcast", "Hot",  "Normal", "Weak",   "Yes" },
                new string[] { "Rain",     "Mild", "High",   "Strong", "No"  },
            };

            // Create a new codification codebook to
            // convert strings into discrete symbols
            Codification codebook = new Codification(columnNames, data);

            // Extract input and output pairs to train
            int[][] symbols = codebook.Transform(data);
            int[][] inputs  = symbols.Get(null, 0, -1); // Gets all rows, from 0 to the last (but not the last)
            int[]   outputs = symbols.GetColumn(-1);    // Gets only the last column

            // Create a new Naive Bayes learning
            var learner = new NaiveBayesLearning();

            NaiveBayes nb = learner.Learn(inputs, outputs);

            // Consider we would like to know whether one should play tennis at a
            // sunny, cool, humid and windy day. Let us first encode this instance
            int[] instance = codebook.Translate("Sunny", "Cool", "High", "Strong");

            // Let us obtain the numeric output that represents the answer
            int c = nb.Decide(instance); // answer will be 0

            // Now let us convert the numeric output to an actual "Yes" or "No" answer
            string result = codebook.Translate("PlayTennis", c); // answer will be "No"

            // We can also extract the probabilities for each possible answer
            double[] probs = nb.Probabilities(instance); // { 0.795, 0.205 }
            #endregion

            Assert.AreEqual("No", result);
            Assert.AreEqual(0, c);
            Assert.AreEqual(0.795, probs[0], 1e-3);
            Assert.AreEqual(0.205, probs[1], 1e-3);
            Assert.AreEqual(1, probs.Sum(), 1e-10);
            Assert.IsFalse(double.IsNaN(probs[0]));
            Assert.AreEqual(2, probs.Length);
        }
Exemple #5
0
        public SentimentResult Predict(string sentiment)
        {
            var result     = new SentimentResult();
            var tokenized  = _preprocessor.Process(sentiment);
            var featurized = _bagOfWords.Transform(tokenized).ToInt32();

            var scores = _bayes.Scores(featurized);
            var prob   = _bayes.Probabilities(featurized);

            result.Polarity            = _bayes.Decide(featurized) == 0 ? Polarity.Negative : Polarity.Positive;
            result.NegativeScore       = scores[0];
            result.PositiveScore       = scores[1];
            result.NegativeProbability = prob[0];
            result.PositiveProbability = prob[1];

            return(result);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            DataTable data = new DataTable("Categories of words");

            data.Columns.Add("Category", "Word");
            List <InputData> words = ExcelDataProvider.GetData(@"C:\Users\Дарья\Desktop\AdverbNoun.xlsx", 0);

            foreach (var word in words)
            {
                data.Rows.Add(word.Category, word.Word);
            }

            Codification codebook = new Codification(data, "Category", "Word");

            DataTable symbols = codebook.Apply(data);

            int[][] inputs  = symbols.ToJagged <int>("Category");
            int[]   outputs = symbols.ToArray <int>("Word");

            var        learner = new NaiveBayesLearning();
            NaiveBayes nb      = learner.Learn(inputs, outputs);

            data = new DataTable("Categories of words");
            data.Columns.Add("Category", "Word");
            words = ExcelDataProvider.GetData(@"C:\Users\Дарья\Desktop\TestAdverbNoun.xlsx", 0);

            foreach (var word in words)
            {
                data.Rows.Add(word.Category, word.Word);
            }

            int[] instance = codebook.Translate("helpful");

            int c = nb.Decide(instance);

            string result = codebook.Translate("Category", c);

            double[] probs = nb.Probabilities(instance);

            Console.WriteLine(0);
        }
Exemple #7
0
        private static void test(NaiveBayes <GeneralDiscreteDistribution, int> nb, int[][] inputs, int[] sp)
        {
            int c = nb.Decide(sp);             // 1

            double[] p = nb.Probabilities(sp); // 0.015, 0.985

            // Evaluation of all points
            int[] actual = nb.Decide(inputs);

            Assert.AreEqual(1, c);
            Assert.AreEqual(0.015197568389057824, p[0], 1e-10);
            Assert.AreEqual(0.98480243161094227, p[1], 1e-10);

            Assert.AreEqual(nb.Distributions[0].Components[0].Frequencies[0], 0.46153846153846156);
            Assert.AreEqual(nb.Distributions[0].Components[1].Frequencies[0], 0.23076923076923078);
            Assert.AreEqual(nb.Distributions[0].Components[2].Frequencies[0], 0.15384615384615385);
            Assert.AreEqual(nb.Distributions[0].Components[3].Frequencies[0], 0.38461538461538464);
            Assert.AreEqual(nb.Distributions[0].Components[4].Frequencies[0], 0.23076923076923078);
            Assert.AreEqual(nb.Distributions[0].Components[5].Frequencies[0], 0.92307692307692313);
            Assert.AreEqual(nb.Distributions[0].Components[6].Frequencies[0], 0.92307692307692313);
            Assert.AreEqual(nb.Distributions[0].Components[7].Frequencies[0], 0.53846153846153844);

            Assert.AreEqual(nb.Distributions[1].Components[0].Frequencies[0], 0.46153846153846156);
            Assert.AreEqual(nb.Distributions[1].Components[1].Frequencies[0], 0.23076923076923078);
            Assert.AreEqual(nb.Distributions[1].Components[2].Frequencies[0], 0.61538461538461542);
            Assert.AreEqual(nb.Distributions[1].Components[3].Frequencies[0], 0.38461538461538464);
            Assert.AreEqual(nb.Distributions[1].Components[4].Frequencies[0], 0.92307692307692313);
            Assert.AreEqual(nb.Distributions[1].Components[5].Frequencies[0], 0.30769230769230771);
            Assert.AreEqual(nb.Distributions[1].Components[6].Frequencies[0], 0.30769230769230771);
            Assert.AreEqual(nb.Distributions[1].Components[7].Frequencies[0], 0.076923076923076927);

            int[] last   = actual.Get(new[] { 11, 12 }.Concatenate(Vector.Range(14, 22)));
            int[] others = actual.Get(Vector.Range(0, 10).Concatenate(13));
            Assert.IsTrue(1.IsEqual(last));
            Assert.IsTrue(0.IsEqual(others));
        }
Exemple #8
0
        public void ComputeTest()
        {
            #region doc_mitchell
            DataTable data = new DataTable("Mitchell's Tennis Example");

            data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");

            data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
            data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
            data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
            data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
            data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
            data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
            data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
            data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
            data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
            data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
            data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");
            #endregion

            #region doc_codebook
            // Create a new codification codebook to
            // convert strings into discrete symbols
            Codification codebook = new Codification(data,
                                                     "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");

            // Extract input and output pairs to train
            DataTable symbols = codebook.Apply(data);
            int[][]   inputs  = symbols.ToArray <int>("Outlook", "Temperature", "Humidity", "Wind");
            int[]     outputs = symbols.ToArray <int>("PlayTennis");
            #endregion

            #region doc_learn
            // Create a new Naive Bayes learning
            var learner = new NaiveBayesLearning();

            // Learn a Naive Bayes model from the examples
            NaiveBayes nb = learner.Learn(inputs, outputs);
            #endregion


            #region doc_test
            // Consider we would like to know whether one should play tennis at a
            // sunny, cool, humid and windy day. Let us first encode this instance
            int[] instance = codebook.Translate("Sunny", "Cool", "High", "Strong");

            // Let us obtain the numeric output that represents the answer
            int c = nb.Decide(instance); // answer will be 0

            // Now let us convert the numeric output to an actual "Yes" or "No" answer
            string result = codebook.Translate("PlayTennis", c); // answer will be "No"

            // We can also extract the probabilities for each possible answer
            double[] probs = nb.Probabilities(instance); // { 0.795, 0.205 }
            #endregion

            Assert.AreEqual("No", result);
            Assert.AreEqual(0, c);
            Assert.AreEqual(0.795, probs[0], 1e-3);
            Assert.AreEqual(0.205, probs[1], 1e-3);
            Assert.AreEqual(1, probs.Sum(), 1e-10);
            Assert.IsFalse(double.IsNaN(probs[0]));
            Assert.AreEqual(2, probs.Length);
        }