/// <summary>Generate Linear Regression model based on a set of examples.</summary>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <returns>Model.</returns>
        public override IModel Generate(Matrix x, Vector y)
        {
            // create initial theta
            Vector theta = Vector.Ones(x.Cols + 1);
            Matrix copy  = x.Copy();

            // normalise features
            for (int i = 0; i < copy.Cols; i++)
            {
                var j = FeatureNormalizer.FeatureScale(copy[i, VectorType.Col]);
                for (int k = 0; k < copy.Rows; k++)
                {
                    copy[k, i] = j[k];
                }
            }

            // add intercept term
            copy = copy.Insert(Vector.Ones(copy.Rows), 0, VectorType.Col);

            // run gradient descent
            var run = GradientDescent.Run(theta, copy, y, this.MaxIterations, this.LearningRate, new LinearCostFunction(), this.Lambda, new Regularization());

            // once converged create model and apply theta

            LinearRegressionModel model = new LinearRegressionModel(x.Mean(VectorType.Row), x.StdDev(VectorType.Row))
            {
                Descriptor = this.Descriptor,
                Theta      = run.Item2
            };

            return(model);
        }
Example #2
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)
        {
            FeatureProperties = Summary.Summarize(X1);

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

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

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

                FeatureDiscretizer = new BinningDiscretizer(bins.ToVector());
                FeatureDiscretizer.Initialize(X1, FeatureProperties);
            }
        }
Example #3
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)
        {
            FeatureProperties = new 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 (NormalizeFeatures)
            {
                if (FeatureNormalizer != null)
                {
                    for (var i = 0; i < X.Rows; i++)
                    {
                        var vectors = FeatureNormalizer.Normalize(X[i, VectorType.Row], FeatureProperties);
                        for (var j = 0; j < X.Cols; j++)
                        {
                            X[i, j] = vectors[j];
                        }
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        ///     TODO The normalise.
        /// </summary>
        /// <param name="y">
        ///     TODO The y.
        /// </param>
        /// <returns>
        /// </returns>
        private Vector Normalise(Vector y)
        {
            for (var i = 0; i < y.Length; i++)
            {
                y[i] = FeatureNormalizer.FeatureScale(y[i], this.FeatureAverages[i], this.FeatureStandardDeviations[i]);
            }

            return(y.Insert(0, 1.0d));
        }
        public List <double> CompareToTemplate(List <SignatureFeatures> aFeaturesToCompare)
        {
            List <double> lScoreList = new List <double>();

            foreach (var featureSet in aFeaturesToCompare)
            {
                lScoreList.Add(CalculateScores(mTemplate, FeatureNormalizer.NormalizeFeaturesSingleSignature(featureSet, mNormalized.NormalizedWith)));
            }

            return(lScoreList);
        }
Example #6
0
        /// <summary>
        ///   Preprocessed the input vector.
        /// </summary>
        /// <param name="x">Input vector.</param>
        /// <returns>Vector.</returns>
        protected void Preprocess(Vector x)
        {
            if (NormalizeFeatures)
            {
                var xp = FeatureNormalizer.Normalize(x, FeatureProperties);

                for (var i = 0; i < x.Length; i++)
                {
                    x[i] = xp[i];
                }
            }
        }
        public Detector(List <SignatureFeatures> aFeatures)
        {
            mNormalized = FeatureNormalizer.NormalizeFeatures(aFeatures);

            foreach (var featureSet in mNormalized.ListOfFeatureSets)
            {
                foreach (var feature in featureSet)
                {
                    mTemplate[feature.Key] += feature.Value;
                }
            }

            foreach (var feature in mTemplate.ToList())
            {
                mTemplate[feature.Key] /= mNormalized.ListOfFeatureSets.Count;
            }
        }