TextCatalog.ProduceHashedNgrams メソッド

定義

オーバーロード

ProduceHashedNgrams(TransformsCatalog+TextTransforms, String, String, Int32, Int32, Int32, Boolean, UInt32, Boolean, Int32, Boolean)

NgramHashingEstimator指定されたinputColumnName列から新しいoutputColumnName列にデータをコピーし、ハッシュされた n-gram の数のベクトルを生成する 、作成します。

ProduceHashedNgrams(TransformsCatalog+TextTransforms, String, String[], Int32, Int32, Int32, Boolean, UInt32, Boolean, Int32, Boolean)

NgramHashingEstimator指定されたinputColumnNames複数の列から新しい列outputColumnNameにデータを受け取り、ハッシュされた n-gram の数のベクトルを生成する 、を作成します。

ProduceHashedNgrams(TransformsCatalog+TextTransforms, String, String, Int32, Int32, Int32, Boolean, UInt32, Boolean, Int32, Boolean)

NgramHashingEstimator指定されたinputColumnName列から新しいoutputColumnName列にデータをコピーし、ハッシュされた n-gram の数のベクトルを生成する 、作成します。

public static Microsoft.ML.Transforms.Text.NgramHashingEstimator ProduceHashedNgrams (this Microsoft.ML.TransformsCatalog.TextTransforms catalog, string outputColumnName, string inputColumnName = default, int numberOfBits = 16, int ngramLength = 2, int skipLength = 0, bool useAllLengths = true, uint seed = 314489979, bool useOrderedHashing = true, int maximumNumberOfInverts = 0, bool rehashUnigrams = false);
static member ProduceHashedNgrams : Microsoft.ML.TransformsCatalog.TextTransforms * string * string * int * int * int * bool * uint32 * bool * int * bool -> Microsoft.ML.Transforms.Text.NgramHashingEstimator
<Extension()>
Public Function ProduceHashedNgrams (catalog As TransformsCatalog.TextTransforms, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional numberOfBits As Integer = 16, Optional ngramLength As Integer = 2, Optional skipLength As Integer = 0, Optional useAllLengths As Boolean = true, Optional seed As UInteger = 314489979, Optional useOrderedHashing As Boolean = true, Optional maximumNumberOfInverts As Integer = 0, Optional rehashUnigrams As Boolean = false) As NgramHashingEstimator

パラメーター

catalog
TransformsCatalog.TextTransforms

変換のカタログ。

outputColumnName
String

の変換によって生成される列の inputColumnName名前。 この列のデータ型は ..Single

inputColumnName
String

データのコピー元の列の名前。 このエスティメーターは、キー型のベクトルで動作します。

numberOfBits
Int32

ハッシュ後のビット数。 1 から 30 までの数にする必要があります (1 と 30 も含まれます)。

ngramLength
Int32

Ngram の長さ。

skipLength
Int32

n-gram を構築するときにスキップするトークンの最大数。

useAllLengths
Boolean

すべての n グラムの長さを最大 ngramLength に含めるか、または長さだけを ngramLength含めるかを指定します。

seed
UInt32

ハッシュ シード。

useOrderedHashing
Boolean

各ソース列の位置をハッシュに含めるかどうか (複数のソース列があるとき)。

maximumNumberOfInverts
Int32

ハッシュ中に、元の値と生成されたハッシュ値の間のマッピングを構築します。 元の値のテキスト表現は、新しい列の注釈のスロット名に格納されます。そのため、ハッシュは多くの初期値を 1 つにマップできます。 maximumNumberOfInverts は、保持する必要があるハッシュに対応する個別の入力値の数の上限を指定します。 0 は入力値を保持しません。 -1 は 、各ハッシュにマッピングされたすべての入力値を保持します。

rehashUnigrams
Boolean

ユニグラムをリハッシングするかどうか。

戻り値

注釈

NgramHashingEstimatorは、トークン化されたテキストを入力として受け取り、WordHashBagEstimator内部的にテキストをトークン化する方法NgramHashingEstimatorとは異なりますWordHashBagEstimator

適用対象

ProduceHashedNgrams(TransformsCatalog+TextTransforms, String, String[], Int32, Int32, Int32, Boolean, UInt32, Boolean, Int32, Boolean)

NgramHashingEstimator指定されたinputColumnNames複数の列から新しい列outputColumnNameにデータを受け取り、ハッシュされた n-gram の数のベクトルを生成する 、を作成します。

public static Microsoft.ML.Transforms.Text.NgramHashingEstimator ProduceHashedNgrams (this Microsoft.ML.TransformsCatalog.TextTransforms catalog, string outputColumnName, string[] inputColumnNames = default, int numberOfBits = 16, int ngramLength = 2, int skipLength = 0, bool useAllLengths = true, uint seed = 314489979, bool useOrderedHashing = true, int maximumNumberOfInverts = 0, bool rehashUnigrams = false);
static member ProduceHashedNgrams : Microsoft.ML.TransformsCatalog.TextTransforms * string * string[] * int * int * int * bool * uint32 * bool * int * bool -> Microsoft.ML.Transforms.Text.NgramHashingEstimator
<Extension()>
Public Function ProduceHashedNgrams (catalog As TransformsCatalog.TextTransforms, outputColumnName As String, Optional inputColumnNames As String() = Nothing, Optional numberOfBits As Integer = 16, Optional ngramLength As Integer = 2, Optional skipLength As Integer = 0, Optional useAllLengths As Boolean = true, Optional seed As UInteger = 314489979, Optional useOrderedHashing As Boolean = true, Optional maximumNumberOfInverts As Integer = 0, Optional rehashUnigrams As Boolean = false) As NgramHashingEstimator

