Compartilhar via


ConversionsExtensionsCatalog.ConvertType Método

Definição

Sobrecargas

ConvertType(TransformsCatalog+ConversionTransforms, InputOutputColumnPair[], DataKind)

Criar um TypeConvertingEstimator, que converte o tipo dos dados para o tipo especificado em outputKind.

ConvertType(TransformsCatalog+ConversionTransforms, String, String, DataKind)

Criar um TypeConvertingEstimator, que converte o tipo dos dados para o tipo especificado em outputKind.

ConvertType(TransformsCatalog+ConversionTransforms, InputOutputColumnPair[], DataKind)

Criar um TypeConvertingEstimator, que converte o tipo dos dados para o tipo especificado em outputKind.

public static Microsoft.ML.Transforms.TypeConvertingEstimator ConvertType (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, Microsoft.ML.InputOutputColumnPair[] columns, Microsoft.ML.Data.DataKind outputKind = Microsoft.ML.Data.DataKind.Single);
static member ConvertType : Microsoft.ML.TransformsCatalog.ConversionTransforms * Microsoft.ML.InputOutputColumnPair[] * Microsoft.ML.Data.DataKind -> Microsoft.ML.Transforms.TypeConvertingEstimator
<Extension()>
Public Function ConvertType (catalog As TransformsCatalog.ConversionTransforms, columns As InputOutputColumnPair(), Optional outputKind As DataKind = Microsoft.ML.Data.DataKind.Single) As TypeConvertingEstimator

Parâmetros

catalog
TransformsCatalog.ConversionTransforms

O catálogo da transformação de conversão.

columns
InputOutputColumnPair[]

As colunas de entrada e saída. Essa transformação opera em tipos de dados numéricos, boolianos, DateTime de texto e de chave.

outputKind
DataKind

O tipo esperado da coluna de saída.

Retornos

Exemplos

using System;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    // This example illustrates how to convert multiple columns of different types
    // to one type, in this case System.Single. 
    // This is often a useful data transformation before concatenating the features
    // together and passing them to a particular estimator.
    public static class ConvertTypeMultiColumn
    {
        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(seed: 1);

            var rawData = new[] {
                new InputData() { Feature1 = true, Feature2 = "0.4",
                    Feature3 = DateTime.Now, Feature4 = 0.145},

                new InputData() { Feature1 = false, Feature2 = "0.5",
                    Feature3 = DateTime.Today, Feature4 = 3.14},

                new InputData() { Feature1 = false, Feature2 = "14",
                    Feature3 = DateTime.Today, Feature4 = 0.2046},

                new InputData() { Feature1 = false, Feature2 = "23",
                    Feature3 = DateTime.Now, Feature4 = 0.1206},

                new InputData() { Feature1 = true, Feature2 = "8904",
                    Feature3 = DateTime.UtcNow, Feature4 = 8.09},
            };

            // Convert the data to an IDataView.
            var data = mlContext.Data.LoadFromEnumerable(rawData);

            // Construct the pipeline.
            var pipeline = mlContext.Transforms.Conversion.ConvertType(new[]
            {
                    new InputOutputColumnPair("Converted1", "Feature1"),
                    new InputOutputColumnPair("Converted2", "Feature2"),
                    new InputOutputColumnPair("Converted3", "Feature3"),
                    new InputOutputColumnPair("Converted4", "Feature4"),
             },
             DataKind.Single);

            // Let's fit our pipeline to the data.
            var transformer = pipeline.Fit(data);
            // Transforming the same data. This will add the 4 columns defined in
            // the pipeline, containing the converted
            // values of the initial columns. 
            var transformedData = transformer.Transform(data);

            // Shape the transformed data as a strongly typed IEnumerable.
            var convertedData = mlContext.Data.CreateEnumerable<TransformedData>(
                transformedData, true);

            // Printing the results.
            Console.WriteLine("Converted1\t Converted2\t Converted3\t Converted4");
            foreach (var item in convertedData)
                Console.WriteLine($"\t{item.Converted1}\t {item.Converted2}\t\t  " +
                    $"{item.Converted3}\t {item.Converted4}");

            // Transformed data.
            //
            // Converted1   Converted2    Converted3     Converted4
            //      1        0.4        6.368921E+17        0.145
            //      0        0.5        6.368916E+17        3.14
            //      0        14         6.368916E+17        0.2046
            //      0        23         6.368921E+17        0.1206
            //      1       8904        6.368924E+17        8.09

        }

        // The initial data type
        private class InputData
        {
            public bool Feature1;
            public string Feature2;
            public DateTime Feature3;
            public double Feature4;
        }

        // The resulting data type after the transformation
        private class TransformedData : InputData
        {
            public float Converted1 { get; set; }
            public float Converted2 { get; set; }
            public float Converted3 { get; set; }
            public float Converted4 { get; set; }
        }
    }
}

