MCAEmotiv.Classification.Example.ZScored C# (CSharp) Method

ZScored() static private method

Zscores the examples, returning the calculated means and standard deviations as out parameters.
static private ZScored ( IArrayView examples, IArrayView &means, IArrayView &standardDeviations ) : IArrayView
examples IArrayView
means IArrayView
standardDeviations IArrayView
return IArrayView
        internal static IArrayView<Example> ZScored(IArrayView<Example> examples, 
            out IArrayView<double> means, 
            out IArrayView<double> standardDeviations)
        {
            // compute means and standard deviations
            int numFeatures = examples[0].Features.Count;
            IArray<double> meansArray = Arrays.New<double>(numFeatures), standardDeviationsArray = Arrays.New<double>(numFeatures);
            double mean;
            for (int i = 0; i < numFeatures; i++)
            {
                standardDeviationsArray[i] = examples.Select(e => e.Features[i]).StandardDeviation(out mean);
                meansArray[i] = mean;
            }
            means = meansArray;
            standardDeviations = standardDeviationsArray;

            // z-score the examples
            return examples.Select(e => e.ZScored(meansArray, standardDeviationsArray)).ToIArray();
        }

Same methods

Example::ZScored ( IArrayView means, IArrayView standardDeviations ) : Example

Usage 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");
        }
All Usage Examples Of MCAEmotiv.Classification.Example::ZScored