Predicting with Deployed Models

This guide shows how to make predictions, perform transformations, and work with deployed models. Deployed models provide the same prediction capabilities as fitted models while using significantly less memory.

How to predict, forecast, and transform

Deployed models support the same prediction and transformation APIs as fitted models. The key difference is that deployed models do not have access to training data or diagnostic information.

For regression and classification models, use the Predict method:

C#
// Make a prediction with a deployed model
var features = Vector.Create(1.5, 2.5);
double predicted = model.Predict(features, ModelInputFormat.OriginalVariables);

For transformation models like PCA, use the Transform method to project new data into the reduced-dimension space:

C#
// Transform new data using a deployed PCA model
var newSamples = Matrix.Create<double>(10, 5);
var transformed = pca.Transform(newSamples);

The ModelInputFormat argument specifies how to interpret the input data. The most common options are:

  • ModelInputFormat.OriginalVariables: The input matches the original variables used when training the model.

  • ModelInputFormat.Automatic: The format is inferred from the number of input variables (default).

What users must supply

When making predictions with a deployed model, you must supply features in a format compatible with how the model was trained:

  • Column names or positions: If the model was trained with named columns (from a DataFrame), you can use either a DataFrame with the same column names, or a vector/matrix with values in the correct order.

  • Categorical encoding: Categorical variables must use the same encoding (category names) as during training. The deployed model retains the necessary mapping information.

  • Number of features: The input must have the correct number of features for the model type and input format.

Worked examples

The following examples demonstrate common prediction scenarios.

Regression: Predicting a numeric value

For regression models, the Predict method returns the predicted value(s) for the given input:

C#
// Load a saved regression model
var model = LinearRegressionModel.FromJson(json);

// Predict single observation
var features = Vector.Create(1.5, 2.5, 3.5);
double predicted = model.Predict(features);

// Predict multiple observations
var data = Matrix.Create<double>(10, 3);
var predictions = model.Predict(data);

Classification: Predicting class labels or probabilities

For classification models like logistic regression, you can predict class indices or obtain class probabilities:

C#
// Load a saved logistic regression model
var model = LogisticRegressionModel.FromJson(json);

// Predict class probability
var features = Vector.Create(1.5, 2.5);
double probability = model.Probability(features);

// Predict class (0 or 1)
int predictedClass = model.Predict(features);

PCA: Transforming new samples

For dimensionality reduction models like PCA, use the Transform method to project new samples into the component space:

C#
// Load a saved PCA model
var pca = PrincipalComponentAnalysis.FromJson(json);

// Transform new sample to component space
var sample = Vector.Create(1.0, 2.0, 3.0, 4.0);
var components = pca.Transform(sample);

// Transform multiple samples
var samples = Matrix.Create<double>(10, 4);
var allComponents = pca.Transform(samples);

Clustering: Assigning clusters to new samples

For clustering models like K-Means, use the Predict method to find the nearest cluster for new samples:

C#
// Load a saved K-Means model
var kmeans = KMeansClusterAnalysis.FromJson(json);

// Predict cluster for new sample
var sample = Vector.Create(1.0, 2.0, 3.0);
int cluster = kmeans.Predict(sample);

// Predict clusters for multiple samples
var samples = Matrix.Create<double>(10, 3);
var clusters = kmeans.Predict(samples);

Important notes

  • Diagnostics are not available: Deployed models do not have access to residuals, fitted values, or goodness-of-fit statistics. Attempting to access these properties will throw an InvalidOperationException.

  • Variable mappings still work: The model adapter contained in the deployed model maintains the mapping between original variable names and the internal model representation.

  • Refitting is not possible: Deployed models cannot be refitted because they do not retain the training data or optimization state.

See Also