パラメーター

catalog
TransformsCatalog.TextTransforms

変換のカタログ。

outputColumnName
String

の変換によって生成される列の inputColumnNames名前。 この列のデータ型は、既知のサイズの Singleベクターになります。

inputColumnNames
String[]

データを取得する複数の列の名前。 このエスティメーターは、キー型のベクトルで動作します。

numberOfBits
Int32

ハッシュ後のビット数。 1 から 30 までの数にする必要があります (1 と 30 も含まれます)。

ngramLength
Int32

Ngram の長さ。

skipLength
Int32

n-gram を構築するときにスキップするトークンの最大数。

useAllLengths
Boolean

すべての n グラムの長さを最大 ngramLength に含めるか、または長さだけを ngramLength含めるかを指定します。

seed
UInt32

ハッシュ シード。

useOrderedHashing
Boolean

各ソース列の位置をハッシュに含めるかどうか (複数のソース列があるとき)。

maximumNumberOfInverts
Int32

ハッシュ中に、元の値と生成されたハッシュ値の間のマッピングを構築します。 元の値のテキスト表現は、新しい列の注釈のスロット名に格納されます。そのため、ハッシュは多くの初期値を 1 つにマップできます。 maximumNumberOfInverts は、保持する必要があるハッシュに対応する個別の入力値の数の上限を指定します。 0 は入力値を保持しません。 -1 は 、各ハッシュにマッピングされたすべての入力値を保持します。

rehashUnigrams
Boolean

ユニグラムをリハッシングするかどうか。

戻り値

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

namespace Samples.Dynamic
{
    public static class ProduceHashedNgrams
    {
        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();

            // Create a small dataset as an IEnumerable.
            var samples = new List<TextData>()
            {
                new TextData(){ Text = "This is an example to compute n-grams " +
                "using hashing." },

                new TextData(){ Text = "N-gram is a sequence of 'N' consecutive" +
                " words/tokens." },

                new TextData(){ Text = "ML.NET's ProduceHashedNgrams API " +
                "produces count of n-grams and hashes it as an index into a " +
                "vector of given bit length." },

                new TextData(){ Text = "The hashing reduces the size of the " +
                "output feature vector" },

                new TextData(){ Text = "which is useful in case when number of " +
                "n-grams is very large." },
            };

            // Convert training data to IDataView.
            var dataview = mlContext.Data.LoadFromEnumerable(samples);

            // A pipeline for converting text into numeric hashed n-gram features.
            // The following call to 'ProduceHashedNgrams' requires the tokenized
            // text /string as input. This is achieved by calling 
            // 'TokenizeIntoWords' first followed by 'ProduceHashedNgrams'.
            // Please note that the length of the output feature vector depends on
            // the 'numberOfBits' settings.
            var textPipeline = mlContext.Transforms.Text.TokenizeIntoWords("Tokens",
                "Text")
                .Append(mlContext.Transforms.Conversion.MapValueToKey("Tokens"))
                .Append(mlContext.Transforms.Text.ProduceHashedNgrams(
                    "NgramFeatures", "Tokens",
                    numberOfBits: 5,
                    ngramLength: 3,
                    useAllLengths: false,
                    maximumNumberOfInverts: 1));

            // Fit to data.
            var textTransformer = textPipeline.Fit(dataview);
            var transformedDataView = textTransformer.Transform(dataview);

            // Create the prediction engine to get the features extracted from the
            // text.
            var predictionEngine = mlContext.Model.CreatePredictionEngine<TextData,
                TransformedTextData>(textTransformer);

            // Convert the text into numeric features.
            var prediction = predictionEngine.Predict(samples[0]);

            // Print the length of the feature vector.
            Console.WriteLine("Number of Features: " + prediction.NgramFeatures
                .Length);

            // Preview of the produced n-grams.
            // Get the slot names from the column's metadata.
            // The slot names for a vector column corresponds to the names
            // associated with each position in the vector.
            VBuffer<ReadOnlyMemory<char>> slotNames = default;
            transformedDataView.Schema["NgramFeatures"].GetSlotNames(ref slotNames);
            var NgramFeaturesColumn = transformedDataView.GetColumn<VBuffer<float>>(
                transformedDataView.Schema["NgramFeatures"]);

            var slots = slotNames.GetValues();
            Console.Write("N-grams: ");
            foreach (var featureRow in NgramFeaturesColumn)
            {
                foreach (var item in featureRow.Items())
                    Console.Write($"{slots[item.Key]}  ");
                Console.WriteLine();
            }

            // Print the first 10 feature values.
            Console.Write("Features: ");
            for (int i = 0; i < 10; i++)
                Console.Write($"{prediction.NgramFeatures[i]:F4}  ");

            //  Expected output:
            //   Number of Features:  32
            //   N-grams:   This|is|an  example|to|compute  compute|n-grams|using  n-grams|using|hashing.  an|example|to  is|an|example  a|sequence|of  of|'N'|consecutive  is|a|sequence  N-gram|is|a  ...
            //   Features:    0.0000          0.0000               2.0000               0.0000               0.0000        1.0000          0.0000        0.0000              1.0000          0.0000  ...
        }

        private class TextData
        {
            public string Text { get; set; }
        }

        private class TransformedTextData : TextData
        {
            public float[] NgramFeatures { get; set; }
        }
    }
}

注釈

NgramHashingEstimatorは、トークン化されたテキストを入力として受け取り、WordHashBagEstimator内部的にテキストをトークン化する方法NgramHashingEstimatorとは異なりますWordHashBagEstimator

適用対象