Main Content

prune

Remove unnecessary components and dependencies from pipeline

Since R2026a

    Description

    newPipeline = prune(pipeline,outputs) creates a reduced version of the pipeline pipeline by retaining only the components, inputs, and dependencies required to produce the specified outputs in outputs.

    The function traces all dependencies needed to compute the requested outputs and removes any components or inputs that are not required. prune also considers the current state of each component. For example, if the learn object function has been applied to a component, the pipeline excludes any inputs used only for learning from the list of dependencies to retain. However, prune includes any dependencies necessary for components that have not completed the learn phase. That is, regardless of the specified outputs, the pipeline retains all components and inputs required for learning.

    example

    Examples

    collapse all

    Prune a classification pipeline to return the classification error only, and not predictions or scores.

    Create a simple classification pipeline with two components: one that retains the principal components that explain 95% of the variance, and one that is a multiclass SVM classifier (ECOC model).

    pca = pcaComponent(VarianceExplained=0.95);
    ecoc = classificationECOCComponent;
    pipeline = series(pca,ecoc)
    pipeline = 
    
      LearningPipeline with properties:
    
                 Name: "defaultName"
               Inputs: ["DataIn"    "Response"]
            InputTags: [1 2]
              Outputs: ["Predictions"    "Scores"    "Loss"]
           OutputTags: [1 0 0]
    
           Components: struct with 2 entries
          Connections: [6×2 table]
    
        HasLearnables: true
           HasLearned: false
    
    
    Show summary of the components

    pipeline is a LearningPipeline object that contains learnables but has not yet completed the learn phase.

    Read the fisheriris data set into a table. Store the predictor and response data in the tables X and Y, respectively.

    fisheriris = readtable("fisheriris.csv");
    X = fisheriris(:,1:end-1);
    Y = fisheriris(:,end);

    Pass the X and Y training data to the learn object function of pipeline.

    learnedPipeline = learn(pipeline,X,Y)
    learnedPipeline = 
    
      LearningPipeline with properties:
    
                 Name: "defaultName"
               Inputs: ["DataIn"    "Response"]
            InputTags: [1 2]
              Outputs: ["Predictions"    "Scores"    "Loss"]
           OutputTags: [1 0 0]
    
           Components: struct with 2 entries
          Connections: [6×2 table]
    
        HasLearnables: true
           HasLearned: true
    
    
    Show summary of the components

    The resulting pipeline learnedPipeline has learned.

    Prune learnedPipeline to return the classification error only.

    prunedPipeline = prune(learnedPipeline,"Loss")
    prunedPipeline = 
    
      LearningPipeline with properties:
    
                 Name: "defaultName"
               Inputs: ["DataIn"    "Response"]
            InputTags: [1 2]
              Outputs: "Loss"
           OutputTags: 0
    
           Components: struct with 2 entries
          Connections: [4×2 table]
    
        HasLearnables: true
           HasLearned: true
    
    
    Show summary of the components

    prunedPipeline has only one output (Loss) instead of the three outputs in learnedPipeline (Predictions, Scores, and Loss).

    Input Arguments

    collapse all

    Existing pipeline, specified as a LearningPipeline object.

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

    Data Types: char | string | cell

    Output Arguments

    collapse all

    Pruned pipeline, returned as a LearningPipeline object.

    Tips

    • Use prune to create a lean inference pipeline after all components have been learned. The pruned pipeline removes parts of the pipeline that are no longer needed for prediction or deployment.

    Version History

    Introduced in R2026a