Share via


NormalizationCatalog.NormalizeLpNorm 方法

定義

建立 , LpNormNormalizingEstimator 以將輸入資料行中的向量標準化 () 向量標準化為單位標準。 所使用的標準類型是由 所 norm 定義。 將 設定 ensureZeroMeantrue ,將會套用前置處理步驟,讓指定的資料行平均值成為零向量。

public static Microsoft.ML.Transforms.LpNormNormalizingEstimator NormalizeLpNorm (this Microsoft.ML.TransformsCatalog catalog, string outputColumnName, string inputColumnName = default, Microsoft.ML.Transforms.LpNormNormalizingEstimatorBase.NormFunction norm = Microsoft.ML.Transforms.LpNormNormalizingEstimatorBase+NormFunction.L2, bool ensureZeroMean = false);
static member NormalizeLpNorm : Microsoft.ML.TransformsCatalog * string * string * Microsoft.ML.Transforms.LpNormNormalizingEstimatorBase.NormFunction * bool -> Microsoft.ML.Transforms.LpNormNormalizingEstimator
<Extension()>
Public Function NormalizeLpNorm (catalog As TransformsCatalog, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional norm As LpNormNormalizingEstimatorBase.NormFunction = Microsoft.ML.Transforms.LpNormNormalizingEstimatorBase+NormFunction.L2, Optional ensureZeroMean As Boolean = false) As LpNormNormalizingEstimator

參數

catalog
TransformsCatalog

轉換的目錄。

outputColumnName
String

轉換 inputColumnName 所產生的資料行名稱。 此資料行的資料類型會與輸入資料行的資料類型相同。

inputColumnName
String

要正規化的資料行名稱。 如果設定為 null ,則會 outputColumnName 將 的值當做來源使用。 此估算器會透過 的已知大小向量 Single 運作。

norm
LpNormNormalizingEstimatorBase.NormFunction

用來正規化每個樣本的準則類型。 所產生向量的指示標準會正規化為一個。

ensureZeroMean
Boolean

如果為 true ,請在正規化之前從每個值減去平均數,否則使用原始輸入。

傳回

範例

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

namespace Samples.Dynamic
{
    class NormalizeLpNorm
    {
        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 mlContext = new MLContext();
            var samples = new List<DataPoint>()
            {
                new DataPoint(){ Features = new float[4] { 1, 1, 0, 0} },
                new DataPoint(){ Features = new float[4] { 2, 2, 0, 0} },
                new DataPoint(){ Features = new float[4] { 1, 0, 1, 0} },
                new DataPoint(){ Features = new float[4] { 0, 1, 0, 1} }
            };
            // Convert training data to IDataView, the general data type used in
            // ML.NET.
            var data = mlContext.Data.LoadFromEnumerable(samples);
            var approximation = mlContext.Transforms.NormalizeLpNorm("Features",
                norm: LpNormNormalizingEstimatorBase.NormFunction.L1,
                ensureZeroMean: true);

            // Now we can transform the data and look at the output to confirm the
            // behavior of the estimator. This operation doesn't actually evaluate
            // data until we read the data below.
            var tansformer = approximation.Fit(data);
            var transformedData = tansformer.Transform(data);

            var column = transformedData.GetColumn<float[]>("Features").ToArray();
            foreach (var row in column)
                Console.WriteLine(string.Join(", ", row.Select(x => x.ToString(
                    "f4"))));
            // Expected output:
            //  0.2500,  0.2500, -0.2500, -0.2500
            //  0.2500,  0.2500, -0.2500, -0.2500
            //  0.2500, -0.2500,  0.2500, -0.2500
            // -0.2500,  0.2500, -0.2500,  0.2500
        }

        private class DataPoint
        {
            [VectorType(4)]
            public float[] Features { get; set; }
        }
    }
}

適用於