MklComponentsCatalog.VectorWhiten Method

Definition

Takes column filled with a vector of random variables with a known covariance matrix into a set of new variables whose covariance is the identity matrix, meaning that they are uncorrelated and each have variance 1.

public static Microsoft.ML.Transforms.VectorWhiteningEstimator VectorWhiten (this Microsoft.ML.TransformsCatalog catalog, string outputColumnName, string inputColumnName = default, Microsoft.ML.Transforms.WhiteningKind kind = Microsoft.ML.Transforms.WhiteningKind.ZeroPhaseComponentAnalysis, float epsilon = 1E-05, int maximumNumberOfRows = 100000, int rank = 0);
static member VectorWhiten : Microsoft.ML.TransformsCatalog * string * string * Microsoft.ML.Transforms.WhiteningKind * single * int * int -> Microsoft.ML.Transforms.VectorWhiteningEstimator
<Extension()>
Public Function VectorWhiten (catalog As TransformsCatalog, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional kind As WhiteningKind = Microsoft.ML.Transforms.WhiteningKind.ZeroPhaseComponentAnalysis, Optional epsilon As Single = 1E-05, Optional maximumNumberOfRows As Integer = 100000, Optional rank As Integer = 0) As VectorWhiteningEstimator

Parameters

catalog
TransformsCatalog

The transform's catalog.

outputColumnName
String

Name of the column resulting from the transformation of inputColumnName.

inputColumnName
String

Name of the column to transform. If set to null, the value of the outputColumnName will be used as source.

kind
WhiteningKind

Whitening kind (PCA/ZCA).

epsilon
Single

Whitening constant, prevents division by zero.

maximumNumberOfRows
Int32

Maximum number of rows used to train the transform.

rank
Int32

In case of PCA whitening, indicates the number of components to retain.

Returns

