From the course: Machine Learning with ML.NET

Build an ML model for sentiment analysis - .NET Tutorial

From the course: Machine Learning with ML.NET

Build an ML model for sentiment analysis

- In this video, we will look at how to build a machine learning model for doing sentiment analysis. So what is sentiment analysis? Sentiment analysis allows us to understand the sentiment based on a text, which is comments a user could have added either on an e-commerce site, or through a form submission, or through various other channels. So in this case, here's a sample dataset on what is the comment and a particular sentiment. So in this case, a comment which is rude is a toxic sentiment, and a toxic sentiment of zero is not toxic. All the features which go into training a model, in this case comment, is input are your features, and then the output column, or the column you want to predict, is the sentiment column. This dataset is based on the Wikipedia dataset. Another way to look at sentiment analysis is around what question do you want to ask? Is this A or B? That means, is it a positive or a negative sentiment? Is it a toxic comment? Yes or no. So sentiment analysis is an example of a binary classification task or type of problem that users want to solve, where they want to classify sentiments into positive or negative value. A typical machine learning workflow has three key tenants across preparing your data, building and training your model, and then consuming your model in your application. It comprises of the following steps. Now that we've understood what is sentiment analysis, let's see how you can build a sentiment analysis model. So this is a application that I have. It's a console application. And let's start by seeing how can we build a sentiment analysis model. I've installed Model Builder, which is a visual tool to build custom machine learning models, and I'm going to launch it by doing Add, Machine Learning on my application. In this case, since I'm doing sentiment analysis, I'm going to choose the Sentiment Analysis template. The next step in this process is around loading my dataset and choosing the column I want to predict. I can either choose from File or SQL Server. In this case, I'm going to choose File, which is a CSV file or a TSV file. So I'm going to upload the wikipedia.tsv file. And in this case, I'm going to see a quick preview of the first 10 rows of my dataset so that I can get an idea as to what makes a sentiment text positive or negative. That means toxic or not toxic. And I'm going to choose the column that I want to predict, which is going to be my label output. So in this case, I'm going to choose Sentiment. The next step in the machine learning process is around training the machine learning model. So I'm going to start by training it for 10 seconds. Underneath the covers, Model Builder uses automated machine learning, which figures out or evaluates different machine learning algorithms with different settings. So as a developer, I don't have to worry about the intrinsics of knowing all the machine learning algorithms and fine-tuning the performance. Once the model has been trained for 10 seconds, it's going to finalize the model and the training process is then complete. I can then go to the Evaluate screen and I can evaluate the performance of this model. So while the tool shows me what was the best model, it also shows me what were the other models explored. In this case, since I only ran it for 10 seconds, there was only one model that was explored. This was the accuracy of the model. And these are some other ways I can evaluate the model as well. The last step in this process is around consuming the model. So I can hit the Add Projects button, which is going to add two projects to my solution. It's going to add a project which is a ConsoleApp, which has code in ModelBuilder.cs on how the model was trained, so I can retrain the model later. And then, it has a class library as well, which has the Model.zip file, which is the actual machine learning model that was trained on my dataset. So let's go ahead and quickly consume this model in my ConsoleApp. So I'm going to add a reference to the class library, which is going to bring in the model inputs and the actual model as well. And we'll go back to the Model Builder screen, and I'm going to use this code to consume the model in my app. I'm going to paste this code. I'm going to add references to the class library that I just added a reference to. And let's understand this code on how to consume the model. As we have seen, I'll create a MLContext, which I'll use to load the model that I just trained, and I'm going to create a prediction engine. You can think about it as a function that you can call where you can pass in your input data and you can get the predicted sentiment of that input data. So in this case, I'm going to write some comment that I want to predict the sentiment for. So I'm just going to say, SentimentText equals that is rudes. And then, I can run this app and I can see the output of calling the Predict function. So I can say, Console.WriteLine, and then result.Prediction. And the result of true means it's toxic and zero means it's not toxic. So let's go ahead and run this application. It's going to launch a ConsoleApp, which is going to load the model, and it's going to try the Predict function. And so this is a toxic comment. So this was a very easy way for me to add sentiment analysis or build a custom model for doing sentiment analysis in my application. So now that we saw how to build a custom machine learning model for doing sentiment analysis, in the next video, we will look at how to build a custom machine learning model for classifying issues on GitHub.

Contents