Esempio n. 1
0
        /// <summary>
        /// Override to perform custom pre-processing steps on the raw Matrix data.
        /// </summary>
        /// <param name="X1">Matrix of initial states.</param>
        /// <param name="y">Vector of action labels.</param>
        /// <param name="X2">Matrix of transition states.</param>
        /// <param name="r">Vector of reward values.</param>
        /// <returns></returns>
        public virtual void Preprocess(Matrix X1, Vector y, Matrix X2, Vector r)
        {
            this.FeatureProperties = numl.Math.Summary.Summarize(X1);

            if (this.NormalizeFeatures)
            {
                if (this.FeatureNormalizer != null)
                {
                    for (int i = 0; i < X1.Rows; i++)
                    {
                        Vector v1 = this.FeatureNormalizer.Normalize(X1[i, VectorType.Row], this.FeatureProperties);
                        X1[i, VectorType.Row] = v1;

                        if (X2 != null)
                        {
                            Vector v2 = this.FeatureNormalizer.Normalize(X2[i, VectorType.Row], this.FeatureProperties);
                            X2[i, VectorType.Row] = v2;
                        }
                    }
                }
            }

            if (this.FeatureDiscretizer == null)
            {
                Matrix   temp = Matrix.VStack(X1, X2);
                double[] bins = new double[temp.Cols];
                for (int x = 0; x < X1.Cols; x++)
                {
                    bins[x] = temp[x, VectorType.Col].Distinct().Count();
                }

                this.FeatureDiscretizer = new numl.Math.Discretization.BinningDiscretizer(bins.ToVector());
                this.FeatureDiscretizer.Initialize(X1, this.FeatureProperties);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Override to perform custom pre-processing steps on the raw Matrix data.
        /// </summary>
        /// <param name="X">Matrix of examples.</param>
        /// <returns></returns>
        public virtual void Preprocess(Matrix X)
        {
            this.FeatureProperties = new numl.Math.Summary()
            {
                Average           = X.Mean(VectorType.Row),
                StandardDeviation = X.StdDev(VectorType.Row),
                Minimum           = X.Min(VectorType.Row),
                Maximum           = X.Max(VectorType.Row),
                Median            = X.Median(VectorType.Row)
            };

            if (this.NormalizeFeatures)
            {
                if (this.FeatureNormalizer != null)
                {
                    for (int i = 0; i < X.Rows; i++)
                    {
                        Vector vectors = this.FeatureNormalizer.Normalize(X[i, VectorType.Row], this.FeatureProperties);
                        for (int j = 0; j < X.Cols; j++)
                        {
                            X[i, j] = vectors[j];
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public void Test_Convert_MDPStates()
        {
            var states = new Matrix(
                new double[][]
            {
                new double[] { 1.0, 0.50, 0.75, 0.82 },
                new double[] { 2.0, 0.71, 0.71, 0.82 },
                new double[] { 3.0, 0.63, 0.69, 0.83 },
                new double[] { 2.0, 0.56, 0.73, 0.82 },
                new double[] { 4.0, 0.67, 0.74, 0.81 },
                new double[] { 5.0, 0.73, 0.71, 0.82 },
                new double[] { 4.0, 0.67, 0.74, 0.81 },
                new double[] { 4.0, 0.67, 0.74, 0.81 },
            });

            var actions = new Vector(new double[]
            {
                1.0,
                1.0,
                1.0,
                2.0,
                1.0,
                0.0,
                0.0,
                0.0
            });

            var statesP = new Matrix(
                new double[][]
            {
                new double[] { 2.0, 0.50, 0.75, 0.82 },     // 1 > 2
                new double[] { 3.0, 0.71, 0.71, 0.82 },     // 2 > 3
                new double[] { 4.0, 0.63, 0.69, 0.83 },     // 3 > 4
                new double[] { 5.0, 0.56, 0.73, 0.82 },     // 2 > 5
                new double[] { 5.0, 0.67, 0.74, 0.81 },     // 4 > 5
                new double[] { 2.0, 0.73, 0.71, 0.82 },     // 5 > 2
                new double[] { 2.0, 0.71, 0.71, 0.82 },     // 4 > 2
                new double[] { 2.0, 0.71, 0.71, 0.82 },     // 4 > 2 (duplicate)
            });

            var rewards = new Vector(new double[]
            {
                0.01,
                0.02,
                0.02,
                0.03,
                0.01,
                0.01,
                0.03,
                0.03
            });

            numl.Math.Summary summary = numl.Math.Summary.Summarize(states);

            var mdps = numl.Reinforcement.MDPConverter.GetStates(states, actions, statesP, rewards,
                                                                 null, new BinningDiscretizer(new Vector(new double[] { 5, 1, 1, 1 })));

            Assert.True(mdps.Count() == 1, "Length of starting states was more than one");
        }
Esempio n. 4
0
 /// <summary>
 /// Normalize a row vector using Logistic normalization.
 /// </summary>
 /// <param name="row"></param>
 /// <param name="properties"></param>
 /// <returns></returns>
 public Vector Normalize(Vector row, numl.Math.Summary properties)
 {
     if (row == null)
     {
         throw new ArgumentNullException("Row was null");
     }
     double[] item = new double[row.Length];
     for (int i = 0; i < row.Length; i++)
     {
         item[i] = this.Logistic.Compute(row[i]);
     }
     return(item);
 }
Esempio n. 5
0
 /// <summary>
 /// Normalize a row vector using Z-Score normalization on the supplied feature properties.
 /// </summary>
 /// <param name="row"></param>
 /// <param name="properties"></param>
 /// <returns></returns>
 public Vector Normalize(Vector row, numl.Math.Summary properties)
 {
     if (row == null)
     {
         throw new ArgumentNullException("Row was null");
     }
     double[] item = new double[row.Length];
     for (int i = 0; i < row.Length; i++)
     {
         item[i] = (row[i] - properties.Average[i]) / properties.StandardDeviation[i];
         item[i] = (double.IsNaN(item[i]) || double.IsInfinity(item[i]) ? 0d : item[i]);
     }
     return(item);
 }