주요 콘텐츠

oneHotEncoderComponent

Pipeline component for encoding categorical data into one-hot vectors

Since R2026a

    Description

    oneHotEncoderComponent is a pipeline component that encodes categorical data into one-hot vectors. During the learn phase, the pipeline component identifies the encoded categories. During the run phase, the pipeline component uses the functionality of the onehotencode function to encode new categorical data.

    Creation

    Description

    component = oneHotEncoderComponent creates a pipeline component for encoding categorical data into one-hot vectors.

    component = oneHotEncoderComponent(Name=Value) sets writable Properties using one or more name-value arguments. For example, Limit=10 specifies to encode categorical variables with ten or fewer categories.

    example

    Properties

    expand all

    Learn Parameters

    The software sets learn parameters when you create the component. You can modify learn parameters using dot notation any time before you use the learn object function. Any unset learn parameters use the corresponding default values.

    Maximum number of categories, specified as a positive integer value. The component encodes only categorical variables with Limit or fewer categories.

    Example: c = oneHotEncoderComponent(Limit=10)

    Example: c.Limit = 30

    Data Types: single | double

    Component Properties

    The software sets component properties when you create the component. You can modify the component properties (excluding HasLearnables and HasLearned) using dot notation at any time. You cannot modify the HasLearnables and HasLearned properties directly.

    Component identifier, specified as a character vector or string scalar.

    Example: c = oneHotEncoderComponent(Name="Encoder")

    Example: c.Name = "OHE"

    Data Types: char | string

    Names of the input ports, specified as a character vector, string array, or cell array of character vectors.

    Example: c = oneHotEncoderComponent(Inputs="Data1")

    Example: c.Inputs = "X"

    Data Types: char | string | cell

    Names of the output ports, specified as a character vector, string array, or cell array of character vectors.

    Example: c = oneHotEncoderComponent(Outputs="EncodedX")

    Example: c.Outputs = "X"

    Data Types: char | string | cell

    Tags that enable the automatic connection of the component inputs with other components or pipelines, specified as a nonnegative integer vector. If you specify InputTags, then the number of tags must match the number of inputs in Inputs.

    Example: c = oneHotEncoderComponent(InputTags=2)

    Example: c.InputTags = 1

    Data Types: single | double

    Tags that enable the automatic connection of the component outputs with other components or pipelines, specified as a nonnegative integer vector. If you specify OutputTags, then the number of tags must match the number of outputs in Outputs.

    Example: c = oneHotEncoderComponent(OutputTags=0)

    Example: c.OutputTags = 1

    Data Types: single | double

    This property is read-only.

    Indicator for the learnables, returned as 1 (true). A value of 1 indicates that the component contains Learnables.

    Data Types: logical

    This property is read-only.

    Indicator showing the learning status of the component, returned as 0 (false) or 1 (true). A value of 1 indicates that the learn object function has been applied to the component and the Learnables are nonempty.

    Data Types: logical

    Learnables

    The software sets learnables when you use the learn object function. You cannot modify learnables directly.

    This property is read-only.

    Encoded categories, returned as a cell array.

    Data Types: cell

    This property is read-only.

    Names of the variables used by the component to encode data, returned as a string array. The variables correspond to columns in the first data argument of learn.

    Data Types: string

    Object Functions

    learnInitialize and evaluate pipeline or component
    runExecute pipeline or component for inference after learning
    resetReset pipeline or component
    seriesConnect components in series to create pipeline
    parallelConnect components or pipelines in parallel to create pipeline
    viewView diagram of pipeline inputs, outputs, components, and connections

    Examples

    collapse all

    Create a oneHotEncoderComponent pipeline component. Specify to encode variables with ten or fewer categories.

    component = oneHotEncoderComponent(Limit=10)
    component = 
      oneHotEncoderComponent with properties:
    
                 Name: "OneHotEncoder"
               Inputs: "DataIn"
            InputTags: 1
              Outputs: "DataEncoded"
           OutputTags: 1
    
       
    Learnables (HasLearned = false)
           Categories: []
        UsedVariables: []
    
       
    Learn Parameters (unlocked)
                Limit: 10
    
    
    Show all parameters
    

    component is a oneHotEncoderComponent object that contains two learnables: Categories and UsedVariables. These properties remains empty until you pass data to the component during the learn phase.

    Load the census1994 data set. Store the predictor and response data in the tables X and Y, respectively. The predictor data contains 14 variables, 8 of which are categorical.

    load census1994
    X = adultdata(:,1:end-1);
    Y = adultdata(:,end);

    Use the learn object function to encode the categorical variables.

    component = learn(component,X)
    component = 
      oneHotEncoderComponent with properties:
    
                 Name: "OneHotEncoder"
               Inputs: "DataIn"
            InputTags: 1
              Outputs: "DataEncoded"
           OutputTags: 1
    
       
    Learnables (HasLearned = true)
           Categories: {[8×1 string]  [7×1 string]  [6×1 string]  [5×1 string]  [2×1 string]}
        UsedVariables: ["workClass"    "marital_status"    "relationship"    "race"    "sex"]
    
       
    Learn Parameters (locked)
                Limit: 10
    
    
    Show all parameters
    

    Note that the HasLearned property is set to true and Categories and UsedVariables are nonempty.

    Of the eight categorical variables in the data set, only five contain ten or fewer categories. Find the category names in the first encoded variable, workClass.

    categories = component.Categories{1}
    categories = 
    
      8×1 string array
    
        "Federal-gov"
        "Local-gov"
        "Never-worked"
        "Private"
        "Self-emp-inc"
        "Self-emp-not-inc"
        "State-gov"
        "Without-pay"

    Version History

    Introduced in R2026a

    See Also