Exemplo n.º 1
0
        public void TestSgd()
        {
            var model  = new SgdClassifier(epochs: 10000, learningRate: 0.01);
            var xTrain = new double[, ] {
                { 2, 0 }, { 1, 1 }, { 2, 3 }, { 6, 7 }, { 8, 9 }, { 1, 1 }
            };
            var yTrain = new[] { 0, 0, 1, 1, 1, 0 };

            model.Fit(xTrain, yTrain);
            WriteLine("Training data:");
            WriteLine("Prediction:");
            foreach (var item in model.Predict(xTrain))
            {
                WriteLine(item.ToString());
            }

            WriteLine("Actual:");
            foreach (var item in yTrain)
            {
                WriteLine(item.ToString());
            }

            WriteLine("Beta params:");
            foreach (var item in model.BetaParam)
            {
                WriteLine(item.ToString(CultureInfo.InvariantCulture));
            }

            AreEqual(model.Predict(xTrain), yTrain);
        }
Exemplo n.º 2
0
        public void DatasetBenchmarkSgdTest3()
        {
            const string path = BasePath +
                                "\\ldp.csv";
            var lineCount  = File.ReadLines(path).Count();
            var reader     = new StreamReader(File.OpenRead(path));
            var properties = new double[lineCount, 22];

            for (var i2 = 0; i2 < lineCount; i2++)
            {
                var line = reader.ReadLine();
                for (var i = 0; i < 22; i++)
                {
                    if (line == null)
                    {
                        continue;
                    }
                    var values = line.Split(',');
                    properties[i2, i] = Convert.ToDouble(values[i]);
                }
            }

            var xTrain = new double[lineCount, 21];
            var yTrain = new int[lineCount];

            for (var i = 0; i < lineCount; i++)
            {
                for (var j = 0; j < 21; j++)
                {
                    xTrain[i, j] = properties[i, j];
                }

                yTrain[i] = (int)properties[i, 21];
            }

            var model = new SgdClassifier(epochs: 10000, learningRate: 0.005);

            model.Fit(xTrain, yTrain);
            WriteLine("Training data:");
            var predicted = model.Predict(xTrain);

            WriteLine("Accuracy:");
            var accuracy = Accuracy(predicted, yTrain);

            WriteLine(accuracy);
            WriteLine("Beta params:");
            foreach (var item in model.BetaParam)
            {
                WriteLine(item.ToString(CultureInfo.InvariantCulture));
            }

            Greater(accuracy, 0.75);
        }