|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Microsoft.ML.Core.Data; |
| 6 | +using Microsoft.ML.Runtime.Internal.Utilities; |
| 7 | +using Microsoft.ML.StaticPipe; |
| 8 | +using Microsoft.ML.StaticPipe.Runtime; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
| 12 | + |
| 13 | +namespace Microsoft.ML.Runtime.Data |
| 14 | +{ |
| 15 | + /// <include file='doc.xml' path='doc/members/member[@name="PCA"]/*' /> |
| 16 | + public sealed class PcaEstimator : TrainedWrapperEstimatorBase |
| 17 | + { |
| 18 | + private readonly PcaTransform.Arguments _args; |
| 19 | + |
| 20 | + /// <include file='doc.xml' path='doc/members/member[@name="PCA"]/*' /> |
| 21 | + /// <param name="env">The environment.</param> |
| 22 | + /// <param name="inputColumn">Input column to apply PCA on.</param> |
| 23 | + /// <param name="outputColumn">Output column. Null means <paramref name="inputColumn"/> is replaced.</param> |
| 24 | + /// <param name="rank">The number of components in the PCA.</param> |
| 25 | + /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param> |
| 26 | + public PcaEstimator(IHostEnvironment env, |
| 27 | + string inputColumn, |
| 28 | + string outputColumn = null, |
| 29 | + int rank = PcaTransform.Defaults.Rank, |
| 30 | + Action<PcaTransform.Arguments> advancedSettings = null) |
| 31 | + : this(env, new[] { (inputColumn, outputColumn ?? inputColumn) }, rank, advancedSettings) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + /// <include file='doc.xml' path='doc/members/member[@name="PCA"]/*' /> |
| 36 | + /// <param name="env">The environment.</param> |
| 37 | + /// <param name="columns">Pairs of columns to run the PCA on.</param> |
| 38 | + /// <param name="rank">The number of components in the PCA.</param> |
| 39 | + /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param> |
| 40 | + public PcaEstimator(IHostEnvironment env, (string input, string output)[] columns, |
| 41 | + int rank = PcaTransform.Defaults.Rank, |
| 42 | + Action<PcaTransform.Arguments> advancedSettings = null) |
| 43 | + : base(Contracts.CheckRef(env, nameof(env)).Register(nameof(PcaEstimator))) |
| 44 | + { |
| 45 | + foreach (var (input, output) in columns) |
| 46 | + { |
| 47 | + Host.CheckUserArg(Utils.Size(input) > 0, nameof(input)); |
| 48 | + Host.CheckValue(output, nameof(input)); |
| 49 | + } |
| 50 | + |
| 51 | + _args = new PcaTransform.Arguments(); |
| 52 | + _args.Column = columns.Select(x => new PcaTransform.Column { Source = x.input, Name = x.output }).ToArray(); |
| 53 | + _args.Rank = rank; |
| 54 | + |
| 55 | + advancedSettings?.Invoke(_args); |
| 56 | + } |
| 57 | + |
| 58 | + public override TransformWrapper Fit(IDataView input) |
| 59 | + { |
| 60 | + return new TransformWrapper(Host, new PcaTransform(Host, _args, input)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Extensions for statically typed <see cref="PcaEstimator"/>. |
| 66 | + /// </summary> |
| 67 | + public static class PcaEstimatorExtensions |
| 68 | + { |
| 69 | + private sealed class OutPipelineColumn : Vector<float> |
| 70 | + { |
| 71 | + public readonly Vector<float> Input; |
| 72 | + |
| 73 | + public OutPipelineColumn(Vector<float> input, int rank, Action<PcaTransform.Arguments> advancedSettings) |
| 74 | + : base(new Reconciler(null, rank, advancedSettings), input) |
| 75 | + { |
| 76 | + Input = input; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private sealed class Reconciler : EstimatorReconciler |
| 81 | + { |
| 82 | + private readonly int _rank; |
| 83 | + private readonly Action<PcaTransform.Arguments> _advancedSettings; |
| 84 | + |
| 85 | + public Reconciler(PipelineColumn weightColumn, int rank, Action<PcaTransform.Arguments> advancedSettings) |
| 86 | + { |
| 87 | + _rank = rank; |
| 88 | + _advancedSettings = advancedSettings; |
| 89 | + } |
| 90 | + |
| 91 | + public override IEstimator<ITransformer> Reconcile(IHostEnvironment env, |
| 92 | + PipelineColumn[] toOutput, |
| 93 | + IReadOnlyDictionary<PipelineColumn, string> inputNames, |
| 94 | + IReadOnlyDictionary<PipelineColumn, string> outputNames, |
| 95 | + IReadOnlyCollection<string> usedNames) |
| 96 | + { |
| 97 | + Contracts.Assert(toOutput.Length == 1); |
| 98 | + |
| 99 | + var pairs = new List<(string input, string output)>(); |
| 100 | + foreach (var outCol in toOutput) |
| 101 | + pairs.Add((inputNames[((OutPipelineColumn)outCol).Input], outputNames[outCol])); |
| 102 | + |
| 103 | + return new PcaEstimator(env, pairs.ToArray(), _rank, _advancedSettings); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// <include file='doc.xml' path='doc/members/member[@name="Whitening"]/*'/> |
| 108 | + /// <param name="input">The column to apply PCA to.</param> |
| 109 | + /// <param name="rank">The number of components in the PCA.</param> |
| 110 | + /// <param name="advancedSettings">A delegate to apply all the advanced arguments to the algorithm.</param> |
| 111 | + public static Vector<float> ToPrincipalComponents(this Vector<float> input, |
| 112 | + int rank = PcaTransform.Defaults.Rank, |
| 113 | + Action<PcaTransform.Arguments> advancedSettings = null) => new OutPipelineColumn(input, rank, advancedSettings); |
| 114 | + } |
| 115 | +} |
0 commit comments