예제 #1
0
        private static void TestNaiveBayes()
        {
            var data = LoadDataFromfCSV("Data.csv");

            var fixedData    = TableFixedData.FromTableData(data);
            var samples      = TableFixedData.ToSample(fixedData);
            var columnsTypes = fixedData.ColumnDataTypes;

            var algorithm = new NaiveBayesClassifierOld(fixedData);

            var algorithm1 = new NaiveBayesClassifier(samples, fixedData.ClassesValue.Length, columnsTypes);

            var dataRow = data.ToList()[2];

            var className = algorithm.Compute(dataRow);
            var classId   = algorithm1.Compute(fixedData.GetSample(dataRow));

            var className1 = fixedData.ClassesValue[classId];

            int missed = 0;

            for (int index = 0; index < 50; index++)
            {
                var row = data.ToList()[index];
                var estimatedClassName = algorithm.Compute(row);
                if (estimatedClassName != row.Class)
                {
                    missed++;
                }
            }
        }
예제 #2
0
        public int Compute(string sentence)
        {
            var sample           = new DataSample();
            var sampleDataPoints = new List <DataPoint>();

            var sentenceWords = TextParser.SplitToWords(sentence).ToArray();
            var isNegated     = false;

            for (int index = 0; index < sentenceWords.Length; index++)
            {
                var currentWord = sentenceWords[index].ToLower();
                if (_negationWords.Contains(currentWord))
                {
                    isNegated = !isNegated;
                }
                else
                {
                    if (currentWord.EndsWith("n't"))
                    {
                        isNegated = !isNegated;
                    }
                    else
                    {
                        currentWord = isNegated ? "not_" + currentWord : currentWord;
                    }


                    if (_wordDictionary.ContainsKey(currentWord))
                    {
                        sampleDataPoints.Add(new DataPoint {
                            ColumnId = _wordDictionary[currentWord], Value = 1
                        });
                    }
                }
            }
            sample.DataPoints = sampleDataPoints.ToArray();

            return(_naiveBayesClassifier.Compute(sample));
        }