ZScored() public method

Efficiently returns an identical example with features that have been z-scored using means and standardDeviations.
public ZScored ( IArrayView means, IArrayView standardDeviations ) : Example
means IArrayView
standardDeviations IArrayView
return Example
        public static void Run()
        {
            var ex = new Example(2, new double[] { 1, 3, 2 });

            // WithClass test
            if (ex.WithClass(5).Class != 5 || !ex.WithClass(5).Features.SequenceEqual(new double[] { 1, 3, 2 }))
                throw new Exception("WithClass failed");

            // ZScore test
            var examples = new Example[] {
                new Example(1, new double[] { 1, 7 }),
                new Example(3, new double[] { 5, 5 }),
                new Example(1, new double[] { 3, 3 }),
            }.AsIArray();

            IArrayView<double> means, sds;
            var zscored = examples.ZScored(out means, out sds);
            if (!means.SequenceEqual(new double[] { 3, 5 }))
                throw new Exception("Bad means");
            if (!sds.SequenceEqual(new double[] { 2, 2 }))
                throw new Exception("Bad standard deviations");
            foreach (int i in examples.Indices())
                foreach (int j in examples[0].Features.Indices())
                    if (zscored[i].Features[j] != (examples[i].Features[j] - means[j]) / sds[j])
                        throw new Exception("Bad zscore value");
        }
Example #2
0
 /// <summary>
 /// ZScores the examples, returning the calculated means and standard deviations as out parameters
 /// </summary>
 public static IArrayView <Example> ZScored(this IArrayView <Example> examples,
                                            out IArrayView <double> means,
                                            out IArrayView <double> standardDeviations)
 {
     return(Example.ZScored(examples, out means, out standardDeviations));
 }