コード例 #1
0
        public void CheckTrain()
        {
            //model data
            double[] incomes = { 63, 16, 28, 55, 22, 20 };
            double[] ages    = { 38, 23, 40, 27, 18, 40 };
            bool[]   willBuy = { true, false, true, true, false, false };

            //test data
            double[] incomesTest = { 58, 18, 22 };
            double[] agesTest    = { 36, 24, 37 };
            bool[]   yTest       = { true, false, false };

            //train and infer
            var machine      = new BayesPointMachine();
            var model        = machine.Train(incomes, ages, willBuy);
            var result       = machine.Test(incomesTest, agesTest, model);
            var resultInBool = result.Select(bernoulli => bernoulli.GetProbTrue() > 0.5 ? true : false).ToList();

            //check accuracy
            var accuracy = 0;

            for (var i = 0; i < resultInBool.Count(); i++)
            {
                accuracy += resultInBool[i] == yTest[i] ? 1:0;
            }
            Assert.Equal(100, accuracy * 100 / resultInBool.Count);
        }
コード例 #2
0
        public void CheckTrain()
        {
            //model data
            double[] incomes = { 63, 16, 28, 55, 22, 20 };
            double[] ages = { 38, 23, 40, 27, 18, 40 };
            bool[] willBuy = { true, false, true, true, false, false };

            //test data
            double[] incomesTest = { 58, 18, 22 };
            double[] agesTest = { 36, 24, 37 };
            bool[] yTest = { true, false, false };

            //train and infer
            var machine = new BayesPointMachine();
            var model = machine.Train(incomes, ages, willBuy);
            var result= machine.Test(incomesTest, agesTest, model);
            var resultInBool = result.Select(bernoulli => bernoulli.GetProbTrue() > 0.5 ? true : false).ToList();

            //check accuracy
            var accuracy = 0;
            for (var i = 0; i < resultInBool.Count(); i++)
            {
                accuracy += resultInBool[i] == yTest[i] ? 1:0;
            }
            Assert.Equal(100, accuracy*100/resultInBool.Count);
        }
コード例 #3
0
        /// <summary>
        /// Load data from CSV.
        /// </summary>
        /// <param name="csvFilePath">CSV file path.</param>
        /// <param name="hasHeader">CSV has headers.</param>
        public virtual void LoadDataFromCsv(string csvFilePath, bool hasHeader = false)
        {
            if (initialized == false)
            {
                throw new Exception("ML Model has not been initialized yet.");
            }

            lock (mlModelCompilation)
            {
                hasHeaders = hasHeader;

                IEnumerable <string> csvLines = File.ReadAllLines(csvFilePath);

                if (hasHeader)
                {
                    csvLines = csvLines.Skip(1);
                }

                foreach (string line in csvLines)
                {
                    IList <double> dict =
                        new List <double>();

                    string[] vals = line.Replace(" ", "").Split(",");

                    for (int i = 0; i < vals.Length - 1; i++)
                    {
                        dict.Add(Convert.ToDouble(vals[i]));
                    }

                    Validity.Add(Convert.ToBoolean(vals[vals.Length - 1]));

                    Data.Add(dict.ToArray());
                }

                // Create random variable w with VectorGuassian (of size 3)
                // Features => (Gold | Nickel | Copper) Concentration, IsGoodMaterial?
                w = Variable.Random(new VectorGaussian(Vector.Zero(FeatureCount), PositiveDefiniteMatrix.Identity(FeatureCount))).Named("w");

                // Create Observable variable based on valid_ui;
                y = Variable.Observed(Validity.ToArray());

                BayesPointMachine.Run(this, y, w);
            }
        }
コード例 #4
0
        /// <summary>
        /// Make a prediction.
        /// </summary>
        /// <typeparam name="TModel">Model type.</typeparam>
        /// <param name="features">Feature values.</param>
        /// <returns>Probability matrix is valid.</returns>
        public virtual double Predict <TModel>(IList <double> features) where TModel : new()
        {
            if (initialized == false)
            {
                throw new Exception("ML Model has not been initialized yet.");
            }

            if (Data == null || Data.Count == 0)
            {
                throw new Exception("Dataset has 0 instances.");
            }

            if (features == null)
            {
                throw new ArgumentNullException("data");
            }

            lock (mlModelCompilation)
            {
                object model =
                    new TModel();

                ((BaseMLModel)model).Data =
                    new List <double[]>()
                {
                    features.ToArray()
                };

                VectorGaussian wPosterior =
                    engine.Infer <VectorGaussian>(w);

                VariableArray <bool> ytest =
                    Variable.Array <bool>(new Range(1));

                BayesPointMachine.Run(this, ytest, Variable.Random(wPosterior).Named("w"));

                return((engine.Infer(ytest) as IEnumerable <Bernoulli>)
                       .First()
                       .GetProbTrue());
            }
        }