internal static double[][] Preprocess(T[] sequence) { if (sequence.Length == 0) { return(new double[0][]); } int dims = Datapoint <T> .Dimensions; double[][] result = new double[sequence.Length][]; for (int i = 0; i < sequence.Length; i++) { //result[i] = new double[] { sequence[i].X, sequence[i].Y }; result[i] = ((IDatapoint)sequence[i]) .AsArray() //.Cast<double>() .Select(f => (double)f) .ToArray(); } double[] currentAxisValues = new double[sequence.Length]; // Just to save on allocations a tiny bit by reusing it within a single axis. double[] means = new double[dims]; double[] sigmas = new double[dims]; for (int j = 0; j < dims; j++) { for (int i = 0; i < sequence.Length; i++) { currentAxisValues[i] = result[i][j]; } means[j] = currentAxisValues.Average(); sigmas[j] = Accord.Statistics.Measures.StandardDeviation(currentAxisValues); } sigmas = CrosslinkAxes.Apply <T>(sigmas); // Datapoint<T>.CrosslinkScalingFactors(sigmas); // Locks values in the same space - like X and Y axes - to the same window size. double[][] zscores = Accord.Statistics.Tools.ZScores(result, means, sigmas); return(Accord.Math.Elementwise.Add(zscores, 10)); }
public double[][] Preprocess(double[][] extracted, PreprocessorCoefficients?coefficients = null) { // If not using the (more advanced) precalculated full-dataset averages & sigmas, calculate them locally here. if (coefficients == null) { //var dims = Datapoint.From<Tsource>(default(Tsource)).Dimensions; var dims = Dimensions; double[] currentAxisValues = new double[extracted.Length]; // Just to save on allocations a tiny bit by reusing it within a single axis. double[] means = new double[dims]; double[] sigmas = new double[dims]; for (int j = 0; j < dims; j++) { for (int i = 0; i < extracted.Length; i++) { currentAxisValues[i] = extracted[i][j]; } means[j] = currentAxisValues.Average(); sigmas[j] = Accord.Statistics.Measures.StandardDeviation(currentAxisValues); } // Use our standard crosslinking function to lock values which should share a normalization constant into the same scale as each other. if (doCrosslink) { sigmas = CrosslinkAxes.Apply <Tin>(sigmas); // Locks values in the same space - like X and Y axes - to the same window size. } coefficients = new PreprocessorCoefficients() { Means = means, Sigmas = sigmas }; } // Calculate the z-scores, then make them positive by the simple expedient of adding ten to them. (Yes, yes, but ten-sigma? Gotta mean an error anyway.) double[][] zscores = Accord.Statistics.Tools.ZScores(extracted, coefficients.Value.Means, coefficients.Value.Sigmas); return(Accord.Math.Elementwise.Add(zscores, 10)); }