Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Learn how to build a multiclass classification model using Model Builder to categorize the risk level of restaurant violations found during health inspections.
In this tutorial, you learn how to:
For a list of prerequisites and installation instructions, visit the Model Builder installation guide.
This sample creates a C# console application that categorizes the risk of health violations using a machine learning model built with Model Builder. You can find the source code for this tutorial at the dotnet/machinelearning-samples GitHub repository.
Create a C# console application called "RestaurantViolations".
The data set used to train and evaluate the machine learning model is originally from the San Francisco Department of Public Health Restaurant Safety Scores. For convenience, the dataset has been condensed to only include the columns relevant to train the model and make predictions. Visit the following website to learn more about the dataset.
Download the Restaurant Safety Scores dataset and unzip it.
Each row in the dataset contains information regarding violations observed during an inspection from the Health Department and a risk assessment of the threat those violations present to public health and safety.
InspectionType | ViolationDescription | RiskCategory |
---|---|---|
Routine - Unscheduled | Inadequately cleaned or sanitized food contact surfaces | Moderate Risk |
New Ownership | High risk vermin infestation | High Risk |
Routine - Unscheduled | Wiping cloths not clean or properly stored or inadequate sanitizer | Low Risk |
The label
is the column you want to predict. When performing a classification task, the goal is to assign a category (text or numerical). In this classification scenario, the severity of the violation is assigned the value of low, moderate, or high risk. Therefore, the RiskCategory is the label. The features
are the inputs you give the model to predict the label
. In this case, the InspectionType and ViolationDescription are used as features or inputs to predict the RiskCategory.
When first adding Model Builder to the solution it will prompt you to create an mbconfig
file. The mbconfig
file keeps track of everything you do in Model Builder to allow you to reopen the session.
mbconfig
project RestaurantViolationsPrediction, and click the Add button.To train your model, select from the list of available machine learning scenarios provided by Model Builder. In this case, the scenario is Data classification.
Model Builder accepts data from a SQL Server database or a local file in csv
, tsv
, or txt
format.
The machine learning task used to train the issue classification model in this tutorial is multiclass classification. During the model training process, Model Builder trains separate models using different multiclass classification algorithms and settings to find the best performing model for your dataset.
The time required for the model to train is proportional to the amount of data. Model Builder automatically selects a default value for Time to train (seconds) based on the size of your data source.
Throughout the training process, progress data is displayed in the Training results
section of the train step.
Once training is complete the mbconfig
file will have the generated model called RestaurantViolationsPrediction.zip
after training and two C# files with it:
Click the Next step button to navigate to the evaluate step.
The result of the training step will be one model which had the best performance. In the evaluate step of the Model Builder tool, in the Best model section, will contain the algorithm used by the best performing model in the Model entry along with metrics for that model in Accuracy.
Additionally, in the Output window of Visual Studio, there will be a summary table containing top models and their metrics.
This section will also allow you to test your model by performing a single prediction. It will offer text boxes to fill in values and you can click the Predict button to get a prediction from the best model. By default this will be filled in by a random row in your dataset.
This step will have project templates that you can use to consume the model. This step is optional and you can choose the method that best suits your needs on how to serve the model.
When adding a console app to your solution, you will be prompted to name the project.
Name the console project RestaurantViolationsPrediction_Console.
Click Add to solution to add the project to your current solution.
Run the application.
The output generated by the program should look similar to the snippet below:
InspectionType: Routine - Unscheduled
ViolationDescription: Moderate risk food holding temperature
Predicted RiskCategory: Moderate Risk
When adding a web API to your solution, you will be prompted to name the project.
Name the Web API project RestaurantViolationsPrediction_API.
Click Add to solution* to add the project to your current solution.
Run the application.
Open PowerShell and enter the following code where PORT is the port your application is listening on.
$body = @{
InspectionType="Reinspection/Followup"
ViolationDescription="Inadequately cleaned or sanitized food contact surfaces"
}
Invoke-RestMethod "https://localhost:<PORT>/predict" -Method Post -Body ($body | ConvertTo-Json) -ContentType "application/json"
If successful, the output should look similar to the text below. The output has the predicted RiskCategory as Moderate Risk and it has the scores of each of the input labels - Low Risk, High Risk, and Moderate Risk.
prediction score
---------- -----
Moderate Risk {0.055566575, 0.058012854, 0.88642055}
Congratulations! You've successfully built a machine learning model to categorize the risk of health violations using Model Builder. You can find the source code for this tutorial at the dotnet/machinelearning-samples GitHub repository.
To learn more about topics mentioned in this tutorial, visit the following resources:
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Train a machine learning model for predictive maintenance by using ML.NET Model Builder - Training
In this module, you'll learn how to use ML.NET Model Builder to train and consume a machine learning model for predictive maintenance.
Certification
Microsoft Certified: Azure Data Scientist Associate - Certifications
Manage data ingestion and preparation, model training and deployment, and machine learning solution monitoring with Python, Azure Machine Learning and MLflow.