Examples

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public sealed class VectorWhiten
    {

        /// This example requires installation of additional nuget package 
        /// <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for
            // exception tracking and logging, as well as the source of randomness.
            var ml = new MLContext();

            // Get a small dataset as an IEnumerable and convert it to an IDataView.
            var data = GetVectorOfNumbersData();
            var trainData = ml.Data.LoadFromEnumerable(data);

            // Preview of the data.
            //
            // Features
            // 0   1   2   3   4   5   6   7   8   9
            // 1   2   3   4   5   6   7   8   9   0  
            // 2   3   4   5   6   7   8   9   0   1
            // 3   4   5   6   7   8   9   0   1   2
            // 4   5   6   7   8   9   0   1   2   3
            // 5   6   7   8   9   0   1   2   3   4
            // 6   7   8   9   0   1   2   3   4   5

            // A small printing utility.
            Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName,
                column) =>
            {
                Console.WriteLine($"{colName} column obtained " +
                    $"post-transformation.");

                foreach (var row in column)
                    Console.WriteLine(string.Join(" ", row.DenseValues().Select(x =>
                        x.ToString("f3"))) + " ");
            };

            // A pipeline to project Features column into white noise vector.
            var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(
                SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms
                .WhiteningKind.ZeroPhaseComponentAnalysis);

            // The transformed (projected) data.
            var transformedData = whiteningPipeline.Fit(trainData).Transform(
                trainData);

            // Getting the data of the newly created column, so we can preview it.
            var whitening = transformedData.GetColumn<VBuffer<float>>(
                transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);

            printHelper(nameof(SampleVectorOfNumbersData.Features), whitening);

            // Features column obtained post-transformation.
            //
            //-0.394 -0.318 -0.243 -0.168  0.209  0.358  0.433  0.589  0.873  2.047
            //-0.034  0.030  0.094  0.159  0.298  0.427  0.492  0.760  1.855 -1.197
            // 0.099  0.161  0.223  0.286  0.412  0.603  0.665  1.797 -1.265 -0.172
            // 0.211  0.277  0.344  0.410  0.606  1.267  1.333 -1.340 -0.205  0.065
            // 0.454  0.523  0.593  0.664  1.886 -0.757 -0.687 -0.022  0.176  0.310
            // 0.863  0.938  1.016  1.093 -1.326 -0.096 -0.019  0.189  0.330  0.483
        }

        private class SampleVectorOfNumbersData
        {
            [VectorType(10)]
            public float[] Features { get; set; }
        }

        /// <summary>
        /// Returns a few rows of the infertility dataset.
        /// </summary>
        private static IEnumerable<SampleVectorOfNumbersData>
            GetVectorOfNumbersData()
        {
            var data = new List<SampleVectorOfNumbersData>();
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 0,
                1, 2, 3, 4, 5, 6, 7, 8, 9 }
            });

            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 1,
                2, 3, 4, 5, 6, 7, 8, 9, 0 }
            });

            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }
            });
            return data;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public sealed class VectorWhitenWithOptions
    {
        /// This example requires installation of additional nuget package
        /// <a href="https://www.nuget.org/packages/Microsoft.ML.Mkl.Components/">Microsoft.ML.Mkl.Components</a>.
        public static void Example()
        {
            // Create a new ML context, for ML.NET operations. It can be used for
            // exception tracking and logging, as well as the source of randomness.
            var ml = new MLContext();

            // Get a small dataset as an IEnumerable and convert it to an IDataView.
            var data = GetVectorOfNumbersData();
            var trainData = ml.Data.LoadFromEnumerable(data);

            // Preview of the data.
            //
            // Features
            // 0   1   2   3   4   5   6   7   8   9
            // 1   2   3   4   5   6   7   8   9   0  
            // 2   3   4   5   6   7   8   9   0   1
            // 3   4   5   6   7   8   9   0   1   2
            // 4   5   6   7   8   9   0   1   2   3
            // 5   6   7   8   9   0   1   2   3   4
            // 6   7   8   9   0   1   2   3   4   5

            // A small printing utility.
            Action<string, IEnumerable<VBuffer<float>>> printHelper = (colName,
                column) =>
            {
                Console.WriteLine($"{colName} column obtained" +
                    $"post-transformation.");

                foreach (var row in column)
                    Console.WriteLine(string.Join(" ", row.DenseValues().Select(x =>
                        x.ToString("f3"))) + " ");
            };


            // A pipeline to project Features column into white noise vector.
            var whiteningPipeline = ml.Transforms.VectorWhiten(nameof(
                SampleVectorOfNumbersData.Features), kind: Microsoft.ML.Transforms
                .WhiteningKind.PrincipalComponentAnalysis, rank: 4);

            // The transformed (projected) data.
            var transformedData = whiteningPipeline.Fit(trainData).Transform(
                trainData);

            // Getting the data of the newly created column, so we can preview it.
            var whitening = transformedData.GetColumn<VBuffer<float>>(
                transformedData.Schema[nameof(SampleVectorOfNumbersData.Features)]);

            printHelper(nameof(SampleVectorOfNumbersData.Features), whitening);

            // Features column obtained post-transformation.
            // -0.979  0.867  1.449  1.236
            // -1.030  1.012  0.426 -0.902
            // -1.047  0.677 -0.946 -1.060
            // -1.029  0.019 -1.502  1.108
            // -0.972 -1.338 -0.028  0.614
            // -0.938 -1.405  0.752 -0.967
        }

        private class SampleVectorOfNumbersData
        {
            [VectorType(10)]
            public float[] Features { get; set; }
        }

        /// <summary>
        /// Returns a few rows of the infertility dataset.
        /// </summary>
        private static IEnumerable<SampleVectorOfNumbersData>
            GetVectorOfNumbersData()
        {
            var data = new List<SampleVectorOfNumbersData>();
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 0,
                1, 2, 3, 4, 5, 6, 7, 8, 9 }
            });

            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 1,
                2, 3, 4, 5, 6, 7, 8, 9, 0 }
            });

            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 5, 6, 7, 8, 9, 0, 1, 2, 3, 4 }
            });
            data.Add(new SampleVectorOfNumbersData
            {
                Features = new float[10] { 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }
            });
            return data;
        }
    }
}

Applies to