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
        /// <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;
        }
Example #2
0
 /// <summary>
 /// Summarizes a given Matrix.
 /// </summary>
 /// <param name="matrix">Matrix to summarize.</param>
 /// <param name="byVector">Indicates which direction to summarize, default is <see cref="VectorType.Row"/> indicating top-down.</param>
 /// <returns></returns>
 public static Summary Summarize(Matrix matrix, VectorType byVector = VectorType.Row)
 {
     return new Summary()
     {
         Average = matrix.Mean(byVector),
         StandardDeviation = matrix.StdDev(byVector),
         Minimum = matrix.Min(byVector),
         Maximum = matrix.Max(byVector),
         Median = matrix.Median(byVector)
     };
 }
Example #3
0
        public Cluster Generate(Descriptor descriptor, IEnumerable<object> examples, int k, IDistance metric = null)
        {
            var data = examples.ToArray();
            Descriptor = descriptor;
            X = Descriptor.Convert(data).ToMatrix();

            // generate assignments
            var assignments = Generate(X, k, metric);

            // begin packing objects into clusters
            var objects = new List<object>[k];
            for (int i = 0; i < assignments.Length; i++)
            {
                var a = assignments[i];
                if (objects[a] == null) objects[a] = new List<object>();
                objects[a].Add(data[i]);
            }

            // create clusters
            List<Cluster> clusters = new List<Cluster>();
            for(int i = 0; i < k; i++)
                if(!Centers[i].IsNaN()) // check for degenerate clusters
                    clusters.Add(new Cluster
                    {
                        Id = i + 1,
                        Center = Centers[i],
                        Members = objects[i].ToArray(),
                        Children = new Cluster[] { }
                    });

            // return single cluster with K children
            return new Cluster
            {
                Id = 0,
                Center = X.Mean(VectorType.Row),
                Children = clusters.ToArray()
            };
        }
Example #4
0
 /// <summary>
 /// A Matrix extension method that determines the mean of the given parameters.
 /// </summary>
 /// <param name="source">The source to act on.</param>
 /// <param name="t">Row or Column sum.</param>
 /// <returns>The mean value.</returns>
 public static Vector Mean(this Matrix source, VectorType t)
 {
     return(Matrix.Mean(source, t));
 }