/// <summary>A Vector extension method that segments.</summary> /// <exception cref="InvalidOperationException">Thrown when the requested operation is invalid.</exception> /// <param name="x">The x to act on.</param> /// <param name="segments">The segments.</param> /// <returns>A Range[].</returns> public static Range[] Segment(this Vector x, int segments) { if (segments < 2) { throw new InvalidOperationException("Invalid Segment Length, must > 1"); } // list max and min var max = x.Max(); var min = x.Min(); // range size var range = (max - min) / segments; // create range array var ranges = new Range[segments]; // first element starts with min ranges[0] = Range.Make(min, min + range); // each subsequent element is max // of previous and max of previous // plus the appropriate range for (int i = 1; i < segments; i++) { ranges[i] = Range.Make(ranges[i - 1].Max, ranges[i - 1].Max + range); } // make last range slightly larger // to maintain r.Min <= d < r.Max ranges[ranges.Length - 1].Max += .01; return(ranges); }
/// <summary> /// Rescales the input vector to the specified range. /// <para>When <paramref name="minValue"/> and <paramref name="maxValue"/> are null, the vector instance min and max values are used instead.</para> /// </summary> /// <param name="v">Vector to rescale.</param> /// <param name="min">New lower bound value.</param> /// <param name="max">New upper bound value.</param> /// <param name="minValue">Lower bound value prior to rescaling.</param> /// <param name="maxValue">Upper bound value prior to rescaling.</param> /// <returns></returns> public static Vector Rescale(this Vector v, double min, double max, double?minValue = null, double?maxValue = null) { double min_tm1 = (minValue ?? v.Min()); double max_tm1 = (maxValue ?? v.Max()); Vector v_t = v.Each(d => ((max - min) * (d - min_tm1)) / (max_tm1 - min_tm1), true); return(v_t); }
/// <summary> /// Computes the Normalized Root Mean Squared Error for the given inputs. /// </summary> /// <param name="y1">Predicted values.</param> /// <param name="y2">Actual values.</param> /// <returns>Double.</returns> public static double ComputeNormRMSE(Vector y1, Vector y2) { return numl.Supervised.Score.ComputeRMSE(y1, y2) / (y1.Max() - y1.Min()); }