Comentários

Essa transformação pode operar em várias colunas.

Aplica-se a

ConvertType(TransformsCatalog+ConversionTransforms, String, String, DataKind)

Criar um TypeConvertingEstimator, que converte o tipo dos dados para o tipo especificado em outputKind.

public static Microsoft.ML.Transforms.TypeConvertingEstimator ConvertType (this Microsoft.ML.TransformsCatalog.ConversionTransforms catalog, string outputColumnName, string inputColumnName = default, Microsoft.ML.Data.DataKind outputKind = Microsoft.ML.Data.DataKind.Single);
static member ConvertType : Microsoft.ML.TransformsCatalog.ConversionTransforms * string * string * Microsoft.ML.Data.DataKind -> Microsoft.ML.Transforms.TypeConvertingEstimator
<Extension()>
Public Function ConvertType (catalog As TransformsCatalog.ConversionTransforms, outputColumnName As String, Optional inputColumnName As String = Nothing, Optional outputKind As DataKind = Microsoft.ML.Data.DataKind.Single) As TypeConvertingEstimator

Parâmetros

catalog
TransformsCatalog.ConversionTransforms

O catálogo da transformação de conversão.

outputColumnName
String

Nome da coluna resultante da transformação de inputColumnName.

inputColumnName
String

Nome da coluna a ser transformada. Se definido como null, o valor do outputColumnName será usado como origem. Essa transformação opera em tipos de dados numéricos, boolianos, DateTime de texto e de chave.

outputKind
DataKind

O tipo esperado da coluna de saída.

Retornos

Exemplos

using System;
using Microsoft.ML;
using Microsoft.ML.Data;

namespace Samples.Dynamic
{
    public static class ConvertType
    {
        public static void Example()
        {
            var mlContext = new MLContext(seed: 1);
            var rawData = new[] {
                new InputData() { Survived = true },
                new InputData() { Survived = false },
                new InputData() { Survived = true },
                new InputData() { Survived = false },
                new InputData() { Survived = false },
            };

            var data = mlContext.Data.LoadFromEnumerable(rawData);

            // Construct the pipeline.
            var pipeline = mlContext.Transforms.Conversion.ConvertType(
                "SurvivedInt32", "Survived", DataKind.Int32);

            // Let's train our pipeline, and then apply it to the same data.
            var transformer = pipeline.Fit(data);
            var transformedData = transformer.Transform(data);

            // Display original column 'Survived' (boolean) and converted column 
            // SurvivedInt32' (Int32)
            var convertedData = mlContext.Data.CreateEnumerable<TransformedData>(
                transformedData, true);

            foreach (var item in convertedData)
            {
                Console.WriteLine("A:{0,-10}  Aconv:{1}", item.Survived,
                    item.SurvivedInt32);
            }

            // Output
            // A: True     Aconv:1
            // A: False    Aconv:0
            // A: True     Aconv:1
            // A: False    Aconv:0
            // A: False    Aconv:0
        }

        private class InputData
        {
            public bool Survived;
        }

        private sealed class TransformedData : InputData
        {
            public Int32 SurvivedInt32 { get; set; }
        }
    }
}

Aplica-se a