コード例 #1
1
        public void Process()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            var res = assembly.GetManifestResourceStream("AIFH_Vol1.Resources.abalone.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();

            // The following ranges are setup for the Abalone data set.  If you wish to normalize other files you will
            // need to modify the below function calls other files.
            ds.EncodeOneOfN(0, 0, 1);
            istream.Close();

            var trainingData = ds.ExtractSupervised(0, 10, 10, 1);

            var reg = new MultipleLinearRegression(10);
            var train = new TrainLeastSquares(reg, trainingData);
            train.Iteration();

            Query(reg, trainingData);
            Console.WriteLine("Error: " + train.Error);

        }
コード例 #2
0
        public void TestTrain()
        {

            double[][] x = {
                new [] {1.0},
                new [] {3.0},
                new [] {2.0},
                new [] {200.0},
                new [] {230.0}};

            double[][] y = {
                new [] {1.0},
                new [] {1.0},
                new [] {1.0},
                new [] {0.0},
                new [] {0.0}
        };


            var trainingData = BasicData.ConvertArrays(x, y);
            var regression = new MultipleLinearRegression(1) {LinkFunction = new LogitLinkFunction()};
            var train = new TrainReweightLeastSquares(regression, trainingData);
            train.Iteration();

            double[] input = { 0 };
            double[] output = regression.ComputeRegression(input);
            Assert.AreEqual(0.883301730269988, output[0], AIFH.DefaultPrecision);
        }
コード例 #3
0
 /// <summary>
 ///     Construct the trainer.
 /// </summary>
 /// <param name="theAlgorithm">The GLM to train.</param>
 /// <param name="theTrainingData">The training data.</param>
 public TrainReweightLeastSquares(MultipleLinearRegression theAlgorithm, IList<BasicData> theTrainingData)
 {
     _algorithm = theAlgorithm;
     _trainingData = theTrainingData;
     _gradient = new DenseMatrix(theAlgorithm.LongTermMemory.Length, 1);
     _hessian = new DenseMatrix(theAlgorithm.LongTermMemory.Length, theAlgorithm.LongTermMemory.Length);
 }
コード例 #4
0
 /// <summary>
 ///     Construct the trainer.
 /// </summary>
 /// <param name="theAlgorithm">The GLM to train.</param>
 /// <param name="theTrainingData">The training data.</param>
 public TrainReweightLeastSquares(MultipleLinearRegression theAlgorithm, IList <BasicData> theTrainingData)
 {
     _algorithm    = theAlgorithm;
     _trainingData = theTrainingData;
     _gradient     = new DenseMatrix(theAlgorithm.LongTermMemory.Length, 1);
     _hessian      = new DenseMatrix(theAlgorithm.LongTermMemory.Length, theAlgorithm.LongTermMemory.Length);
 }
コード例 #5
0
        public void TestTrain()
        {
            double[][] x = {
                new [] {5.0, 10.0, 2.0},
                new []{10.0, 20.0, 4.0},
                new []{15.0, 30.0, 6.0},
                new []{20.0, 40.0, 8.0},
                new []{25.0, 50.0, 10.0}};

            double[][] y = {
                new []{70.0},
                new []{132.0},
                new []{194.0},
                new []{256.0},
                new []{318.0}
            };

            var trainingData = BasicData.ConvertArrays(x, y);
            var regression = new MultipleLinearRegression(3);
            var train = new TrainLeastSquares(regression, trainingData);
            train.Iteration();

            Assert.AreEqual(8, regression.LongTermMemory[0], 0.0001);
            Assert.AreEqual(-54.8, regression.LongTermMemory[1], 0.0001);
            Assert.AreEqual(8, regression.LongTermMemory[2], 0.0001);
            Assert.AreEqual(1, train.R2, 0.0001);
            Assert.AreEqual(0, train.Error, AIFH.DefaultPrecision);

            for (int i = 0; i < x.Length; i++)
            {
                double[] output = regression.ComputeRegression(x[i]);
                Assert.AreEqual(y[i][0], output[0], 0.0001);
            }
        }
コード例 #6
0
        public void TestBasic()
        {
            var reg = new MultipleLinearRegression(1);

            Assert.AreEqual(2, reg.LongTermMemory.Length);

            var lnk = new LogLinkFunction();
            reg.LinkFunction = lnk;
            Assert.IsTrue(reg.LinkFunction == lnk);

            reg.LongTermMemory[0] = 1;
            reg.LongTermMemory[1] = 2;

            double[] input = { 1.0 };
            double[] output = reg.ComputeRegression(input);
            Assert.AreEqual(1, output.Length);
            Assert.AreEqual(1.0986122886681098, output[0], AIFH.DefaultPrecision);
        }
コード例 #7
0
ファイル: GLMExample.cs プロジェクト: Raghavendra1509/aifh
        /// <summary>
        /// Run the example.
        /// </summary>
        public void Process()
        {
            // read the iris data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            var res = assembly.GetManifestResourceStream("AIFH_Vol1.Resources.breast-cancer-wisconsin.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();

            ds.DeleteUnknowns();
            ds.DeleteColumn(0);
            ds.ReplaceColumn(9, 4, 1, 0);
            var trainingData = ds.ExtractSupervised(0, 9, 9, 1);

            var reg = new MultipleLinearRegression(9) {LinkFunction = new LogitLinkFunction()};
            var train = new TrainReweightLeastSquares(reg, trainingData);

            int iteration = 0;
            do
            {
                iteration++;
                train.Iteration();
                Console.WriteLine("Iteration #" + iteration + ", Error: " + train.Error);
            } while (iteration < 1000 && train.Error > 0.01);

            Query(reg, trainingData);
            Console.WriteLine("Error: " + train.Error);


        }
コード例 #8
0
 /// <summary>
 ///     Construct the trainer.
 /// </summary>
 /// <param name="theAlgorithm">The algorithm to train.</param>
 /// <param name="theTrainingData">The training data.</param>
 public TrainLeastSquares(MultipleLinearRegression theAlgorithm, IList <BasicData> theTrainingData)
 {
     _algorithm    = theAlgorithm;
     _trainingData = theTrainingData;
 }
コード例 #9
0
ファイル: TrainLeastSquares.cs プロジェクト: pwoolcoc/aifh
 /// <summary>
 ///     Construct the trainer.
 /// </summary>
 /// <param name="theAlgorithm">The algorithm to train.</param>
 /// <param name="theTrainingData">The training data.</param>
 public TrainLeastSquares(MultipleLinearRegression theAlgorithm, IList<BasicData> theTrainingData)
 {
     _algorithm = theAlgorithm;
     _trainingData = theTrainingData;
 }