Contenuto principale

predict

R2026b

Predict responses for new observations from neural network incremental learning model

Since R2026b

Description

Label = predict(Mdl,X) returns the predicted responses (or labels) label of the observations in the predictor data X from the incremental learning model Mdl.

example

Label = predict(Mdl,X,ObservationsIn=dimension) specifies the predictor data observation dimension, either "rows" (default) or "columns". For example, specify ObservationsIn="columns" to indicate that columns in the predictor data correspond to observations.

[Label,Score] = predict(___) uses any of the input argument combinations in the previous syntaxes and additionally returns classification scores score for each class when Mdl is an incremental learning model for classification.

Examples

collapse all

Create an incremental learning model by converting a traditionally trained neural network model, and predict class labels using both models.

Load the human activity data set.

load humanactivity

For details on the data set, enter Description at the command line.

Fit a neural network classification model to the entire data set.

Mdl = fitcnet(feat,actid);

Mdl is a ClassificationNeuralNetwork model object representing a traditionally trained neural network classification model.

Convert the traditionally trained neural network classification model to a model for incremental learning.

IncrementalMdl = incrementalLearner(Mdl)
IncrementalMdl = 
  incrementalClassificationNeuralNetwork

                   IsWarm: 0
                  Metrics: [1×2 table]
               ClassNames: [1 2 3 4 5]
           ScoreTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "softmax"
                   Solver: "minibatch-lbfgs"


  Properties, Methods

IncrementalMdl is an incrementalClassificationNeuralNetwork model object prepared for incremental learning.

The incrementalLearner function initializes the incremental learner by passing the neural network and model parameters to it, along with other information Mdl extracts from the training data. An incremental learner created from converting a traditionally trained model can generate predictions without further processing.

Predict class labels for all observations using both models.

ttlabels = predict(Mdl,feat);
illabels = predict(IncrementalMdl,feat);
isequal(ttlabels,illabels)
ans = logical
   1

Both models predict the same labels for each observation.

Create an incremental learning model by converting a traditionally trained neural network model, and predict responses using both models.

Load the 2015 NYC housing data set. For more details on the data, see NYC Open Data.

load NYCHousing2015

Extract the response variable SALEPRICE from the table. For numerical stability, scale SALEPRICE by 1e6.

Y = NYCHousing2015.SALEPRICE/1e6;
NYCHousing2015.SALEPRICE = [];

To reduce computational cost for this example, remove the NEIGHBORHOOD column, which contains a categorical variable with 254 categories.

NYCHousing2015.NEIGHBORHOOD = [];

Create dummy variable matrices from the other categorical predictors.

catvars = ["BOROUGH","BUILDINGCLASSCATEGORY"];
dumvarstbl = varfun(@(x)dummyvar(categorical(x)),NYCHousing2015, ...
    InputVariables=catvars);
dumvarmat = table2array(dumvarstbl);
NYCHousing2015(:,catvars) = [];

Treat all other numeric variables in the table as predictors of sales price. Concatenate the matrix of dummy variables to the rest of the predictor data.

idxnum = varfun(@isnumeric,NYCHousing2015,OutputFormat="uniform");
X = [dumvarmat NYCHousing2015{:,idxnum}];

Fit a neural network regression model to the entire data set. Standardize the predictor data.

Mdl = fitrnet(X,Y,Standardize=true)
Mdl = 
  RegressionNeuralNetwork
             ResponseName: 'Y'
    CategoricalPredictors: []
        ResponseTransform: 'none'
          NumObservations: 91446
               LayerSizes: 10
              Activations: 'relu'
    OutputLayerActivation: 'none'
                   Solver: 'LBFGS'
          ConvergenceInfo: [1×1 struct]
          TrainingHistory: [1000×7 table]


  Properties, Methods

Mdl is a RegressionNeuralNetwork model object representing a traditionally trained neural network regression model.

Convert the traditionally trained neural network regression model to a model for incremental learning. Use the FreeREX solver.

IncrementalMdl = incrementalLearner(Mdl,TrainingOptions=incrementalTrainingOptions("freerex"))
IncrementalMdl = 
  incrementalRegressionNeuralNetwork

                   IsWarm: 1
                  Metrics: [1×2 table]
        ResponseTransform: 'none'
               LayerSizes: 10
              Activations: "relu"
    OutputLayerActivation: "none"
                   Solver: "freerex"


  Properties, Methods

IncrementalMdl is an incrementalRegressionNeuralNetwork model object prepared for incremental learning.

The incrementalLearner function initializes the incremental learner by passing model parameters to it, along with other information Mdl extracted from the training data. IncrementalMdl is warm (IsWarm is 1), which means that incremental learning functions can start tracking performance metrics.

An incremental learner created from converting a traditionally trained model can generate predictions without further processing.

Predict sales prices for all observations using both models.

ttyfit = predict(Mdl,X);
ilyfit = predict(IncrementalMdl,X);
compareyfit = norm(ttyfit - ilyfit)
compareyfit = 
0

The difference between the fitted values generated by the models is 0.

Input Arguments

collapse all

Incremental learning model, specified as an incrementalClassificationNeuralNetwork or incrementalRegressionNeuralNetwork model object. You can create Mdl directly or by converting a supported, traditionally trained machine learning model using the incrementalLearner function. For more details, see the corresponding reference page.

To generate predictions, the model must be trained. That is, you must pass Mdl and data to fit or updateMetricsAndFit before calling predict, or convert a traditionally trained model using incrementalLearner.

Chunk of predictor data, specified as a floating-point matrix of n observations and Mdl.NumPredictors predictor variables. The value of the ObservationsIn name-value argument determines the orientation of the variables and observations. The default ObservationsIn value is "rows", which indicates that observations in the predictor data are oriented along the rows of X.

The length of the observation responses (labels) Y and the number of observations in X must be equal; Y(j) is the response (label) of observation j (row or column) in X.

Note

  • predict supports only floating-point input predictor data. If your input data includes categorical data, you must prepare an encoded version of the categorical data. Use dummyvar to convert each categorical variable to a numeric matrix of dummy variables. Then, concatenate all dummy variable matrices and any other numeric predictors. For more details, see Dummy Variables.

Data Types: single | double

Predictor data observation dimension, specified as "rows" or "columns".

Example: ObservationsIn="columns"

Data Types: char | string

Output Arguments

collapse all

Predicted responses (labels), returned as a categorical or character array; floating-point, logical, or string vector; or cell array of character vectors with n rows. n is the number of observations in X, and label(j) is the predicted response for observation j.

  • For regression problems, Label is a floating-point vector.

  • For classification problems, Label has the same data type as the class names stored in Mdl.ClassNames. (The software treats string arrays as cell arrays of character vectors.)

    The predict function assigns the observation to the class that yields the largest classification score (posterior probability). If the neural network uses the softmax output layer activation (default), scores represent posterior class probabilities.

Classification scores, returned as an n-by-K numeric matrix when Mdl is an incrementalClassificationNeuralNetwork model. n is the number of observations in X and K is the number of classes (numel(Mdl.ClassNames)).

Score(j,k) is the classification score of observation j being classified into class k. The class order corresponds to Mdl.ClassNames.

If the output layer activation is "softmax" (default for classification), the scores are posterior class probabilities. That is, for each observation, the scores across all classes sum to 1.

Algorithms

collapse all

Version History

Introduced in R2026b