다음을 통해 공유


ConversionsExtensionsCatalog.ConvertType 메서드

정의

오버로드

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

Create a TypeConvertingEstimator, which converts the type of the data to the type specified in outputKind.

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

Create a TypeConvertingEstimator, which converts the type of the data to the type specified in outputKind.

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

Create a TypeConvertingEstimator, which converts the type of the data to the type specified in 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

매개 변수

catalog
TransformsCatalog.ConversionTransforms

변환 변환의 카탈로그입니다.

columns
InputOutputColumnPair[]

입력 및 출력 열입니다. 이 변환은 숫자, 부울, 텍스트 DateTime 및 키 데이터 형식에 대해 작동합니다.

outputKind
DataKind

예상된 출력 열 종류입니다.

반환

예제

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; }
        }
    }
}

설명

이 변환은 여러 열에서 작동할 수 있습니다.

적용 대상

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

Create a TypeConvertingEstimator, which converts the type of the data to the type specified in 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

매개 변수

catalog
TransformsCatalog.ConversionTransforms

변환 변환의 카탈로그입니다.

outputColumnName
String

의 변환에서 생성된 열의 inputColumnName이름입니다.

inputColumnName
String

변환할 열의 이름입니다. 이 값으로 null설정하면 값이 outputColumnName 원본으로 사용됩니다. 이 변환은 숫자, 부울, 텍스트 DateTime 및 키 데이터 형식에 대해 작동합니다.

outputKind
DataKind

예상된 출력 열 종류입니다.

반환

예제

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; }
        }
    }
}

적용 대상