Like the Features and Label input column names, ML.NET has default names for the predicted value columns produced by a model. The names can differ depending on the task.
Because the algorithm used in this sample is a linear regression algorithm, the default name of the output column is Score, which is defined by the ColumnName attribute on the PredictedPrice property.
Whether making a single or batch prediction, the prediction pipeline needs to be loaded into the application. This pipeline contains both the data preprocessing transformations and the trained model. The following code snippet loads the prediction pipeline from a file named model.zip.
C#
//Create MLContext
MLContext mlContext = new MLContext();
// Load Trained Model
DataViewSchema predictionPipelineSchema;
ITransformer predictionPipeline = mlContext.Model.Load("model.zip", out predictionPipelineSchema);
Single prediction
To make a single prediction, create a PredictionEngine using the loaded prediction pipeline.
Then, use the Predict method and pass in your input data as a parameter. Notice that using the Predict method doesn't require the input to be an IDataView. That's because it conveniently internalizes the input data type manipulation so you can pass in an object of the input data type. Additionally, since CurrentPrice is the target or label you're trying to predict using new data, it's assumed there's no value for it at the moment.
C#
// Input Data
HousingData inputData = new HousingData
{
Size = 900f,
HistoricalPrices = newfloat[] { 155000f, 190000f, 220000f }
};
// Get Prediction
HousingPrediction prediction = predictionEngine.Predict(inputData);
If you access the Score property of the prediction object, you should get a value similar to 150079.
Tip
PredictionEngine is not thread-safe. Additionally, you have to create an instance of it everywhere it is needed within your application. As your application grows, this process can become unmanageable. For improved performance and thread safety, use a combination of dependency injection and the PredictionEnginePool service, which creates an ObjectPool of PredictionEngine objects for use throughout your application.
Given the following data, load it into an IDataView. In this case, the name of the IDataView is inputData. Because CurrentPrice is the target or label you're trying to predict using new data, it's assumed there's no value for it at the moment.
C#
// Actual data
HousingData[] housingData = new HousingData[]
{
new HousingData
{
Size = 850f,
HistoricalPrices = newfloat[] { 150000f, 175000f, 210000f }
},
new HousingData
{
Size = 900f,
HistoricalPrices = newfloat[] { 155000f, 190000f, 220000f }
},
new HousingData
{
Size = 550f,
HistoricalPrices = newfloat[] { 99000f, 98000f, 130000f }
}
};
Then, use the Transform method to apply the data transformations and generate predictions.
C#
// Predicted Data
IDataView predictions = predictionPipeline.Transform(inputData);
Inspect the predicted values by using the GetColumn method.
C#
// Get Predictionsfloat[] scoreColumn = predictions.GetColumn<float>("Score").ToArray();
The predicted values in the score column should resemble the following values:
Observation
Prediction
1
144638.2
2
150079.4
3
107789.8
Multiple predictions (PredictionEnginePool)
To make multiple predictions using PredictionEnginePool, you can take an IEnumerable containing multiple instances of your model input. For example, take an IEnumerable<HousingInput> and apply the Predict method to each element using the LINQ Select method.
This code sample assumes you have a PredictionEnginePool called predictionEnginePool and an IEnumerable<HousingData> called housingData.
The result is an IEnumerable containing instances of your predictions. In this case, it would be IEnumerable<HousingPrediction>.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Manage data ingestion and preparation, model training and deployment, and machine learning solution monitoring with Python, Azure Machine Learning and MLflow.
Learn how to build machine learning models, collect metrics, and measure performance with ML.NET. A machine learning model identifies patterns within training data to make predictions using new data.
The ML.NET Automated ML (AutoML) API automates the model building process and generates a model ready for deployment. Learn the options that you can use to configure automated machine learning tasks.