/// <summary>Computes.</summary> /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception> /// <param name="x">The Vector to process.</param> /// <param name="y">The Vector to process.</param> /// <returns>A double.</returns> public double Compute(Vector x, Vector y) { if (x.Length != y.Length) throw new InvalidOperationException("Cannot compute similarity between two unequally sized Vectors!"); var xSum = x.Sum(); var ySum = y.Sum(); var xElem = (x^2).Sum() - ((xSum * xSum) / x.Length); var yElem = (y^2).Sum() - ((ySum * ySum) / y.Length); return (x.Dot(y) - ((xSum * ySum) / x.Length)) / System.Math.Sqrt(xElem * yElem); }
/// <summary> /// Compute the error cost of the given Theta parameter for the training and label sets /// </summary> /// <param name="theta">Learning Theta parameters</param> /// <param name="X">Training set</param> /// <param name="y">Training labels</param> /// <param name="lambda">Regularization constant</param> /// <param name="regularizer">Regularization term function.</param> /// <returns></returns> public double ComputeCost(Vector theta, Matrix X, Vector y, double lambda, IRegularizer regularizer) { int m = X.Rows; double j = 0.0; Vector s = (X * theta).ToVector(); IFunction function = new Logistic(); s = s.Each(v => function.Compute(v)); Vector slog = s.Copy().Each(v => System.Math.Log(System.Math.Abs(1.0 - v))); j = (-1.0 / m) * ( (y.Dot(s.Log())) + (-1.0 * ((1.0 - y).Dot(slog))) ); if (lambda != 0) { j = regularizer.Regularize(j, theta, m, lambda); } return j; }
/// <summary> /// Computes the linear kernel function between the two input vectors. /// </summary> /// <param name="v1">Vector one.</param> /// <param name="v2">Vector two.</param> /// <returns>Similarity.</returns> public double Compute(Vector v1, Vector v2) { return v1.Dot(v2); }
/// <summary>Computes.</summary> /// <param name="x">The Vector to process.</param> /// <param name="y">The Vector to process.</param> /// <returns>A double.</returns> public double Compute(Vector x, Vector y) { return x.Dot(y) / (x.Norm() * y.Norm()); }
public double Compute(Vector x, Vector y) { double dot = x.Dot(y); return dot / (System.Math.Pow(x.Norm(), 2) + System.Math.Pow(y.Norm(), 2) - dot); }
/// <summary> /// Computes the logistic kernel function between the two input vectors. /// </summary> /// <param name="v1">Vector one.</param> /// <param name="v2">Vector two.</param> /// <returns>Similarity.</returns> public double Compute(Vector v1, Vector v2) { return LogisticFunction.Compute(v1.Dot(v2)); }
/// <summary>Computes.</summary> /// <param name="x">The Vector to process.</param> /// <param name="y">The Vector to process.</param> /// <returns>A double.</returns> public double Compute(Vector x, Vector y) { var dot = x.Dot(y); return dot / (Math.Pow(x.Norm(), 2) + Math.Pow(y.Norm(), 2) - dot); }
/// <summary> /// Create a prediction based on the learned Theta values and the supplied test item. /// </summary> /// <param name="y">Training record</param> /// <returns></returns> public override double Predict(Vector y) { y = this.Normalise(y); return y.Dot(this.Theta); }
/// <summary>A Vector extension method that dots.</summary> /// <param name="v">The v to act on.</param> /// <param name="x">The x to act on.</param> /// <returns>A double.</returns> public static double Dot(this Vector v, Vector x) { return(Vector.Dot(v, x)); }
/// <summary> /// Computes the polynomial kernel function between the two input vectors. /// </summary> /// <param name="v1">Vector one.</param> /// <param name="v2">Vector two.</param> /// <returns>Similarity.</returns> public double Compute(Vector v1, Vector v2) { return System.Math.Pow((1d + v1.Dot(v2)), Dimension); }