numl.Math.LinearAlgebra.Matrix.Mean C# (CSharp) Method

Mean() public static method

Determines the mean of the given parameters.
public static Mean ( Matrix source, VectorType t ) : Vector
source Matrix Source for the.
t VectorType Row or Column sum.
return Vector
        public static Vector Mean(Matrix source, VectorType t)
        {
            int count = t == VectorType.Row ? source.Cols : source.Rows;
            VectorType type = t == VectorType.Row ? VectorType.Col : VectorType.Row;
            Vector v = new Vector(count);
            for (int i = 0; i < count; i++)
                v[i] = source[i, type].Mean();
            return v;
        }

Usage Example

        /// <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, MaxIterations, LearningRate, new LinearCostFunction(),
                Lambda, new Regularization());

            // once converged create model and apply theta

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

            return model;
        }
All Usage Examples Of numl.Math.LinearAlgebra.Matrix::Mean